From 8ac22cc853098c8a64e295b0bb215d3736ab06b6 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Tue, 29 Jan 2013 18:35:21 +0400 Subject: [PATCH] Fixed callable inspection for return values of getattr() (PY-7625) --- python/helpers/StdlibTypes2.properties | 8 ++++---- .../com/jetbrains/python/psi/types/PyTypeChecker.java | 11 ++++++----- .../PyCallingNonCallableInspection/getattrCallable.py | 7 +++++++ .../PyCallingNonCallableInspectionTest.java | 5 +++++ 4 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 python/testData/inspections/PyCallingNonCallableInspection/getattrCallable.py diff --git a/python/helpers/StdlibTypes2.properties b/python/helpers/StdlibTypes2.properties index d1083948e6d4..48f494547c81 100644 --- a/python/helpers/StdlibTypes2.properties +++ b/python/helpers/StdlibTypes2.properties @@ -62,7 +62,7 @@ __builtin__.filter.3 = \ __builtin__.getattr = \ :type name: string \n\ - :rtype: unknown \n\ + :rtype: object or unknown \n\ __builtin__.globals = \ :rtype: dict of (bytes, object) @@ -129,7 +129,7 @@ __builtin__.reduce = \ :type function: collections.Callable \n\ :type sequence: collections.Iterable \n\ :type initial: object or None \n\ - :rtype: unknown \n\ + :rtype: object or unknown \n\ __builtin__.round = \ :type number: int or long or float \n\ @@ -1073,7 +1073,7 @@ __builtin__.tuple.__rmul__ = \ __builtin__.tuple.__getitem__ = \ :type y: int \n\ - :rtype: unknown \n\ + :rtype: object or unknown \n\ ## 5.8 Mapping types @@ -2877,7 +2877,7 @@ subprocess.Popen.send_signal = \ json.loads = \ :type s: string \n\ :type encoding: string \n\ - :rtype: unknown \n\ + :rtype: object or unknown \n\ ## 18.12. base64 diff --git a/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java b/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java index 648f655e64e2..e880d580f9bb 100644 --- a/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java +++ b/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java @@ -438,16 +438,17 @@ public class PyTypeChecker { } } else if (type instanceof PyUnionType) { + Boolean result = true; for (PyType member : ((PyUnionType)type).getMembers()) { - final Boolean result = isCallable(member); - if (result == null) { + final Boolean callable = isCallable(member); + if (callable == null) { return null; } - else if (!result) { - return false; + else if (!callable) { + result = false; } } - return true; + return result; } else if (type instanceof PyCallableType) { return true; diff --git a/python/testData/inspections/PyCallingNonCallableInspection/getattrCallable.py b/python/testData/inspections/PyCallingNonCallableInspection/getattrCallable.py new file mode 100644 index 000000000000..39dd0a70afab --- /dev/null +++ b/python/testData/inspections/PyCallingNonCallableInspection/getattrCallable.py @@ -0,0 +1,7 @@ +class value(): + pass + +class MyClass(object): + def bar(self): + foo = getattr(self, 'foo') + foo() diff --git a/python/testSrc/com/jetbrains/python/inspections/PyCallingNonCallableInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyCallingNonCallableInspectionTest.java index 3c2cf2a1cf5e..dde5f89679d7 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyCallingNonCallableInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyCallingNonCallableInspectionTest.java @@ -64,6 +64,11 @@ public class PyCallingNonCallableInspectionTest extends PyTestCase { doTest(); } + // PY-8182 + public void testGetattrCallable() { + doTest(); + } + private void doTest() { setLanguageLevel(LanguageLevel.PYTHON27); try {