[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
MichaReiser
left a comment
There was a problem hiding this comment.
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.
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.
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] ) = 3I'm not absolutely sure though.
Summary
This PR improves the fix for
PYI055to be able to handle nested and mixed type unions.It also marks the fix as unsafe when comments are present.
Test Plan