-
-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only typecheck immediate sources, not transitive #12
Conversation
Yes the
I'm not sure this is true, but I'm not an expert on MyPy's behaviour or underlying workings. The docs say this:
It seems like you might want a target's Dropbox's more advanced and complicated MyPy Aspect appears to pass all transitive srcs though. They also propagate along |
if not hasattr(base_rule.attr, "srcs"): | ||
return None | ||
direct_src_files = _extract_srcs(base_rule.attr.srcs) | ||
direct_src_root_paths = sets.to_list( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing this looks right. My single --package-root
looks hacky and wrong, but it wasn't breaking things from what I can see. Did you encounter problems with --package-root .
?
Why not also transitive srcs
as --package-roots
too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reviewing.
I encountered the package root issue trying to depend on the Google protobuf python target using @com_google_protobuf//:protobuf_python (similar to #11). However, after fixing the source roots, I found another issue: that target includes __init__.py's under compatibility_test/v2.5.0, which is not a valid Python module name, and since the mypy invocation includes all transitive sources, mypy fails on the bad module name. I ended up switching to depending on PyPI's protobuf, since that's packaged correctly (I think @com_google_protobuf//:protobuf_python is broken, in this case).
Now, after switching to protobuf from PyPI, I have a new issue: MYPYPATH isn't populated with imports from transitive dependencies. This causes mypy to emit tons of type errors for the protobuf library because mypy sees a different import path for the external dependency (something like external.pypi__...__protobuf.google.protobuf
), so it fails to match it with typeshed's definitions. I haven't figured out how to fix this yet—let me know if you see an obvious solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like more of a mypy issue? The default mypi.ini
is rather aggressive, if you turn off imports using
[mypy]
follow_imports=skip
You might see a solution to your problem. A less shotgun approach is to use # type: ignore
. I just took a look at my code, which works with #11 and I have:
from google.protobuf import text_format # type: ignore
In my mypy_test files.
Sorry I haven't taken the time to review this. I've been just using the tests not the aspect, so unless you make those changes- I don't have much to input.
But I'd be excited for a canonical solution that supports protobuf 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this also work as an alternative to follow_imports=skip
?
[mypy-protobuf.*]
ignore_missing_imports = True
I've created https://github.com/thundergolfer/bazel-python-mypy-protobuf as a repro of #15. Does it also work as a repro of the problems you're experiencing with generated code? |
@thundergolfer Awesome! I was planning to do something similar. I'll apply my patch and see if it works. This PR has some nice changes I might try to incorporate as well. However, I may not get around to it until the weekend. |
Hi @thundergolfer,
I found this repository after reading python/mypy#3912 (comment) and I'm also interested in typechecking Bazel-built Python code. I think I've run into a similar issue as @dmadisetti (with generated protobuf code), but I have a question and/or a different approach than #11.
If I understand Bazel aspects correctly (I'm not an expert), if I have
py_library
targets//py:b
which depends on//py:a
, and I build//py:b
, Bazel will invoke the mypy aspect for both, so it's not necessary to check:a
's sources while checking:b
s. This PR modifies the mypy aspect to do that. I tested in an internal codebase and found that typecheck errors were still caught in transitive dependencies with this change.This PR is incomplete because it breaks
mypy_test
, but if this approach seems reasonable to you, I'll go ahead and fix it so it's mergeable.Thanks.