From 46ca96b7cb7fc665b7b9af8145bed6801ae03e54 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Tue, 28 Nov 2017 21:52:06 +0300 Subject: [PATCH] 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). --- .../com/jetbrains/python/PyTypingTest.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/python/testSrc/com/jetbrains/python/PyTypingTest.java b/python/testSrc/com/jetbrains/python/PyTypingTest.java index 34e0726be345..ff9cd6a45d3c 100644 --- a/python/testSrc/com/jetbrains/python/PyTypingTest.java +++ b/python/testSrc/com/jetbrains/python/PyTypingTest.java @@ -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",