From d5204ab32b8335ca1cdc573e2f36aaee1f931443 Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Fri, 24 Mar 2017 14:53:25 +0300 Subject: [PATCH] PY-23164 Fixed: unittest unresolved attribute reference in setUp inside with statement Don't skip with-statement children while collecting target expressions inside function. --- python/src/com/jetbrains/python/psi/impl/PyClassImpl.java | 2 ++ .../instanceAttributeCreatedInsideWithStatement.py | 8 ++++++++ .../inspections/PyUnresolvedReferencesInspectionTest.java | 5 +++++ 3 files changed, 15 insertions(+) create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/instanceAttributeCreatedInsideWithStatement.py diff --git a/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java b/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java index 561feda63191..27db8b6fe64b 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java @@ -1222,6 +1222,8 @@ public class PyClassImpl extends PyBaseElementImpl implements PyCla .map(PyWithItem::getTarget) .select(PyTargetExpression.class) .forEach(result::add); + + super.visitPyWithStatement(node); } }); return result; diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/instanceAttributeCreatedInsideWithStatement.py b/python/testData/inspections/PyUnresolvedReferencesInspection/instanceAttributeCreatedInsideWithStatement.py new file mode 100644 index 000000000000..fd25530a1870 --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/instanceAttributeCreatedInsideWithStatement.py @@ -0,0 +1,8 @@ +class Foo(object): + def __init__(self): + with open('b.py'): + self.scope = "a" + pass + + def get_scope(self): + return self.scope \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java index 807e879d85ef..0b4da8a34b8e 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java @@ -758,6 +758,11 @@ public class PyUnresolvedReferencesInspectionTest extends PyInspectionTestCase { assertNotParsed((PyFile)fooPsiFile); } + // PY-23164 + public void testInstanceAttributeCreatedInsideWithStatement() { + doTest(); + } + @NotNull @Override protected Class getInspectionClass() {