[flake8-pyi] Extend fix to Python <= 3.9 for redundant-none-literal (PYI061) - #16044
Merged
Conversation
In case the version is <=3.9 and we are _not_ in a stub file, the rule behavior changes as follows: - The diagnostic messages reference `typing.Union` instead of `|` - The offered fix first imports `typing.Union` and then uses it In order to achieve this we had to make the following changes: - `create_fix_edit` is now `create_fix` and returns a `Fix` instead of an edit. That's because importing `Union` and using it is _two_ edits, so we have to combine them into a `Fix` before returning. - We use the enum `UnionKind` to determine one of three possible diagnostic messages and fix behavior, rather than the previous `bool` of `other_literal_elements_seen`. Finally, in order to uniformly handle building union expressions throughout the codebase, we use the helpers `pep_604_union` and `typing_union` rather to create the appropriate union expression, rather than manually building this AST node.
Contributor
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| PYI061 | 30 | 15 | 15 | 0 | 0 |
Collaborator
Author
|
Ecosystem changes as expected - both repos have |
Member
Hmmm... wouldn't it be better to suggest |
AlexWaygood
reviewed
Feb 8, 2025
Collaborator
Author
🤦 of course! much better |
AlexWaygood
approved these changes
Feb 9, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR extends the fix offered for redundant-none-literal (PYI061) to include Python versions <= 3.9 by using
typing.Optionalinstead of the operator|. We also offer the fix with|for any target version on stub files.Closes #15795
For review:
|since we will not remove them if they are already present, e.g.Literal[None] | intsimplifies toNone | intregardless of Python version or source type.