-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
RUF031
: Ignore unparenthesized tuples in subscripts when the subscript is obviously a type annotation or type alias
#12762
Conversation
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
RUF031 | 1 | 0 | 1 | 0 | 0 |
Oh, that's ugly. It sorta feels like a shame that we'll no longer be emitting a diagnostic there 😆 I suppose we could only ignore the rule for type annotations/aliases if |
Does it make sense to say in the docstring for the rule that we skip annotations, or is that too in the weeds? |
That's a good idea. I'll add some more docs tomorrow (it's late now where I am :-) |
@@ -64,6 +64,10 @@ pub(crate) fn subscript_with_parenthesized_tuple(checker: &mut Checker, subscrip | |||
if tuple_subscript.parenthesized == prefer_parentheses || tuple_subscript.elts.is_empty() { | |||
return; | |||
} | |||
let semantic = checker.semantic(); | |||
if semantic.in_annotation() || semantic.in_type_definition() { |
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.
Should we also do semantic.in_simple_string_type_definition()
here?
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.
It's not needed -- if the SIMPLE_STRING_TYPE_DEFINITION
flag has been set on the semantic model, the TYPE_DEFINITION
flag will also have been set:
ruff/crates/ruff_linter/src/checkers/ast/mod.rs
Lines 2196 to 2204 in c4e6519
let type_definition_flag = match kind { | |
AnnotationKind::Simple => SemanticModelFlags::SIMPLE_STRING_TYPE_DEFINITION, | |
AnnotationKind::Complex => { | |
SemanticModelFlags::COMPLEX_STRING_TYPE_DEFINITION | |
} | |
}; | |
self.semantic.flags |= | |
SemanticModelFlags::TYPE_DEFINITION | type_definition_flag; |
I think it might make sense to cover generics as well (#12773 (comment)) like: class Foo(Generic[T1, T2]):
... (It can be done as a follow-up.) |
I find it hard to following this reasoning but it's probably just me not writing enough python. Isn't this as "silly" as doing the same in regular subscripts? |
…ipt is obviously a type annotation or type alias
ec02b47
to
0ffde3a
Compare
Type annotations just have their own specific conventions around them. And the subscription has a very different meaning in a typing context. You're not "looking up" a value at an index or key in a typing context in the same way; and I think the vast majority of people aren't even aware that they're creating implicit tuples in a type annotation context. |
Summary
Fixes #12758. Previously with the setting
lint.ruff.parenthesize-tuple-in-subscript = true
, RUF031 would have complained about annotations such asdict[str, int]
, telling you that you should spell them asdict[(str, int)]
instead. That feels somewhat silly -- although it is of course a subscription in exactly the same way as a dictionary key lookup, it's not really what the rule is trying to detect.This PR fixes most of those cases. Unfortunately it doesn't fix cases like this, where it's ambiguous whether the variable is a type alias or not:
Faced with this, however, users have a fairly simple workaround: make it explicit that it's a type alias, and the false positive will go away
Test Plan
cargo test -p ruff_linter --lib