We run mypy as part of pre-commit. Here is the portion of .pre-commit-config.yaml that refers to mypy
rev: v0.910
# https://github.com/pre-commit/mirrors-mypy/issues/5
hooks:
- id: mypy
name: mypy module1
files: ^path/to.module1
- id: mypy
name: mypy module2
files: ^path/to.module2
I recently made a change to a file being tested by one of those hooks and pre-commit failed with:
path/to/module/main.py:9: error: Library stubs not installed for "requests" (or incompatible with Python 3.9)
path/to/module/main:9: note: Hint: "python3 -m pip install types-requests"
path/to/module/main:9: note: (or run "mypy --install-types" to install all missing stub packages)
path/to/module/main:9: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
path/to/module/main:14: error: Library stubs not installed for "requests.models" (or incompatible with Python 3.9)
I fixed this by specifying types-requests as an additional dependency in .pre-commit-config.yaml so it now looks like this:
rev: v0.910
# https://github.com/pre-commit/mirrors-mypy/issues/5
hooks:
- id: mypy
additional_dependencies: [types-requests]
name: mypy module1
files: ^path/to.module1
- id: mypy
additional_dependencies: [types-requests]
name: mypy module2
files: ^path/to.module2
Someone else on my team suggested adding this to mypy.ini because we also run mypy in our CI pipeline so it would make sense if pre-commit and the CI pipeline were both using the same mypy.ini. I've been reading https://mypy.readthedocs.io/en/stable/config_file.html but it doesn't (AFAICT) cover anything pertaining to defining additional dependencies.
I tried reverting my change to .pre-commit-config.yaml and instead placing file mypy.ini at the root of my repo containing:
[mypy]
additional_dependencies = [types-requests]
but upon running pre-commit again I got the same errors as before. Furthermore if I run mypy in a standalone fashion I see the following:
mypy.ini: [mypy]: Unrecognized option: additional_dependencies = [types-requests]
What is the correct way to define additional dependencies in mypy.ini?