-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
stubgen: Gracefully handle invalid Optional
and recognize aliases to PEP 604 unions
#17386
Conversation
This comment has been minimized.
This comment has been minimized.
Hmm not sure about this. I lean towards saying we should refuse the temptation to guess if we're given an invalid annotation. I would probably print an error to the terminal noting that we encountered an invalid annotation on line X, fall back to using |
This comment has been minimized.
This comment has been minimized.
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! Do we have any tests for e.g. Optional[str | int]
?
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.
LGTM. It looks like stubgen already does the right thing here, but you could also test exotic things like Optional[type[int | str]]
, for completeness
Optional
usage in stubgenOptional
and recognize aliases to PEP 604 unions
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This Fixes 2 issues with invalid
Optional
(inspired by an error reported in #17197):Optional
Optional
with more than one index as an unknown type instead of choosing the first type.It also fixes PEP 604 unions not being recognized as type aliases.