Don't forget type declarations while collecting class attributes (PY-26163)

This commit is contained in:
Semyon Proshev
2018-01-22 21:41:16 +03:00
parent 0ee0f0189c
commit cff0305fb8
6 changed files with 66 additions and 0 deletions
@@ -1066,6 +1066,12 @@ public class PyClassImpl extends PyBaseElementImpl<PyClassStub> implements PyCla
}
}
}
else if (psiElement instanceof PyTypeDeclarationStatement) {
final PyExpression target = ((PyTypeDeclarationStatement)psiElement).getTarget();
if (target instanceof PyTargetExpression) {
result.add((PyTargetExpression)target);
}
}
}
return result;
}
@@ -0,0 +1,16 @@
from typing import ClassVar
def dist(p1):
print(p1.x)
print(p1.y)
return p1
class A:
x: ClassVar[int]
y: ClassVar[str]
dist(A)
dist(A())
@@ -0,0 +1,16 @@
import typing
def dist(p1):
print(p1.x)
print(p1.y)
return p1
class TP(typing.NamedTuple):
x: int
y: int
dist(TP)
dist(TP(1, 2))
@@ -0,0 +1,2 @@
class MyClass:
foo: int
@@ -908,4 +908,20 @@ public class PyStubsTest extends PyTestCase {
assertNull(funcStub.findChildStubByType(PyElementTypes.ANNOTATION));
});
}
// PY-26163
public void testClassAttributeTypeDeclaration() {
runWithLanguageLevel(LanguageLevel.PYTHON36, () -> {
final PyFile file = getTestFile();
final PyClass pyClass = file.findTopLevelClass("MyClass");
final TypeEvalContext context = TypeEvalContext.codeAnalysis(file.getProject(), file);
assertNotNull(pyClass.findClassAttribute("foo", false, context));
assertNotParsed(file);
//noinspection ResultOfMethodCallIgnored
pyClass.getText();
assertNotNull(pyClass.findClassAttribute("foo", false, context));
});
}
}
@@ -455,4 +455,14 @@ public class PyTypeCheckerInspectionTest extends PyInspectionTestCase {
public void testCallableInstanceAgainstCallable() {
runWithLanguageLevel(LanguageLevel.PYTHON35, this::doTest);
}
// PY-26163
public void testTypingNTAgainstStructural() {
runWithLanguageLevel(LanguageLevel.PYTHON36, this::doTest);
}
// PY-26163
public void testDefinitionAgainstStructural() {
runWithLanguageLevel(LanguageLevel.PYTHON36, this::doTest);
}
}