From 7c2b0794815444f55c60635d5592796b558b6029 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Tue, 14 Aug 2018 22:58:03 +0300 Subject: [PATCH] PY-24161 Properly treat variables annotated with Type[...] as callable --- .../jetbrains/python/psi/types/PyTypeChecker.java | 3 +++ .../explicitClassObjectTypeAnnotation.py | 10 ++++++++++ .../genericClassObjectTypeAnnotation.py | 13 +++++++++++++ .../PyCallingNonCallableInspectionTest.java | 10 ++++++++++ 4 files changed, 36 insertions(+) create mode 100644 python/testData/inspections/PyCallingNonCallableInspection/explicitClassObjectTypeAnnotation.py create mode 100644 python/testData/inspections/PyCallingNonCallableInspection/genericClassObjectTypeAnnotation.py diff --git a/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java b/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java index e181ce5cb582..70b2bcd91259 100644 --- a/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java +++ b/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java @@ -860,6 +860,9 @@ public class PyTypeChecker { if (type instanceof PyStructuralType && ((PyStructuralType)type).isInferredFromUsages()) { return true; } + if (type instanceof PyGenericType) { + return ((PyGenericType)type).isDefinition(); + } return false; } diff --git a/python/testData/inspections/PyCallingNonCallableInspection/explicitClassObjectTypeAnnotation.py b/python/testData/inspections/PyCallingNonCallableInspection/explicitClassObjectTypeAnnotation.py new file mode 100644 index 000000000000..23be3996c820 --- /dev/null +++ b/python/testData/inspections/PyCallingNonCallableInspection/explicitClassObjectTypeAnnotation.py @@ -0,0 +1,10 @@ +from typing import Type + + +class MyClass(object): + pass + + +def func(param): + # type: (Type[MyClass]) -> MyClass + param() diff --git a/python/testData/inspections/PyCallingNonCallableInspection/genericClassObjectTypeAnnotation.py b/python/testData/inspections/PyCallingNonCallableInspection/genericClassObjectTypeAnnotation.py new file mode 100644 index 000000000000..46013aa4b133 --- /dev/null +++ b/python/testData/inspections/PyCallingNonCallableInspection/genericClassObjectTypeAnnotation.py @@ -0,0 +1,13 @@ +from typing import TypeVar, Type + + +class User: + pass + + +U = TypeVar('U', bound=User) + + +def new_user(user_class): + # type: (Type[U]) -> U + return user_class() diff --git a/python/testSrc/com/jetbrains/python/inspections/PyCallingNonCallableInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyCallingNonCallableInspectionTest.java index a197ca87b0bb..0a08bba14c71 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyCallingNonCallableInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyCallingNonCallableInspectionTest.java @@ -113,6 +113,16 @@ public class PyCallingNonCallableInspectionTest extends PyInspectionTestCase { doMultiFileTest(); } + // PY-24161 + public void testGenericClassObjectTypeAnnotation() { + doTest(); + } + + // PY-24161 + public void testExplicitClassObjectTypeAnnotation() { + doTest(); + } + @NotNull @Override protected Class getInspectionClass() {