diff --git a/python/python-psi-impl/src/com/jetbrains/python/inspections/unusedLocal/PyUnusedLocalInspectionVisitor.java b/python/python-psi-impl/src/com/jetbrains/python/inspections/unusedLocal/PyUnusedLocalInspectionVisitor.java
index 782ed05a3893..542a71cda9f3 100644
--- a/python/python-psi-impl/src/com/jetbrains/python/inspections/unusedLocal/PyUnusedLocalInspectionVisitor.java
+++ b/python/python-psi-impl/src/com/jetbrains/python/inspections/unusedLocal/PyUnusedLocalInspectionVisitor.java
@@ -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();
diff --git a/python/testData/inspections/PyUnusedLocalInspection/newStyleTypeParameterInFuncNotMarkedAsUnused.py b/python/testData/inspections/PyUnusedLocalInspection/newStyleTypeParameterInFuncNotMarkedAsUnused.py
new file mode 100644
index 000000000000..22df2b95dde5
--- /dev/null
+++ b/python/testData/inspections/PyUnusedLocalInspection/newStyleTypeParameterInFuncNotMarkedAsUnused.py
@@ -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, U](fn: Callable[..., T]) -> T:
+ return fn()
+
+def baz[T, U](fn: Callable[..., int]) -> str:
+ return fn()
\ No newline at end of file
diff --git a/python/testSrc/com/jetbrains/python/inspections/PyUnusedLocalInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyUnusedLocalInspectionTest.java
index 20a318d37d21..d2ab4bfa8d0f 100644
--- a/python/testSrc/com/jetbrains/python/inspections/PyUnusedLocalInspectionTest.java
+++ b/python/testSrc/com/jetbrains/python/inspections/PyUnusedLocalInspectionTest.java
@@ -247,6 +247,11 @@ def test():
doTest();
}
+ // PY-84107
+ public void testNewStyleTypeParameterInFuncNotMarkedAsUnused() {
+ doTest();
+ }
+
@NotNull
@Override
protected Class extends PyInspection> getInspectionClass() {