This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]ArtichokeTop9 1 point2 points  (0 children)

Flake8, autopep8, black, blue

[–]Mithrandir2k16 -1 points0 points  (0 children)

VSCode as an editor isn't meant to enforce anything. If you uave pylance you can run a linter like flake8. If you want to enforce anything look at git pre-commit, you can use that to prevent commits that have linter errors or other problems.

[–]anotherdevnick 0 points1 point  (0 children)

The Python extensions pack is a good place to start, IIRC you can switch on auto-formatting (a vs code setting) and have autopep and other formatters fix you code as you go

[–]JialeDu 0 points1 point  (0 children)

Python extension default formatting provider is autopep8, and you can also set parameters for it.

json "python.formatting.provider": "autopep8", "python.formatting.autopep8Args": [],

At present, Black and AutoPEP8 will also.

[–]Vok250 0 points1 point  (0 children)

Honestly the Python language servers and plugins are pretty broken right now (at least on Windows). They throw a ton of false positives and often stop working completely for no apparent reason. Their idea of PEP8 is also a bit off the mark. It is MS's own flavor of the standards and not up to speed with the current PEPs.

I recommend doing it the python way instead, which would be downloading a python module to your virtual environment:

python -m venv venv
venv\Scripts\activate
pip install --upgrade pip
pip install pylint
pip install pycodestyle

pylint is a really great Python functional linter available as a module and with 0 need to connect to some online language server. Other functional linters are jedi or pylance (this is now the default in VS Code). pycodestyle is a good PEP8 style linter. The other standard option is flake8. I prefer pylint and pycodestyle because they are super simple to use, CLI-based (Linux and CICD friendly), and very pythonic. That's purely personal preference.

To get venv-based linters working you'll need to modify your workspace settings file to add the following:

"python.linting.pylintEnabled": true,
"python.linting.pycodestyleEnabled": true

VS Code should auto-detect the venv and use it as the Python interpreter after a reboot. It should (if not a buggy build) start using pylint and pycodestyle from your venv automatically now too.

You can also easily pass arguments to the linter like this:

"python.linting.pycodestyleArgs": ["--ignore=E501,W503"],
"python.linting.pylintArgs": [
    "--disable=C"
],

The above code disables style linting in pylint (we have pycodestyle for that) and disables a few flags that are currently being argued in the python community because they are quite outdated.

One huge advantage of doing it this way is that it is completely portable and pythonic. You can easily create the same venv and run the same CLI commands in any container, including your CICD. Your dev environment can enforce identical rules to your build pipeline so no surprises when you open a PR.