From ad894202093071c0482391735d133e24f5fd246f Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Wed, 19 Jul 2017 17:39:01 +0300 Subject: [PATCH] PY-18816 Test that accessing annotation in type declarations doesn't cause unstubbing --- .../stubs/AttributeTypeDeclaration.py | 5 ++++ python/testData/stubs/VariableAnnotation.py | 3 +- .../com/jetbrains/python/PyStubsTest.java | 30 ++++++++++++++++--- 3 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 python/testData/stubs/AttributeTypeDeclaration.py diff --git a/python/testData/stubs/AttributeTypeDeclaration.py b/python/testData/stubs/AttributeTypeDeclaration.py new file mode 100644 index 000000000000..55fffa229e25 --- /dev/null +++ b/python/testData/stubs/AttributeTypeDeclaration.py @@ -0,0 +1,5 @@ +class MyClass: + foo: str + + def __init__(self): + self.bar: int \ No newline at end of file diff --git a/python/testData/stubs/VariableAnnotation.py b/python/testData/stubs/VariableAnnotation.py index 7036172f9c55..47cfc6c89cc0 100644 --- a/python/testData/stubs/VariableAnnotation.py +++ b/python/testData/stubs/VariableAnnotation.py @@ -1 +1,2 @@ -x: int = ... \ No newline at end of file +x: int = ... +y: str \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyStubsTest.java b/python/testSrc/com/jetbrains/python/PyStubsTest.java index c407622400c6..7b6592933a74 100644 --- a/python/testSrc/com/jetbrains/python/PyStubsTest.java +++ b/python/testSrc/com/jetbrains/python/PyStubsTest.java @@ -755,11 +755,33 @@ public class PyStubsTest extends PyTestCase { public void testVariableAnnotation() { runWithLanguageLevel(LanguageLevel.PYTHON36, () -> { final PyFile file = getTestFile(); - final PyTargetExpression var = file.findTopLevelAttribute("x"); - final String annotation = var.getAnnotationValue(); - assertEquals("int", annotation); + final PyTargetExpression assignmentTarget = file.findTopLevelAttribute("x"); + final String assignmentAnnotation = assignmentTarget.getAnnotationValue(); + assertEquals("int", assignmentAnnotation); assertNotParsed(file); - assertType("int", var, TypeEvalContext.codeInsightFallback(myFixture.getProject())); + assertType("int", assignmentTarget, TypeEvalContext.codeInsightFallback(myFixture.getProject())); + assertNotParsed(file); + + final PyTargetExpression typeDecTarget = file.findTopLevelAttribute("y"); + final String typeDecAnnotation = typeDecTarget.getAnnotationValue(); + assertEquals("str", typeDecAnnotation); + assertNotParsed(file); + assertType("str", typeDecTarget, TypeEvalContext.codeInsightFallback(myFixture.getProject())); + assertNotParsed(file); + }); + } + + // PY-18116 + public void testAttributeTypeDeclaration() { + runWithLanguageLevel(LanguageLevel.PYTHON36, () -> { + final PyFile file = getTestFile(); + final PyClass pyClass = file.findTopLevelClass("MyClass"); + final TypeEvalContext context = TypeEvalContext.codeInsightFallback(myFixture.getProject()); + final PyTargetExpression classAttr = pyClass.findClassAttribute("foo", false, context); + assertType("str", classAttr, context); + + final PyTargetExpression instAttr = pyClass.findInstanceAttribute("bar", false); + assertType("int", instAttr, context); assertNotParsed(file); }); }