From 87cfacbb3f00b46fa1106841ad2ab897e9b42b90 Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Fri, 23 Mar 2018 19:37:51 +0300 Subject: [PATCH] Don't collect names in parameter default and annotation for visited function (PY-19599) Because they are collected in the outer scope and outer scope is considered as their owner in ScopeUtil.getScopeOwner. --- .../dataflow/scope/impl/ScopeImpl.java | 15 ++++++++++----- .../PyUnresolvedReferencesInspectionTest.java | 7 +++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/python/src/com/jetbrains/python/codeInsight/dataflow/scope/impl/ScopeImpl.java b/python/src/com/jetbrains/python/codeInsight/dataflow/scope/impl/ScopeImpl.java index 9702b007a23b..70d7f00a7a1b 100644 --- a/python/src/com/jetbrains/python/codeInsight/dataflow/scope/impl/ScopeImpl.java +++ b/python/src/com/jetbrains/python/codeInsight/dataflow/scope/impl/ScopeImpl.java @@ -236,6 +236,11 @@ public class ScopeImpl implements Scope { super.visitPyFunction(node); } + @Override + public void visitPyNamedParameter(PyNamedParameter node) { + processNamedElement(node); + } + @Override public void visitPyClass(PyClass node) { visitDecorators(node.getDecoratorList()); @@ -249,11 +254,7 @@ public class ScopeImpl implements Scope { @Override public void visitPyElement(PyElement node) { if (node instanceof PsiNamedElement && !(node instanceof PyKeywordArgument)) { - final String name = node.getName(); - if (!namedElements.containsKey(name)) { - namedElements.put(name, Sets.newLinkedHashSet()); - } - namedElements.get(name).add((PsiNamedElement)node); + processNamedElement((PsiNamedElement)node); } if (node instanceof PyImportedNameDefiner) { importedNameDefiners.add((PyImportedNameDefiner)node); @@ -274,6 +275,10 @@ public class ScopeImpl implements Scope { } } } + + private void processNamedElement(@NotNull PsiNamedElement element) { + namedElements.computeIfAbsent(element.getName(), __ -> Sets.newLinkedHashSet()).add(element); + } }); myNamedElements = namedElements; diff --git a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java index 56695988e66a..a936078564ed 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java @@ -714,6 +714,13 @@ public class PyUnresolvedReferencesInspectionTest extends PyInspectionTestCase { " self.value = self.datetime(2016, 1, 1)"); } + // PY-19599 + public void testDefinedInParameterDefaultAndBody() { + doTestByText("def f(p=(x for x in [])):\n" + + " x = 1\n" + + " return x"); + } + @NotNull @Override protected Class getInspectionClass() {