-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Fixed stubgen parsing generics from C extensions #8939
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.
Thanks! Looks reasonable, but I'd love to see some tests for this, as otherwise a future contributor might accidentally break something.
r'(^|[\[, ]+)' + re.escape(module.__name__ + '.'), | ||
r'\1', | ||
typ, | ||
) |
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.
It would be nice to have some unit tests for the new functionality, since this logic is non-trivial. You can add them to mypy/test/teststubgen.py
.
(Maybe also modify this so that the module name is passed instead of a module object.)
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.
Ah I couldn't find the tests for stubgen. I'll get those added. Thanks for the review!
0305fab
to
64ebff3
Compare
64ebff3
to
cf31345
Compare
cf31345
to
0c3a7bd
Compare
Is there anything else that I can do to help progress this pull request? |
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 writing tests! This is good to merge now. The regexp based parsing looks a bit fragile, so having tests is important.
pybind11 is capable of producing type signatures that use generics (for example https://github.com/pybind/pybind11/blob/4e3d9fea74ed50a042d98f68fa35a3133482289b/include/pybind11/stl.h#L140). A user may also opt to write a signature in the docstring that uses generics.
Currently when stubgen parses one of these generics, it attempts to import a part of it. For example if a docstring had
my_func(str, int) -> List[mypackage.module_being_parsed.MyClass]
, the resulting stub file tries to importList[mypackage.module_being_parsed
.This change fixes this behaviour by breaking the found type down into the multiple types around
[],
characters, adding any imports from those types that are needed, and then stripping out the name of the module being parsed.