PY-24990 Add extra tests on more elaborate cases of annotations for cls/self

First, on matching type var with a union receiver type.

Though we're not able to match types like "Union[Type[A], Type[B]]" with
parametrized "Type[T]" to infer that T is Union[A, B] yet (should we?).

Second, on calling an annotated instance method on a class object
(when an argument for "self" is provided explicitly).
This commit is contained in:
Mikhail Golubev
2017-12-15 15:14:12 +03:00
parent 163846c5d8
commit 46ca96b7cb
@@ -1165,6 +1165,82 @@ public class PyTypingTest extends PyTestCase {
"expr = D().factory()");
}
// PY-24990
public void testSelfAnnotationReceiverUnionType() {
doTest("Union[A, B]",
"from typing import TypeVar\n" +
"\n" +
"T = TypeVar('T')\n" +
"\n" +
"class Base:\n" +
" def method(self: T) -> T:\n" +
" pass\n" +
"\n" +
"class A(Base):\n" +
" pass\n" +
"\n" +
"class B(Base): \n" +
" pass\n" +
"\n" +
"expr = (A() or B()).method()");
}
// PY-24990
public void _testClsAnnotationReceiverUnionType() {
doTest("Union[A, B]",
"from typing import TypeVar, Type\n" +
"\n" +
"T = TypeVar('T')\n" +
"\n" +
"class Base:\n" +
" @classmethod\n" +
" def factory(cls: Type[T]) -> T:\n" +
" pass\n" +
"\n" +
"class A(Base):\n" +
" pass\n" +
"\n" +
"class B(Base):\n" +
" pass\n" +
"\n" +
"expr = (A or B).factory()");
}
// PY-24990
public void testClsAnnotationReceiverUnionTypeClassMethodCalledOnMixedInstanceClassObject() {
doTest("A",
"from typing import TypeVar, Type\n" +
"\n" +
"T = TypeVar('T')\n" +
"\n" +
"class Base:\n" +
" @classmethod\n" +
" def factory(cls: Type[T]) -> T:\n" +
" pass\n" +
"\n" +
"class A(Base):\n" +
" pass\n" +
"\n" +
"expr = (A or A()).factory()");
}
// PY-24990
public void testSelfAnnotationInstanceMethodCalledOnClassObject() {
doTest("D",
"from typing import TypeVar\n" +
"\n" +
"T = TypeVar('T')\n" +
"\n" +
"class C:\n" +
" def method(self: T) -> T:\n" +
" pass\n" +
"\n" +
"class D(C):\n" +
" pass\n" +
"\n" +
"expr = C.method(D())");
}
// PY-24990
public void testSelfAnnotationInTypeCommentSameClassInstance() {
doTest("C",