Python Lint And Format

Created: January 04, 2021  |  1 minute read

Lint

pylint

As pylint has too many options, it recommends to use the pylint config file:

# file ~/.pylintrc, can be generated by pylint --generate-rcfile

[MESSAGES CONTROL]

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
# multiple time.
#enable=

# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once).

# C116 for missing-function-docstring
disable=C0116

But we can also ignore some warnings directly in the pylint command:

pylint . -j 0 --disable=C0116

flake8

# ignore W503 because of black format. BTW, flake8 also has W504 which is in contrary to W503.
flake8 . --exclude=venv --ignore=W503
flake8 [a_file_path]

Format

isort

isort . --profile=black --virtual-env=venv --recursive --check-only
isort . --profile=black --virtual-env=venv --recursive
isort [a_file_path]

Be very careful with isort, it’s not uncompromising, especially for some codes that dynamically import some modules inside a function instead of from the beginning of a file. People use often this to avoid circular import problem. Always run the tests after the isort.

black

black . --check
black .
black [a_file_path]

VSCode

Just my 2 cents, try the errorlens extension in VSCode, it will lint all the warnings/errors on live when coding, it’s really cool.

And don’t forget to install the official SonarLint extension, it will give you extra lint. It eats a lot of memory with its java processes nevertheless.

Leave a comment