Skip to content

Fix type hints for mypy 0.780+

Rohit Kothur requested to merge rkothur/fix-mypy into master

Description

This MR allows the mypy version to be unpinned in requirements.dev.in.

The gist of the issue is that protoc fails to generate stubs with the correct import paths. Several of our protos depend on other protos, and in generated python code, the import paths are missing buildgrid._protos as a prefix. This a problem both in the generated .py modules as well as the .pyi type stubs.

We only noticed this with mypy 0.780 because this version clamped down on adding directories with __init__.py files to the MYPYPATH, which was our previous workaroud to this problem for the pyi files. Since we no longer have that option, we have to fix the stubs themselves. (Though there is still some ongoing discussion of the issue.)

This thread has some further discussion of the protoc issue. The (terrible) workarounds are using sed to replace the relevant import paths (which is what is done here) or hacking the top-level proto path into the sys.path. There is some discussion in that issue of supporting a --python_import_root option, which would help us deal with the issue, and there is something similar being proposed in this PR.

I'm not sure if I updated requirements.dev.txt correctly. I had to delete it before rerunning manage-deps because it wasn't updating the mypy version. Please let me know if there is a more preferable way to do this.

Another solution that is more future-proof is to this protoc plugin mentioned in the above discussion thread that uses relative proto-to-proto imports, which would solve this problem.

Edited by Rohit Kothur

Merge request reports

Loading