Skip to content

Commit

Permalink
bpo-46685: improve test coverage of Self and Never in typing (G…
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored Feb 19, 2022
1 parent 25c0b9d commit 32e3e0b
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -162,13 +170,35 @@ 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

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):
Expand All @@ -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')
Expand All @@ -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):
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 32e3e0b

Please sign in to comment.