Skip to content
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

Improve test coverage for is_typeddict #104884

Merged
merged 2 commits into from
May 24, 2023
Merged
Changes from 1 commit
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
25 changes: 22 additions & 3 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7223,10 +7223,29 @@ class Wrong(*bases):
pass

def test_is_typeddict(self):
assert is_typeddict(Point2D) is True
assert is_typeddict(Union[str, int]) is False
self.assertTrue(is_typeddict(Point2D))
self.assertFalse(is_typeddict(Union[str, int]))
Copy link
Member

Choose a reason for hiding this comment

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

+1 for using the unittest methods, but note that this isn't a direct translation. assertTrue only asserts that the thing is truthy, not that it actually is the True constant.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh good point, I'll switch to assertIs(..., True).

# classes, not instances
assert is_typeddict(Point2D()) is False
self.assertFalse(is_typeddict(Point2D()))
call_based = TypedDict('call_based', {'a': int})
self.assertTrue(is_typeddict(call_based))
self.assertFalse(is_typeddict(call_based()))

T = TypeVar("T")
class BarGeneric(TypedDict, Generic[T]):
a: T
self.assertTrue(is_typeddict(BarGeneric))
self.assertFalse(is_typeddict(BarGeneric[int]))
self.assertFalse(is_typeddict(BarGeneric()))

class NewGeneric[T](TypedDict):
Copy link
Member Author

Choose a reason for hiding this comment

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

I'll manually remove this one from the 3.11 backport

a: T
self.assertTrue(is_typeddict(NewGeneric))
self.assertFalse(is_typeddict(NewGeneric[int]))
self.assertFalse(is_typeddict(NewGeneric()))

# The TypedDict constructor is not itself a TypedDict
self.assertFalse(is_typeddict(TypedDict))

def test_get_type_hints(self):
self.assertEqual(
Expand Down