mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
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:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -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
|
||||
+9
@@ -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()
|
||||
+11
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user