8000 gh-91243: Add typing.Required and NotRequired (PEP 655) by JelleZijlstra · Pull Request #32419 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-91243: Add typing.Required and NotRequired (PEP 655) #32419

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 6 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
Apply feedback from David Foster
  • Loading branch information
JelleZijlstra committed Apr 10, 2022
commit 3cafefa03964fae96eb99f8c281e0beb0f0b22bb
21 changes: 21 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3979,6 +3979,10 @@ class AnnotatedMovie(TypedDict):
title: Annotated[Required[str], "foobar"]
year: NotRequired[Annotated[int, 2000]]

class DeeplyAnnotatedMovie(TypedDict):
title: Annotated[Annotated[Required[str], "foobar"], "another level"]
year: NotRequired[Annotated[int, 2000]]

class HasForeignBaseClass(mod_generics_cache.A):
some_xrepr: 'XRepr'
other_a: 'mod_generics_cache.A'
Expand Down Expand Up @@ -4278,6 +4282,11 @@ def test_get_type_hints_typeddict(self):
'title': Annotated[Required[str], "foobar"],
'year': NotRequired[Annotated[int, 2000]],
}
assert get_type_hints(DeeplyAnnotatedMovie) == {'title': str, 'year': int}
assert get_type_hints(DeeplyAnnotatedMovie, include_extras=True) == {
'title': Annotated[Annotated[Required[str], "foobar"], "another level"],
'year': NotRequired[Annotated[int, 2000]],
}

class GetUtilitiesTestCase(TestCase):
def test_get_origin(self):
Expand Down Expand Up @@ -5432,6 +5441,12 @@ class C(type(Required)):
with self.assertRaises(TypeError):
class C(type(Required[int])):
pass
with self.assertRaises(TypeError):
class C(Required):
pass
with self.assertRaises(TypeError):
class C(Required[int]):
pass

def test_cannot_init(self):
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -5472,6 +5487,12 @@ class C(type(NotRequired)):
with self.assertRaises(TypeError):
class C(type(NotRequired[int])):
pass
with self.assertRaises(TypeError):
class C(NotRequired):
pass
with self.assertRaises(TypeError):
class C(NotRequired[int]):
pass

def test_cannot_init(self):
with self.assertRaises(TypeError):
Expand Down
8000
4 changes: 2 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2906,7 +2906,7 @@ class Movie(TypedDict, total=False):
There is no runtime checking that a required key is actually provided
when instantiating a related TypedDict.
"""
item = _type_check(parameters, f'{self._name} accepts only single type')
item = _type_check(parameters, f'{self._name} accepts only a single type')
return _GenericAlias(self, (item,))


Expand All @@ -2924,7 +2924,7 @@ class Movie(TypedDict):
year=1999,
)
"""
item =_type_check(parameters, f'{self._name} accepts only single type')
item = _type_check(parameters, f'{self._name} accepts only a single type')
return _GenericAlias(self, (item,))


Expand Down
0