From 32e3e0bea613711a8f19003445eebcb05fb75acc Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Sat, 19 Feb 2022 04:56:30 +0300 Subject: [PATCH] bpo-46685: improve test coverage of `Self` and `Never` in `typing` (GH-31222) --- Lib/test/test_typing.py | 49 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 3dc9497c3a02e8..b38e27c5f90471 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -127,6 +127,14 @@ def test_any_works_with_alias(self): class BottomTypeTestsMixin: bottom_type: ClassVar[Any] + def test_equality(self): + self.assertEqual(self.bottom_type, self.bottom_type) + self.assertIs(self.bottom_type, self.bottom_type) + self.assertNotEqual(self.bottom_type, None) + + def test_get_origin(self): + self.assertIs(get_origin(self.bottom_type), None) + def test_instance_type_error(self): with self.assertRaises(TypeError): isinstance(42, self.bottom_type) @@ -162,6 +170,19 @@ class NoReturnTests(BottomTypeTestsMixin, BaseTestCase): def test_repr(self): self.assertEqual(repr(NoReturn), 'typing.NoReturn') + def test_get_type_hints(self): + def some(arg: NoReturn) -> NoReturn: ... + def some_str(arg: 'NoReturn') -> 'typing.NoReturn': ... + + expected = {'arg': NoReturn, 'return': NoReturn} + for target in [some, some_str]: + with self.subTest(target=target): + self.assertEqual(gth(target), expected) + + def test_not_equality(self): + self.assertNotEqual(NoReturn, Never) + self.assertNotEqual(Never, NoReturn) + class NeverTests(BottomTypeTestsMixin, BaseTestCase): bottom_type = Never @@ -169,6 +190,15 @@ class NeverTests(BottomTypeTestsMixin, BaseTestCase): def test_repr(self): self.assertEqual(repr(Never), 'typing.Never') + def test_get_type_hints(self): + def some(arg: Never) -> Never: ... + def some_str(arg: 'Never') -> 'typing.Never': ... + + expected = {'arg': Never, 'return': Never} + for target in [some, some_str]: + with self.subTest(target=target): + self.assertEqual(gth(target), expected) + class AssertNeverTests(BaseTestCase): def test_exception(self): @@ -177,11 +207,23 @@ def test_exception(self): class SelfTests(BaseTestCase): + def test_equality(self): + self.assertEqual(Self, Self) + self.assertIs(Self, Self) + self.assertNotEqual(Self, None) + def test_basics(self): class Foo: def bar(self) -> Self: ... + class FooStr: + def bar(self) -> 'Self': ... + class FooStrTyping: + def bar(self) -> 'typing.Self': ... - self.assertEqual(gth(Foo.bar), {'return': Self}) + for target in [Foo, FooStr, FooStrTyping]: + with self.subTest(target=target): + self.assertEqual(gth(target.bar), {'return': Self}) + self.assertIs(get_origin(Self), None) def test_repr(self): self.assertEqual(repr(Self), 'typing.Self') @@ -194,6 +236,9 @@ def test_cannot_subclass(self): with self.assertRaises(TypeError): class C(type(Self)): pass + with self.assertRaises(TypeError): + class C(Self): + pass def test_cannot_init(self): with self.assertRaises(TypeError): @@ -5425,11 +5470,13 @@ def test_special_attrs(self): typing.Literal: 'Literal', typing.NewType: 'NewType', typing.NoReturn: 'NoReturn', + typing.Never: 'Never', typing.Optional: 'Optional', typing.TypeAlias: 'TypeAlias', typing.TypeGuard: 'TypeGuard', typing.TypeVar: 'TypeVar', typing.Union: 'Union', + typing.Self: 'Self', # Subscribed special forms typing.Annotated[Any, "Annotation"]: 'Annotated', typing.ClassVar[Any]: 'ClassVar',