-
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
incorrectly-parenthesized-tuple-in-subscript
(RUF031
) - false positives with parenthesize-tuple-in-subscript = true
on types outside of type annotations
#13065
Comments
Neat! This is tricky. Ignoring subscripts followed by calls would lead to false negatives for folks using a dispatch pattern: dispatch = {(0,1): north, (1,0): east, (0,-1): south, (-1,0): west }
def walk(x,y,z):
return dispatch[x,y](z) Maybe there is some sneakier way of teasing out this generic type situation... |
Yeah, I also think this is tricky without type inference. @DetachHead I'm curious to know how did you run into this, do you have an example of a real world code base? |
yeah there are about a hundred false positives in our codebase after enabling this rule. i haven't looked through all of them but it seems that most of them fall into one of the following scenarios:
all the examples i found use stdlib types so i guess in these cases they can be special-cased to reduce the risk of these false positives |
Interesting, thanks for sharing that. cc @AlexWaygood do you think it's currently feasible to resolve this? |
incorrectly-parenthesized-tuple-in-subscript
(RUF031
) with parenthesize-tuple-in-subscript = true
false positives on types outside of type annotationsincorrectly-parenthesized-tuple-in-subscript
(RUF031
) - false positives with parenthesize-tuple-in-subscript = true
on types outside of type annotations
Hrm, this is hard! For your three bullets in #13065 (comment): bullets (1) and (2) would be easy with type inference; we could just check to see if the subscripted object was a type that had Bullet point 3 (detecting whether an assignment not annotated using I can see several options, none perfect:
|
Hi, I can chime in with another data point.
|
Maybe in the short term I can try to disable the rule for calls on builtin types like dict and tuple since I think there is some support for that. I also think it's worth the potential false negative to disable the rule for "Literal" since I doubt many people are shadowing that name and subscripting with it - plus But I'll wait for the green light from the maintainers. |
hmm, is that true? Type checkers like mypy just read the AST, and |
TIL! And pyright appears to enforce that rule, too. Thanks! |
the fix for #12758 only fixed the issue for type annotations, but the generic syntax for types can appear outside of type annotations, eg:
playground
i'm not sure how feasible it is to fix this without type checking (#3893), perhaps it could just assume any index access followed by a call is using a type
The text was updated successfully, but these errors were encountered: