Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Implement PYI030: Unnecessary literal union #5570
Implement PYI030: Unnecessary literal union #5570
Changes from 12 commits
7a05d80
fe2dd78
ca29457
d707704
56f7d09
9c7194a
e088b34
8b9d54c
7c605c3
40e4fe3
1759d91
19f9cbf
8b068ad
becf391
80d2269
47d8983
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
We may want to add these (
&& self.semantic.in_type_definition()
) to theRule::UnnecessaryLiteralUnion
condition too. I can't quite remember why they're necessary here. I'm guessing it's because for|
, if it's used outside of a type definition, it's not a union operator (it's a bitwise or, I think).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.
That's true for the
DuplicateUnionMember
rule where they're checking for any duplicate items in a|
but here we know we are constrained to literal types where this does apply.We test for this at
https://github.com/charliermarsh/ruff/blob/1759d914456b62dfc479518ce8717949935f6c87/crates/ruff/resources/test/fixtures/flake8_pyi/PYI030.pyi#L28
You can see that a bitwise or of literals resolve to a
Union
outside of an annotation: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.
I guess it's possible that someone has
Literal[1] | bar | Literal[2]
wherebar
implements some sort of custom bitwise or behavior and this rule no longer holds but I'm not sure that's worth handlingThere 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.
Interesting, that makes sense. Thanks for the thorough follow-up!
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.
In a follow-up, we can probably use this in PYI016 (#3922) and fix support for
typing.Union
there