-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[flake8-pyi
] Extend fix to Python <= 3.9 for redundant-none-literal
(PYI061
)
#16044
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.
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
PYI061 | 30 | 15 | 15 | 0 | 0 |
Ecosystem changes as expected - both repos have |
Hmmm... wouldn't it be better to suggest |
crates/ruff_linter/src/rules/flake8_pyi/rules/redundant_none_literal.rs
Outdated
Show resolved
Hide resolved
🤦 of course! much better |
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.
Excellent, thank you!
This PR extends the fix offered for redundant-none-literal (PYI061) to include Python versions <= 3.9 by using
typing.Optional
instead 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] | int
simplifies toNone | int
regardless of Python version or source type.