-
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
] Improve autofix for nested and mixed type unions unnecessary-type-union
(PYI055
)
#14272
[flake8-pyi
] Improve autofix for nested and mixed type unions unnecessary-type-union
(PYI055
)
#14272
Conversation
…cessary-type-union` (`PYI055`)
|
flake8-pyi
] Implement autofix and handle nested unions with single element (PYI041
, PYI055
)
#14214
crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs
Outdated
Show resolved
Hide resolved
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.
Nice.
The same as for other fixes. I think we should not offer the fix if the range contains comments instead of marking the fix as unsafe.
crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs
Outdated
Show resolved
Hide resolved
))); | ||
} | ||
// Mark [`Fix`] as unsafe when comments are in range. | ||
let applicability = if checker.comment_ranges().intersects(union.range()) { |
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.
Hmm, I looked through all the intersects
usages and I only found one exception where we set Applicability::Unsafe
if the node contains comments. All other usages don't offer a fix instead. I think we should do the same here.
Could you take a look if we should use has_comments
instead?
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.
has_comments
is just wrapping intersects
that includes leading and trailing content.
For type annotations I cannot come up with a case where the fix is changing the leading/trailing content:
a: ( # comment 1
- type[int] | type[float] ) = 3
+ type[int | float] ) = 3
I'm not absolutely sure though.
Summary
This PR improves the fix for
PYI055
to be able to handle nested and mixed type unions.It also marks the fix as unsafe when comments are present.
Test Plan