PY-27186 Use generic type information from .pyi stub in the respective .py file

by implementing missing getGenericType() and getGenericSubstitutions()
in PyiTypeProvider.
This commit is contained in:
Mikhail Golubev
2017-11-30 15:19:58 +03:00
parent fe6a2c234d
commit 63f25e8e4d
7 changed files with 73 additions and 4 deletions
@@ -17,16 +17,16 @@ package com.jetbrains.python.pyi;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.PsiElement;
import com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyCallExpressionHelper;
import com.jetbrains.python.psi.types.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import static com.jetbrains.python.psi.PyUtil.as;
/**
* @author vlan
@@ -139,4 +139,24 @@ public class PyiTypeProvider extends PyTypeProviderBase {
}
return null;
}
@Nullable
@Override
public PyType getGenericType(@NotNull PyClass cls, @NotNull TypeEvalContext context) {
final PyClass classStub = as(PyiUtil.getPythonStub(cls), PyClass.class);
if (classStub != null) {
return new PyTypingTypeProvider().getGenericType(classStub, context);
}
return null;
}
@NotNull
@Override
public Map<PyType, PyType> getGenericSubstitutions(@NotNull PyClass cls, @NotNull TypeEvalContext context) {
final PyClass classStub = as(PyiUtil.getPythonStub(cls), PyClass.class);
if (classStub != null) {
return new PyTypingTypeProvider().getGenericSubstitutions(classStub, context);
}
return Collections.emptyMap();
}
}
@@ -0,0 +1,3 @@
from other import Holder
ex<caret>pr = Holder(42).get()
@@ -0,0 +1,6 @@
class Holder:
def __init__(self, x):
self.x = x
def get(self):
return self.x
@@ -0,0 +1,11 @@
from typing import Generic, TypeVar
T = TypeVar('T')
class Holder(Generic[T]):
def __init__(self, x: T):
pass
def get(self) -> T:
pass
@@ -0,0 +1,9 @@
class Holder:
def __init__(self, x):
self.x = x
def get(self):
return self.x
ex<caret>pr = Holder(42).get()
@@ -0,0 +1,11 @@
from typing import Generic, TypeVar
T = TypeVar('T')
class Holder(Generic[T]):
def __init__(self, x: T):
pass
def get(self) -> T:
pass
@@ -133,4 +133,13 @@ public class PyiTypeTest extends PyTestCase {
public void testOverloadedNotMatchedGenericType() {
doTest("Union[Dict[str, Any], list]");
}
public void testGenericClassDefinitionInOtherFile() {
doTest("int");
}
// PY-27186
public void testGenericClassDefinitionInSameFile() {
doTest("int");
}
}