Infer type for targets initialized through TypeVar(...) (PY-28227)

This commit is contained in:
Semyon Proshev
2018-04-27 19:57:15 +03:00
parent 88871610d5
commit 437b183160
3 changed files with 25 additions and 0 deletions
@@ -491,6 +491,14 @@ public class PyTypingTypeProvider extends PyTypeProviderBase {
return annotatedType;
}
final PyExpression assignedValue = PyTypingAliasStubType.getAssignedValueStubLike(target);
if (assignedValue != null) {
final PyType type = getGenericTypeFromTypeVar(assignedValue, new Context(context));
if (type != null) {
return Ref.create(type);
}
}
final String name = target.getReferencedName();
final ScopeOwner scopeOwner = ScopeUtil.getScopeOwner(target);
if (name == null || scopeOwner == null) {
@@ -0,0 +1,3 @@
from typing import TypeVar
T = TypeVar('T')
@@ -3230,6 +3230,20 @@ public class PyTypeTest extends PyTestCase {
doTest("float", "expr = round(True, 1)");
}
// PY-28227
public void testTypeVarTargetAST() {
doTest("T",
"from typing import TypeVar\n" +
"expr = TypeVar('T')");
}
// PY-28227
public void testTypeVarTargetStub() {
doMultiFileTest("T",
"from a import T\n" +
"expr = T");
}
private static List<TypeEvalContext> getTypeEvalContexts(@NotNull PyExpression element) {
return ImmutableList.of(TypeEvalContext.codeAnalysis(element.getProject(), element.getContainingFile()).withTracing(),
TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile()).withTracing());