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
Add support for gRPC mypy stub files #11658
Conversation
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.
This is awesome! Thank you so much @jyggen!
It will, of course, only work on mypy-protobuf 2.0+, and anyone using an older version will have a bad time. I'm not familiar enough with the Pants codebase to know if adding a version check to if downloaded_grpc_plugin: will be enough to prevent failure or if VenvPexRequest will crash and burn if a potentially invalid binary is referenced.
Hm, yeah, tricky. We would need to check if mypy-protobuf is 2+, but that's non-trivial because the requirement string could be something like mypy-protobuf>=1.0
. I don't know of a library to do this. We have code like this for Python versions:
pants/src/python/pants/backend/python/util_rules/pex.py
Lines 223 to 230 in f411f0d
def _includes_version(self, major_minor: str, last_patch: int) -> bool: | |
patch_versions = list(reversed(range(0, last_patch + 1))) | |
for req in self: | |
if any( | |
req.specifier.contains(f"{major_minor}.{p}") for p in patch_versions # type: ignore[attr-defined] | |
): | |
return True | |
return False |
But that's not very performant to iterate through every possible version and it's more complexity than we ideally want.
I think the intersection is small enough here that it's okay to break mypy-protobuf 1.x users; to be hit by this, you must have:
- enabled
[python-protobuf].mypy_plugin
- pinned
[python-protobuf].mypy_plugin_version
to 1.x - be using
grpc=True
in someprotobuf_library
targets
@stuhood @benjyw what do you think about the backwards compat? The concerning part is that there is no remedy if you're in that small audience other than bumping to 2.x for the plugin..we permanently break gRPC users who must stay on 1.x of the plugin.
--
@jyggen, would you be willing to please break out a precursor PR that simply bumps --mypy-plugin-version
to 2.4? It would be great for the changelog to have an entry "Bump default for [python-protobuf].mypy_plugin_version
to 2.4", given that they made API changes.
Thanks for the contribution @jyggen! Yeah the backwards compat question is tricky. I think we can mitigate by capturing the semver from the requirement string and just checking the major version. In the case of |
@Eric-Arellano Sure! Done in #11662. |
Yeah I think you're right that we should do this. And it's less painful than I expected, there are few 1.x releases for the plugin and they don't use patch versions, so we don't need to check a ton of versions to see if 1.x is in use: https://pypi.org/project/mypy-protobuf/#history @jyggen could you please add a check if any of See this example: pants/src/python/pants/backend/python/util_rules/pex.py Lines 223 to 230 in f411f0d
That is, first use
It probably makes sense to write that as a top-level function so that you can write a unit test, e.g. pants/src/python/pants/backend/python/util_rules/pex_test.py Lines 140 to 152 in e3b3136
|
Keep in mind it's simple to just check the actual version. 1st create a VenvPex with the 1script, then run |
True. @jyggen I'll put up a precursor PR to add a helper rule to facilitate this. Scratch my above recommendation about doing static analysis on the requirement string. |
Sorry to armchair, but I'll be AFK here in a few minutes. I wanted to make sure you all were also aware the parser needed for this is already in our dependency set: https://github.com/pypa/packaging/blob/53fd698b1620aca027324001bf53c8ffda0c17d1/packaging/utils.py#L88-L89 |
So what's the next step? Add an additional check so we only append the new flags when 2.x of the plugin is used ? I took a quick look at it earlier but couldn't find any (to me) obvious way to get the version of the plugin that ends up being installed, but if someone can point me in the right direction I could take a proper look at it. |
Thanks for checking in. John is about to land #11684, which will unblock me from landing #11665 so that we have a way to get information from what was actually used in the PEX, rather than relying on static string analysis. Then, you'll use that in this PR to implement your logic. I'll send some further thoughts once I land #11665, I'm still trying to see what the final API will look like. |
@jyggen thanks for your patience as we work this through. Should get it in within a few days! |
@benjyw No worries at all! Just wanted to check if the ball was in my court or not :) |
This is useful for querying what version of a dist is being used, such as is required by #11658. [ci skip-rust] [ci skip-build-wheels]
Okay, #11665 has landed: pants/src/python/pants/backend/python/util_rules/pex.py Lines 1018 to 1031 in 1902297
Start by reverting setting In that
Then, final step is to update the new test you added to check that if pants/src/python/pants/backend/codegen/protobuf/python/rules_integration_test.py Lines 43 to 52 in 0f27e41
|
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! @Eric-Arellano , sounds like you'll follow up on that TODO and a couple of others?
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.
@Eric-Arellano , sounds like you'll follow up on that TODO and a couple of others?
Thank you! Yes, I will get --pex-path
working to avoid the unnecessary Pex resolve, and will also address the two places I see code duplication if Jonas doesn't beat me to it.
This looks great! Thanks for the patch and for iterating on this.
src/python/pants/backend/codegen/protobuf/python/rules_integration_test.py
Outdated
Show resolved
Hide resolved
Thanks! The code duplication should be fixed. While I was at it I also updated the TODO comment to be a tad more accurate.. hopefully! |
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!
I've got a change here that will allow the pex_path cleanup in another way: #11693
Problem
Pants currently supports generating mypy stubs for the generated protobuf code, which makes mypy very happy. However, when type checking a codebase that has generated gRPC code as well, mypy throws quite the hissy fit due to the lack of gRPC stubs.
Solution
mypy-protobuf can (since version 2.0 at least) generate gRPC stubs as well, and it can be used in Pants with some minor changes. It will, of course, only work on mypy-protobuf 2.0+, and anyone using an older version will have a bad time. I'm not familiar enough with the Pants codebase to know if adding a version check to
if downloaded_grpc_plugin:
will be enough to prevent failure or ifVenvPexRequest
will crash and burn if a potentially invalid binary is referenced.Result
Stubs are now generated for the generated gRPC code as well! Awesome!