Repository navigation
Description
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_pathwe need to tell MyPy where to find ourtest_frameworkpackage.
- 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: ignoretoimport zmq--ignore-missing-importssimply hides too many errors.
Resources
- Import handling is confusing and misleading python/mypy#4542
- https://mypy.readthedocs.io/en/stable/existing_code.html
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.
Activity
fanquake commentedon Jun 27, 2020
Just a note for anyone looking into this. Make sure that whatever changes you plan on making are compatible with Python 3.5, as that is our minimum supported version of Python.
ysangkok commentedon Jun 27, 2020
Why would you use stub files for in-tree modules? Why not add the type annotations in the implementation file?
troygiorshev commentedon Jun 27, 2020
This is a very important point, I've updated the issue to reflect it. 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: intas opposed tofoo: int). This will complicate the use of MonkeyType, as it doesn't support type comments. It may be easy to make a scripted diff between type comments and type hints.afaik we will need to do both, and this is simply a limitation of MyPy's design. I'm more than happy to be proven wrong!
am1r021 commentedon Jul 6, 2020
I am open to take over this issue if there is no one actively working on it.
JeremyRubin commentedon Jul 6, 2020
I have some of the library parts mypy typed (I think messages.py and script.py), if anyone wants them I can share.
am1r021 commentedon Jul 15, 2020
I generated the stub files using stubgen, but they do not seem to be python 3.5 compatible. I cannot think of a better solution than to write a script to change non-function-parameter type hints (i.e.
foo: int) into type comments (i.e.foo # type: int). I think this is what @troygiorshev means by "scripted diff", right?troygiorshev commentedon Jul 16, 2020
@ghorbanian Thanks for giving this a look! Yup, that's exactly what I mean by "scripted diff". I'll go check out #19532.
emc99 commentedon Mar 9, 2021
I am keen on helping out with this issue. Where do I start? Should I go away and learn C++ and Python first?
NelsonFrancisco commentedon Mar 29, 2021
I just want to point out two things because of my experience:
I don't see any mention in this repo about a Python virtualenv to facilitate development. Is it too far fetched or a script/tutorial would be relevant?
Maybe it's because it is outdated, so an update would be relevant:
mininode.py does not exist in that folder.
fanquake commentedon May 28, 2021
Since the release of PyZMQ 21.0, the library now contains mypy stubs:
This should negate the need to ignore zmq imports.
fspv commentedon Nov 3, 2021
I created a PR to remove the most of remaining
type: ignorecomments #23420Merging it will leave us with just a few suppressed places:
maflcko commentedon Nov 3, 2021
Anything left to do here that is a good first issue? If yes, the issue text should be updated or a new issue be filed (and this one closed).
fspv commentedon Nov 3, 2021
These 2 Windows-specific lines are easy to fix
https://mypy.readthedocs.io/en/stable/common_issues.html#python-version-and-system-platform-checks
After that it can be closed, I believe, as only untyped libs (lief and jinja2) are left.
UPD: for the ignored imports the description suggests generating stub files, so the issue can't be closed. But needs to be updated anyway.
maflcko commentedon Nov 3, 2021
If someone wants to fix those, seems fine to just create a pull. Though, I think there is no risk if they are left unfixed. It is not like color formatting in the test framework output is any critical feature.
I am not sure if we want to maintain stub files for third party python packages that we use. Is the effort going to be worth the additional checks?
jinja2is only used to print logs, a feature that is know to be working and unlikely to break.liefI am not sure. An alternative would be to just wait for the upstream project to add the annotations and do nothing here?fspv commentedon Nov 3, 2021
Agree, no risks at all. Just a low-hanging fruit for those looking for the "good first issue".
Agree with this as well. The description should be updated to reflect this though.
maflcko commentedon Nov 3, 2021
Ok, I am just going ahead and closing this. Maybe a new issue can be started if people want to discuss whether we want to maintain stub files for 3rd party packages.
Merge bitcoin/bitcoin#23420: test: Correct MyPy typing for subtest de…