-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[pyupgrade
] Avoid PEP-604 unions with typing.NamedTuple
(UP007
, UP045
)
#18682
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
Conversation
687cd54
to
ca36006
Compare
|
pyupgrade
] Fix UP007
: ignore Optional[NamedTuple]
pyupgrade
] Fix UP045
: ignore Optional[NamedTuple]
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 think this looks good for UP045. However, we have the same problem for UP007, which is actually the rule from the initial report. Maybe my earlier comments were a bit confusing, but we need to handle both UP045, which replaces Optional[NamedTuple]
with NamedTuple | None
and UP007, which replaces Union[NamedTuple, int]
(for example) with NamedTuple | int
, which is also an error.
// `NamedTuple` is not a type; it's a type constructor. Using it in a type annotation doesn't | ||
// make much sense. But since type checkers will currently (incorrectly) _not_ complain about it | ||
// being used in a type annotation, we just ignore `Optional[typing.NamedTuple]`. | ||
// https://github.com/astral-sh/ruff/issues/18619 |
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.
nit: I think this might make sense as the docstring for is_optional_named_tuple
instead of here in the middle of the code.
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!
pyupgrade
] Fix UP045
: ignore Optional[NamedTuple]
pyupgrade
] Avoid PEP-604 unions with typing.NamedTuple
(UP007
, UP045
)
… `UP045`) (astral-sh#18682) <!-- Thank you for contributing to Ruff/ty! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? (Please prefix with `[ty]` for ty pull requests.) - Does this pull request include references to any relevant issues? --> ## Summary Make `UP045` ignore `Optional[NamedTuple]` as `NamedTuple` is a function (not a proper type). Rewriting it to `NamedTuple | None` breaks at runtime. While type checkers currently accept `NamedTuple` as a type, they arguably shouldn't. Therefore, we outright ignore it and don't touch or lint on it. For a more detailed discussion, see the linked issue. ## Test Plan Added examples to the existing tests. ## Related Issues Fixes: astral-sh#18619
Summary
Make
UP045
ignoreOptional[NamedTuple]
asNamedTuple
is a function (not a proper type). Rewriting it toNamedTuple | None
breaks at runtime. While type checkers currently acceptNamedTuple
as a type, they arguably shouldn't. Therefore, we outright ignore it and don't touch or lint on it.For a more detailed discussion, see the linked issue.
Test Plan
Added examples to the existing tests.
Related Issues
Fixes: #18619