From 539ba37e9e87f664cbaab0965a71c086adc7f158 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Thu, 26 Sep 2013 19:39:00 +0400 Subject: [PATCH] If we cannot infer the type of '__new__', return a weak class type (PY-10893) --- .../jetbrains/python/psi/impl/PyCallExpressionHelper.java | 6 ++++++ .../CustomNewReturnInAnotherModule/a.py | 4 ++++ .../CustomNewReturnInAnotherModule/b.py | 8 ++++++++ .../inspections/PyUnresolvedReferencesInspectionTest.java | 5 +++++ 4 files changed, 23 insertions(+) create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/CustomNewReturnInAnotherModule/a.py create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/CustomNewReturnInAnotherModule/b.py diff --git a/python/src/com/jetbrains/python/psi/impl/PyCallExpressionHelper.java b/python/src/com/jetbrains/python/psi/impl/PyCallExpressionHelper.java index ff563cd79988..1f3d65c93fa5 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyCallExpressionHelper.java +++ b/python/src/com/jetbrains/python/psi/impl/PyCallExpressionHelper.java @@ -408,6 +408,12 @@ public class PyCallExpressionHelper { if (t != null && !(t instanceof PyNoneType)) { return t; } + if (cls != null && t == null) { + final PyFunction newMethod = cls.findMethodByName(PyNames.NEW, true); + if (newMethod != null && !PyBuiltinCache.getInstance(call).hasInBuiltins(newMethod)) { + return PyUnionType.createWeakType(new PyClassTypeImpl(cls, false)); + } + } } if (cls != null) { return new PyClassTypeImpl(cls, false); diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/CustomNewReturnInAnotherModule/a.py b/python/testData/inspections/PyUnresolvedReferencesInspection/CustomNewReturnInAnotherModule/a.py new file mode 100644 index 000000000000..5e6d4a745397 --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/CustomNewReturnInAnotherModule/a.py @@ -0,0 +1,4 @@ +from b import C + +c = C() +c.foo() diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/CustomNewReturnInAnotherModule/b.py b/python/testData/inspections/PyUnresolvedReferencesInspection/CustomNewReturnInAnotherModule/b.py new file mode 100644 index 000000000000..16af06a7831e --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/CustomNewReturnInAnotherModule/b.py @@ -0,0 +1,8 @@ +class C(object): + def __new__(cls): + return D() + + +class D(object): + def foo(self): + pass diff --git a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java index 363d4c0c7242..771c4426fe84 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java @@ -298,6 +298,11 @@ public class PyUnresolvedReferencesInspectionTest extends PyTestCase { doMultiFileTest("a.py"); } + // PY-10893 + public void testCustomNewReturnInAnotherModule() { + doMultiFileTest("a.py"); + } + private void doTest() { myFixture.configureByFile(TEST_DIRECTORY + getTestName(true) + ".py"); myFixture.enableInspections(PyUnresolvedReferencesInspection.class);