Skip to content

bpo-46685: improve test coverage of Self and Never in typing #31222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -5295,11 +5340,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