PY-76855 Do not report ambiguous access to a generic instance if the type parameter has a default

GitOrigin-RevId: ce6c1cb85beac3a326dd87c4477be8893d99e802
This commit is contained in:
Daniil Kalinin
2025-07-16 12:53:11 +00:00
committed by intellij-monorepo-bot
parent c2b72108a3
commit e52455d022
2 changed files with 16 additions and 1 deletions
@@ -267,7 +267,9 @@ public class PyTypeCheckerInspection extends PyInspection {
}
private static boolean requiresTypeSpecialization(@Nullable PyType type) {
if (type instanceof PyTypeParameterType && !(type instanceof PySelfType)) return true;
if (type instanceof PyTypeParameterType typeParameterType &&
typeParameterType.getDefaultType() == null &&
!(type instanceof PySelfType)) return true;
return type instanceof PyCollectionType collectionType &&
exists(collectionType.getElementTypes(), Visitor::requiresTypeSpecialization);
}
@@ -2994,4 +2994,17 @@ def foo(param: str | int) -> TypeGuard[str]:
v5: Hashable = DC5(0)
""");
}
// PY-76855
public void testAccessToAttributeOfGenericClassWithDefaultIsNotAmbiguous() {
doTestByText("""
class Test1[T = int]():
attr: T
class Test2[T]():
attr: T
Test1.attr #OK
Test2.<warning descr="Access to generic instance variables via class is ambiguous">attr</warning>
""");
}
}