MyPy was added in #18210, allowing for static type checking of type hints in our testing framework. However, there is still some work to be done before we can take advantage of these new capabilities. 🚀
Right now, lint-python.sh
runs MyPy with the --ignore-imports
flag. This is done primarily to ignore the lack of type hinting for zmq
, but it hides many errors in our own code too.
To begin to see what’s needed, first run mypy test/functional
. You’ll see two sets of errors. One complains about Skipping analyzing 'zmq'
. This one can be ignored for now by changing the identified import zmq
lines to import zmq # type: ignore
.
The other states that it Cannot find implementation or library stub for module named 'data'
. Remember that for a second.
Now go into test/functional/test_framework
and run mypy mininode.py
. Observe two more Cannot find implementation or library stub for module
errors, now for test_framework.messages
and test_framework.util
.
What’s happening here is that MyPy is failing to handle importing our test_framework
package. We’ll need to do a few things to fix this.
- MYPYPATH and/or mypy_path
- Using either the environment variable MYPYPATH or the config file option
mypy_path
we need to tell MyPy where to find ourtest_framework
package.
- Using either the environment variable MYPYPATH or the config file option
- Stub Files
- We need to create stub files (ending in
.pyi
) for the modules intest_framework
- https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-type-hints-for-third-party-library
- https://mypy.readthedocs.io/en/stable/stubs.html#stub-files
- This can be made much easier with stubgen, and possibly MonkeyType or PyAnnotate
- We need to create stub files (ending in
- Remove –ignore-missing-imports and add
# type: ignore
toimport zmq
--ignore-missing-imports
simply hides too many errors.
Resources
Important Notes
Our minimum supported python version is 3.5, so any changes must be compatible. In particular this means that we must use type comments, as opposed to type hinting, in variables that aren’t function parameters. (i.e. foo # type: int
as opposed to foo: int
). This will complicate the use of MonkeyType, as it doesn’t support type comments. It may be useful to make a scripted diff between type comments and type hints.
Useful skills:
MyPy experience
Want to work on this issue?
For guidance on contributing, please read CONTRIBUTING.md before opening your pull request.