- Ruff, also available as a VSCode extension.
- Ruff config in
pyproject.toml
under[tool.ruff]
. - Linting and format-on-save are available by adding the following to VSCode
settings.json
once the extension is installed:
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff"
},
settings.json
can be accessed by searching forPreferences: Open User Settings (JSON)
in the VSCode command palette.
- Check
pre-commit
is installed:
pre-commit --version
- If not installed:
brew install pre-commit
- Generate the git hooks script from the
.pre-commit-config.yaml
file:
pre-commit install
Local workspace checking:
mypy
must be installed.- The best option seems to be to install
mypy
locally (in the project virtual environment), and then set"mypy.runUsingActiveInterpreter" : true,
in VSCode user setting (mentioned above how to find these). The Mypy daemon can then have visibility on the dependencies currently isntalled in the project environment (see info on the daemon below).
- The best option seems to be to install
- The VSCode extension (
Mypy
by Matan Gover, which is better than the MicrosoftMypy Type Checker
extension) will run a server to do live type checking of the entire workspace, not just files that have changes. - Config in
pyproject.toml
under[tool.mypy]
(it's set up to leave any entirely un-annotated functions un-checked, to provide good type checking whilst allowing for partial type coverage). [[tool.mypy.overrides]]
sections inpyproject.toml
allow for module specific settings, or applying a set of settings to a list of modules - eg. to blacklist a set of modules that do not have type support and are to be ignored by mypy.
Pre-commit checking:
- The pre-commit hook will only type check files that have changed, so is not as strict as the local type checking with the VSCode extension (mypy daemon).
- If an external module does have type support, it should not be
blacklisted, and instead the dependency must be added to the
.pre-commit-config.yaml
file, underadditional_dependencies
for the mypy hook. - The mypy pre-commit checks are run in a seperate python environment, without the modules that have been installed into the project working envionment. This is the reason that the pre-commit hook needs to be aware of the 3rd-party modules that do have type support, and for which type stubs must be fetched.