diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2022-02-19 01:56:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-19 01:56:30 (GMT) |
commit | 32e3e0bea613711a8f19003445eebcb05fb75acc (patch) | |
tree | 2eb7b1c9fb4cc76bc566ddc0c89413a14af6f7a0 /Lib | |
parent | 25c0b9d243b64ccd2eeab483089eaf7e4b4d5834 (diff) | |
download | cpython-32e3e0bea613711a8f19003445eebcb05fb75acc.zip cpython-32e3e0bea613711a8f19003445eebcb05fb75acc.tar.gz cpython-32e3e0bea613711a8f19003445eebcb05fb75acc.tar.bz2 |
bpo-46685: improve test coverage of `Self` and `Never` in `typing` (GH-31222)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_typing.py | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 3dc9497..b38e27c 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -127,6 +127,14 @@ class AnyTests(BaseTestCase): 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 @@ class AssertNeverTests(BaseTestCase): 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 @@ class SelfTests(BaseTestCase): 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 @@ class SpecialAttrsTests(BaseTestCase): 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', |