PY-84107 Fix Type Parameters mistakenly reported as unused in PyUnusedLocalInspection

GitOrigin-RevId: 0593c2290aa1c84dde96673297374efef2b27fc3
This commit is contained in:
Daniil Kalinin
2025-10-23 22:48:43 +00:00
committed by intellij-monorepo-bot
parent 6639fe14c3
commit 931cf5c4e1
3 changed files with 25 additions and 0 deletions
@@ -257,6 +257,13 @@ public final class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor {
if (declOwner != null && declOwner != owner) {
readsFromInstruction.addAll(ScopeUtil.getElementsOfAccessType(name, declOwner, ReadWriteInstruction.ACCESS.WRITE));
}
// Type params are not present in CFG
else if (owner instanceof PyTypeParameterListOwner typeParameterListOwner &&
typeParameterListOwner.getTypeParameterList() != null) {
StreamEx.of(typeParameterListOwner.getTypeParameterList().getTypeParameters())
.filter(typeParameter -> name.equals(typeParameter.getName()))
.forEach(readsFromInstruction::add);
}
}
ControlFlowUtil.iteratePrev(startInstruction, instructions, inst -> {
final PsiElement instElement = inst.getElement();
@@ -0,0 +1,13 @@
from typing import Callable
def wait_for_condition[T](fn: Callable[..., T]) -> T:
return fn()
def foo[T, U](fn: Callable[..., T]) -> U:
return fn()
def bar[T, <weak_warning descr="Type parameter 'U' is not used">U</weak_warning>](fn: Callable[..., T]) -> T:
return fn()
def baz[<weak_warning descr="Type parameter 'T' is not used">T</weak_warning>, <weak_warning descr="Type parameter 'U' is not used">U</weak_warning>](fn: Callable[..., int]) -> str:
return fn()
@@ -247,6 +247,11 @@ def test():
doTest();
}
// PY-84107
public void testNewStyleTypeParameterInFuncNotMarkedAsUnused() {
doTest();
}
@NotNull
@Override
protected Class<? extends PyInspection> getInspectionClass() {