PY-20744 Consider variable annotations only in current file to prevent unstabbing everywhere

It will be fixed eventually when we start storing annotations
for parameter, functions and target expressions in stub files and
process them using PyTypeParser only.
This commit is contained in:
Mikhail Golubev
2016-09-16 08:16:26 +03:00
parent 18f9ba8b6e
commit 83771208a3
5 changed files with 26 additions and 7 deletions
@@ -176,14 +176,16 @@ public class PyTypingTypeProvider extends PyTypeProviderBase {
public PyType getReferenceType(@NotNull PsiElement referenceTarget, TypeEvalContext context, @Nullable PsiElement anchor) {
if (referenceTarget instanceof PyTargetExpression) {
final PyTargetExpression target = (PyTargetExpression)referenceTarget;
// XXX: Requires switching from stub to AST
final PyAnnotation annotation = target.getAnnotation();
if (annotation != null) {
final PyExpression value = annotation.getValue();
if (value != null) {
return getType(value, new Context(context));
if (context.maySwitchToAST(target)) {
// XXX: Requires switching from stub to AST
final PyAnnotation annotation = target.getAnnotation();
if (annotation != null) {
final PyExpression value = annotation.getValue();
if (value != null) {
return getType(value, new Context(context));
}
return null;
}
return null;
}
final String comment = target.getTypeCommentAnnotation();
if (comment != null) {
@@ -0,0 +1 @@
attr: int = ...
@@ -0,0 +1,3 @@
from .lib import attr
x = attr
@@ -596,4 +596,17 @@ public class PyStubsTest extends PyTestCase {
assertNotParsed(file1);
assertNotParsed(file2);
}
public void testVariableAnnotationsInExternalFiles() {
runWithLanguageLevel(LanguageLevel.PYTHON36, () -> {
final PyFile current = getTestFile(getTestName(true) + "/main.py");
final PyFile external = getTestFile(getTestName(true) + "/lib.py");
final PyTargetExpression attr = current.findTopLevelAttribute("x");
assertNotNull(attr);
final TypeEvalContext context = TypeEvalContext.codeAnalysis(myFixture.getProject(), current);
// Will turn into concrete type when we start saving annotations in stubs
assertNull(context.getType(attr));
assertNotParsed(external);
});
}
}