10000 bpo-46445: cover multiple inheritance of `TypedDict` in `test_typing` by sobolevn · Pull Request #30719 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-46445: cover multiple inheritance of TypedDict in test_typing #30719 8000

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 3 commits into from
Jan 21, 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
88 changes: 87 additions & 1 deletion Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from typing import is_typeddict
from typing import no_type_check, no_type_check_decorator
from typing import Type
from typing import NewType
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related, but it was unused and I felt like there's no need to create a separate PR for this. If required, I can revert this change.

from typing import NamedTuple, TypedDict
from typing import IO, TextIO, BinaryIO
from typing import Pattern, Match
Expand Down Expand Up @@ -4393,6 +4392,93 @@ class Cat(Animal):
'voice': str,
}

def test_multiple_inheritance(self):
class One(TypedDict):
one: int
class Two(TypedDict):
two: str
class Untotal(TypedDict, total=False):
untotal: str
Inline = TypedDict('Inline', {'inline': bool})
class Regular:
pass

class Child(One, Two):
child: bool
self.assertEqual(
Child.__required_keys__,
frozenset(['one', 'two', 'child']),
)
self.assertEqual(
Child.__optional_keys__,
frozenset([]),
)
self.assertEqual(
Child.__annotations__,
{'one': int, 'two': str, 'child': bool},
)

class ChildWithOptional(One, Untotal):
child: bool
self.assertEqual(
ChildWithOptional.__required_keys__,
frozenset(['one', 'child']),
)
self.assertEqual(
ChildWithOptional.__optional_keys__,
frozenset(['untotal']),
)
self.assertEqual(
ChildWithOptional.__annotations__,
{'one': int, 'untotal': str, 'child': bool},
)

class ChildWithTotalFalse(One, Untotal, total=False):
child: bool
self.assertEqual(
ChildWithTotalFalse.__required_keys__,
frozenset(['one']),
)
self.assertEqual(
ChildWithTotalFalse.__optional_keys__,
frozenset(['untotal', 'child']),
)
self.assertEqual(
ChildWithTotalFalse.__annotations__,
{'one': int, 'untotal': str, 'child': bool},
)

class ChildWithInlineAndOptional(Untotal, Inline):
child: bool
self.assertEqual(
ChildWithInlineAndOptional.__required_keys__,
frozenset(['inline', 'child']),
)
self.assertEqual(
ChildWithInlineAndOptional.__optional_keys__,
frozenset(['untotal']),
)
self.assertEqual(
ChildWithInlineAndOptional.__annotations__,
{'inline': bool, 'untotal': str, 'child': bool},
)

wrong_bases = [
(One, Regular),
(Regular, One),
(One, Two, Regular),
(Inline, Regular),
(Untotal, Regular),
]
for bases in wrong_bases:
with self.subTest(bases=bases):
with self.assertRaisesRegex(
TypeError,
'cannot inherit from both a TypedDict type and a non-TypedDict',
):
class Wrong(*bases):
pass

def test_is_typeddict(self):
assert is_typeddict(Point2D) is True
assert is_typeddict(Union[str, int]) is False
Expand Down
0