PY-31424 Adjust the signature to TypeVar to accept type references

One can pass string literals containing such references both to "*constraints"
and "bound" parameters.
This commit is contained in:
Mikhail Golubev
2018-08-17 12:57:03 +03:00
parent dc79cf0ace
commit a28f8a4668
4 changed files with 12 additions and 7 deletions
@@ -312,8 +312,9 @@ public class PyTypingTypeProvider extends PyTypeProviderBase {
final PyElementGenerator generator = PyElementGenerator.getInstance(referenceExpression.getProject());
parameters.add(PyCallableParameterImpl.nonPsi("name", builtinCache.getStringType(languageLevel)));
parameters.add(PyCallableParameterImpl.positionalNonPsi("constraints", builtinCache.getTypeType()));
parameters.add(PyCallableParameterImpl.nonPsi("bound", builtinCache.getTypeType(), generator.createEllipsis()));
final PyType typeOrForwardReference = PyUnionType.union(builtinCache.getTypeType(), builtinCache.getStrType());
parameters.add(PyCallableParameterImpl.positionalNonPsi("constraints", typeOrForwardReference));
parameters.add(PyCallableParameterImpl.nonPsi("bound", typeOrForwardReference, generator.createEllipsis()));
final PyClassType boolType = builtinCache.getBoolType();
final PyExpression falseValue = generator.createExpressionFromText(languageLevel, "False");
@@ -1,4 +1,7 @@
from typing import TypeVar
TypeVar("T", int, str, bound=int, covariant=True, contravariant=True)
TypeVar("T", <warning descr="Expected type 'type', got 'int' instead">0</warning>, <warning descr="Expected type 'type', got 'int' instead">1</warning>, <warning descr="Expected type 'type', got 'int' instead">bound=2</warning>, <warning descr="Expected type 'bool', got 'int' instead">covariant=3</warning>, <warning descr="Expected type 'bool', got 'int' instead">contravariant=4</warning>)
TypeVar("T", int, str, bound='int', covariant=True, contravariant=True)
TypeVar("T", int, 'str', bound=int, covariant=True, contravariant=True)
TypeVar("T", 'int', 'str', bound=int, covariant=True, contravariant=True)
TypeVar("T", <warning descr="Expected type 'Union[type, str]', got 'int' instead">0</warning>, <warning descr="Expected type 'Union[type, str]', got 'int' instead">1</warning>, <warning descr="Expected type 'Union[type, str]', got 'int' instead">bound=2</warning>, <warning descr="Expected type 'bool', got 'int' instead">covariant=3</warning>, <warning descr="Expected type 'bool', got 'int' instead">contravariant=4</warning>)
@@ -846,14 +846,15 @@ public class PyParameterInfoTest extends LightMarkedTestCase {
);
}
// PY-28127
// PY-28127 PY-31424
public void testInitializingTypeVar() {
runWithLanguageLevel(
LanguageLevel.PYTHON34,
() -> {
final int offset = loadTest(1).get("<arg1>").getTextOffset();
feignCtrlP(offset).check("name: str, *constraints: type, bound: type=..., covariant: bool=False, contravariant: bool=False",
feignCtrlP(offset).check("name: str, *constraints: Union[type, str], bound: Union[type, str]=..., " +
"covariant: bool=False, contravariant: bool=False",
new String[]{"name: str, "},
ArrayUtil.EMPTY_STRING_ARRAY);
}
@@ -81,7 +81,7 @@ public class Py3TypeCheckerInspectionTest extends PyInspectionTestCase {
public void testStrFormatPy3() {
doTest();
}
// PY-18762
public void testHomogeneousTuples() {
doTest();
@@ -341,7 +341,7 @@ public class Py3TypeCheckerInspectionTest extends PyInspectionTestCase {
runWithLanguageLevel(LanguageLevel.PYTHON37, () -> super.doMultiFileTest());
}
// PY-28127
// PY-28127 PY-31424
public void testInitializingTypeVar() {
doTest();
}