[flake8-pyi] Expand PYI018 to cover ParamSpecs and TypeVarTuples#9198
[flake8-pyi] Expand PYI018 to cover ParamSpecs and TypeVarTuples#9198charliermarsh merged 4 commits intoastral-sh:mainfrom
Conversation
|
| _ => { | ||
| continue; | ||
| } | ||
| }; |
There was a problem hiding this comment.
I might write this like:
let typevarlike_kind = if semantic.match_typing_expr(f, "TypeVar") {
"TypeVar"
} else if ...But I don't know that what I'm proposing is actually better, I just hadn't seen this pattern before :)
| Some("TypeVar") | ||
| } else if semantic.match_typing_call_path(&call_path, "ParamSpec") { | ||
| Some("ParamSpec") | ||
| } else if semantic.match_typing_call_path(&call_path, "TypeVarTuple") { |
There was a problem hiding this comment.
@AlexWaygood - I made one change here per your comment in the summary. Each call to semantic.match_typing_expr does a lookup internally to map the expression to a "call path", like ["typing", "TypeVar"]. However, we can just do that lookup once via semantic.resolve_call_path, then pass the call path to semantic.match_typing_call_path. (semantic.match_typing_expr is just a wrapper around this process, so if we want to do multiple checks, it's more efficient to do the lookup outside of the semantic.match_typing_expr.)
There was a problem hiding this comment.
Ahh nice, thanks! Yep, that sounds like it's exactly what I was looking for :)
Summary
Part of #8771. flake8-pyi will emit a Y018 error for unused TypeVars, ParamSpecs or TypeVarTuples; Ruff currently only emits PYI018 for unused TypeVars.
This is my first "proper" Ruff PR -- let me know if there's a better way of doing this! Not sure if the repeated calls to
match_typing_expr()are ideal.Test Plan
I manually updated the fixtures to add some unused ParamSpecs and TypeVarTuples, and then updated the snapshots using
cargo insta review. All tests then passed when run usingcargo test.