Python specific
Type hints
For some time now python supports type hints. These allow to add type annotations to variables and function declarations, much like they are required in many statically typed languages (C, C++, …). They are called “annotations” or “hints” because they are not enforced (i.e., checked) at runtime, although tools like mypy and pyright can be used to perform type checks.
Although type hints should not be used to substitute function docstrings, they help a lot to document code and improve readability and could be ultimately used to perform type checking.
Visual Studio Code extensions
Python extension
Basic extension for writing python code in VS Code. Integrates the python debugger, allows to specify and change the interpreter (i.e., environment) and has support for jupyter notebooks among many other things.
Automatically format code (black and ruff)
This is probably the easiest and cheapest way to improve readability and help to adhere to some of pythons coding standards (or PEPs). If you work on your own codebase, I think there are few reasons not to (auto-)format your code. Setting this up is straight forward and takes at most a few minutes (if you include the time for reading up on different formatters).
Steps
- From this list, choose and install a code formatter (I previously used
black
, butruff
offers the same functionality and is a lot faster) - Configure your default formatter.
- Set VSCode to automatically format code when saving a file
autoDocstring
Generate docstrings for functions and methods. After placing the cursor below the function or method header, hitting Ctrl + Shift + 2
will generate a correctly formatted docstring template with the names of arguments where you only need to add a little bit of description. In the settings you can choose between different docstring formats. I personally prefer the numpy
style.
If you use type hints, autoDocstring will pick these up from the function header and add them to the docstring template, which is quite convenient.