-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[flake8-pyi
] Expand Optional[A]
to A | None
(PYI016
)
#18572
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
Conversation
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
PYI016 | 2 | 2 | 0 | 0 | 0 |
Formatter (stable)
✅ ecosystem check detected no format changes.
Formatter (preview)
✅ ecosystem check detected no format changes.
0fb3f9f
to
0e6490a
Compare
Nice, the ecosystem hits are both true positives! |
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! This is looking good. I just had a few minor suggestions and a question about Optional[None]
.
// Avoid duplicate checks inside `Optional` | ||
&& !( | ||
optional_as_none_in_union_enabled(checker.settings) | ||
&& checker.semantic.inside_optional() |
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.
Could we move this check inside the rule itself so we don't have to duplicate it? This feels surprising to me, but I tried removing them locally and they're obviously needed somewhere to avoid duplicate diagnostics, as the comment says.
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 suppose we could. I was just trying to be consistent with what I saw in the code-base. Just a few lines above there's
// Avoid duplicate checks if the parent is a union, since these rules already
// traverse nested unions.
if !checker.semantic.in_nested_union() {
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.
Fair enough!
crates/ruff_linter/src/rules/flake8_pyi/rules/duplicate_union_member.rs
Outdated
Show resolved
Hide resolved
struct UnionTraversalOptions { | ||
traverse_optional: bool, | ||
} |
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 think I'm not as against passing bare bool
arguments as most people, but if you really want to avoid it, I think the more typical approach is defining a two-variant enum. Maybe something like this:
enum TraverseOptions {
Yes,
No,
}
As an even smaller nit here, I think we usually put the docs above the #[derive]
.
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 went with the struct as it's easier to extend should the need for more config options arise in the future. If you'd rather have it an enum, I'm also fine with that. I just find bare bool arguments hard to read at call sites, so I try to avoid them unless the function name gives a strong clue as to what the flag does.
Should I change it anyway?
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.
Are there more options you think might be needed? I guess I was assuming this would be the only one.
In any case, I don't feel too strongly about this, so we can just move ahead with the current version.
130 |-field39: typing.Optional[None] | ||
130 |+field39: None |
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 was a bit skeptical about this transformation in #18508 (comment) but if you and @AlexWaygood think it's fine, I'm happy with it. I just thought it was likely to be a separate mistake that a safe autofix could hide.
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.
This might become hard to track, but how about making the fix unsafe if both
- An
Optional
was visited None
is (one of) the redundant types in the union
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 fine if it would complicate the code, again I don't feel too strongly. And the change is in preview, so we can collect some feedback here.
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
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.
Thanks! Can you resolve the conflicts?
After that, I can merge after the patch release today.
struct UnionTraversalOptions { | ||
traverse_optional: bool, | ||
} |
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.
Are there more options you think might be needed? I guess I was assuming this would be the only one.
In any case, I don't feel too strongly about this, so we can just move ahead with the current version.
130 |-field39: typing.Optional[None] | ||
130 |+field39: None |
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 fine if it would complicate the code, again I don't feel too strongly. And the change is in preview, so we can collect some feedback here.
// Avoid duplicate checks inside `Optional` | ||
&& !( | ||
optional_as_none_in_union_enabled(checker.settings) | ||
&& checker.semantic.inside_optional() |
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.
Fair enough!
flake8_pyi
] Expand Optional[A]
to A | None
in duplicate-union-member (PYI016
)flake8_pyi
] Expand Optional[A]
to A | None
in duplicate-union-member
(PYI016
)
flake8_pyi
] Expand Optional[A]
to A | None
in duplicate-union-member
(PYI016
)flake8-pyi
] Expand Optional[A]
to A | None
(PYI016
)
Summary
Under preview 🧪 I've expanded rule
PYI016
to also flag type union duplicates containingNone
andOptional
.Test Plan
Examples/tests have been added. I've made sure that the existing examples did not change unless preview is enabled.
Relevant Issues
Optional[None]
toNone | None
#18508 (discussing introducing/extending a rule to flagOptional[None]
)