Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 138
Support __extra__ for PEP 728.#329
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -52,6 +52,9 @@ | ||
| # 3.12 changes the representation of Unpack[] (PEP 692) | ||
| TYPING_3_12_0 = sys.version_info[:3] >= (3, 12, 0) | ||
| # 3.13 drops support for the keyword argument syntax of TypedDict | ||
| TYPING_3_13_0 = sys.version_info[:3] >= (3, 13, 0) | ||
| # https://github.com/python/cpython/pull/27017 was backported into some 3.9 and 3.10 | ||
| # versions, but not all | ||
| HAS_FORWARD_MODULE = "module" in inspect.signature(typing._type_check).parameters | ||
| @@ -3820,6 +3823,24 @@ class ChildWithInlineAndOptional(Untotal, Inline): | ||
| {'inline': bool, 'untotal': str, 'child': bool}, | ||
| ) | ||
| class Closed(TypedDict, closed=True): | ||
| __extra_items__: None | ||
| class Unclosed(TypedDict, closed=False): | ||
| ... | ||
| class ChildUnclosed(Closed, Unclosed): | ||
| ... | ||
| self.assertFalse(ChildUnclosed.__closed__) | ||
| self.assertEqual(ChildUnclosed.__extra_items__, type(None)) | ||
| class ChildClosed(Unclosed, Closed): | ||
| ... | ||
| self.assertFalse(ChildClosed.__closed__) | ||
| self.assertEqual(ChildClosed.__extra_items__, type(None)) | ||
| wrong_bases = [ | ||
| (One, Regular), | ||
| (Regular, One), | ||
| @@ -4178,6 +4199,139 @@ class AllTheThings(TypedDict): | ||
| self.assertEqual(AllTheThings.__readonly_keys__, frozenset({'a', 'b', 'c'})) | ||
| self.assertEqual(AllTheThings.__mutable_keys__, frozenset({'d'})) | ||
| def test_extra_keys_non_readonly(self): | ||
| class Base(TypedDict, closed=True): | ||
| __extra_items__: str | ||
| class Child(Base): | ||
| a: NotRequired[int] | ||
| self.assertEqual(Child.__required_keys__, frozenset({})) | ||
| self.assertEqual(Child.__optional_keys__, frozenset({'a'})) | ||
| self.assertEqual(Child.__readonly_keys__, frozenset({})) | ||
| self.assertEqual(Child.__mutable_keys__, frozenset({'a'})) | ||
| def test_extra_keys_readonly(self): | ||
| class Base(TypedDict, closed=True): | ||
| __extra_items__: ReadOnly[str] | ||
| class Child(Base): | ||
| a: NotRequired[str] | ||
| self.assertEqual(Child.__required_keys__, frozenset({})) | ||
| self.assertEqual(Child.__optional_keys__, frozenset({'a'})) | ||
| self.assertEqual(Child.__readonly_keys__, frozenset({})) | ||
| self.assertEqual(Child.__mutable_keys__, frozenset({'a'})) | ||
| def test_extra_key_required(self): | ||
| with self.assertRaisesRegex( | ||
| TypeError, | ||
| "Special key __extra_items__ does not support Required" | ||
| ): | ||
| TypedDict("A", {"__extra_items__": Required[int]}, closed=True) | ||
| with self.assertRaisesRegex( | ||
| TypeError, | ||
| "Special key __extra_items__ does not support NotRequired" | ||
| ): | ||
| TypedDict("A", {"__extra_items__": NotRequired[int]}, closed=True) | ||
| def test_regular_extra_items(self): | ||
| class ExtraReadOnly(TypedDict): | ||
| __extra_items__: ReadOnly[str] | ||
| self.assertEqual(ExtraReadOnly.__required_keys__, frozenset({'__extra_items__'})) | ||
| self.assertEqual(ExtraReadOnly.__optional_keys__, frozenset({})) | ||
| self.assertEqual(ExtraReadOnly.__readonly_keys__, frozenset({'__extra_items__'})) | ||
| self.assertEqual(ExtraReadOnly.__mutable_keys__, frozenset({})) | ||
| self.assertEqual(ExtraReadOnly.__extra_items__, None) | ||
| self.assertFalse(ExtraReadOnly.__closed__) | ||
| class ExtraRequired(TypedDict): | ||
| __extra_items__: Required[str] | ||
| self.assertEqual(ExtraRequired.__required_keys__, frozenset({'__extra_items__'})) | ||
| self.assertEqual(ExtraRequired.__optional_keys__, frozenset({})) | ||
| self.assertEqual(ExtraRequired.__readonly_keys__, frozenset({})) | ||
| self.assertEqual(ExtraRequired.__mutable_keys__, frozenset({'__extra_items__'})) | ||
| self.assertEqual(ExtraRequired.__extra_items__, None) | ||
| self.assertFalse(ExtraRequired.__closed__) | ||
| class ExtraNotRequired(TypedDict): | ||
| __extra_items__: NotRequired[str] | ||
| self.assertEqual(ExtraNotRequired.__required_keys__, frozenset({})) | ||
| self.assertEqual(ExtraNotRequired.__optional_keys__, frozenset({'__extra_items__'})) | ||
| self.assertEqual(ExtraNotRequired.__readonly_keys__, frozenset({})) | ||
| self.assertEqual(ExtraNotRequired.__mutable_keys__, frozenset({'__extra_items__'})) | ||
| self.assertEqual(ExtraNotRequired.__extra_items__, None) | ||
| self.assertFalse(ExtraNotRequired.__closed__) | ||
| def test_closed_inheritance(self): | ||
| class Base(TypedDict, closed=True): | ||
| __extra_items__: ReadOnly[Union[str, None]] | ||
| self.assertEqual(Base.__required_keys__, frozenset({})) | ||
| self.assertEqual(Base.__optional_keys__, frozenset({})) | ||
| self.assertEqual(Base.__readonly_keys__, frozenset({})) | ||
| self.assertEqual(Base.__mutable_keys__, frozenset({})) | ||
| self.assertEqual(Base.__annotations__, {}) | ||
| self.assertEqual(Base.__extra_items__, ReadOnly[Union[str, None]]) | ||
| self.assertTrue(Base.__closed__) | ||
| class Child(Base): | ||
| a: int | ||
| __extra_items__: int | ||
| self.assertEqual(Child.__required_keys__, frozenset({'a', "__extra_items__"})) | ||
| self.assertEqual(Child.__optional_keys__, frozenset({})) | ||
| self.assertEqual(Child.__readonly_keys__, frozenset({})) | ||
| self.assertEqual(Child.__mutable_keys__, frozenset({'a', "__extra_items__"})) | ||
| self.assertEqual(Child.__annotations__, {"__extra_items__": int, "a": int}) | ||
| self.assertEqual(Child.__extra_items__, ReadOnly[Union[str, None]]) | ||
| self.assertFalse(Child.__closed__) | ||
| class GrandChild(Child, closed=True): | ||
| __extra_items__: str | ||
| self.assertEqual(GrandChild.__required_keys__, frozenset({'a', "__extra_items__"})) | ||
| self.assertEqual(GrandChild.__optional_keys__, frozenset({})) | ||
| self.assertEqual(GrandChild.__readonly_keys__, frozenset({})) | ||
| self.assertEqual(GrandChild.__mutable_keys__, frozenset({'a', "__extra_items__"})) | ||
| self.assertEqual(GrandChild.__annotations__, {"__extra_items__": int, "a": int}) | ||
| self.assertEqual(GrandChild.__extra_items__, str) | ||
| self.assertTrue(GrandChild.__closed__) | ||
| def test_implicit_extra_items(self): | ||
| class Base(TypedDict): | ||
| a: int | ||
| self.assertEqual(Base.__extra_items__, None) | ||
| self.assertFalse(Base.__closed__) | ||
| class ChildA(Base, closed=True): | ||
| ... | ||
| self.assertEqual(ChildA.__extra_items__, Never) | ||
| self.assertTrue(ChildA.__closed__) | ||
| class ChildB(Base, closed=True): | ||
| __extra_items__: None | ||
| self.assertEqual(ChildB.__extra_items__, type(None)) | ||
| self.assertTrue(ChildB.__closed__) | ||
| @skipIf( | ||
| TYPING_3_13_0, | ||
| "The keyword argument alternative to define a " | ||
| "TypedDict type using the functional syntax is no longer supported" | ||
| ) | ||
| def test_backwards_compatibility(self): | ||
PIG208 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| with self.assertWarns(DeprecationWarning): | ||
| TD = TypedDict("TD", closed=int) | ||
| self.assertFalse(TD.__closed__) | ||
| self.assertEqual(TD.__annotations__, {"closed": int}) | ||
| class AnnotatedTests(BaseTestCase): | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -875,7 +875,7 @@ def _get_typeddict_qualifiers(annotation_type): | ||
| break | ||
| class _TypedDictMeta(type): | ||
| def __new__(cls, name, bases, ns, *, total=True): | ||
| def __new__(cls, name, bases, ns, *, total=True, closed=False): | ||
| """Create new typed dict class object. | ||
| This method is called when TypedDict is subclassed, | ||
| @@ -920,6 +920,7 @@ def __new__(cls, name, bases, ns, *, total=True): | ||
| optional_keys = set() | ||
| readonly_keys = set() | ||
| mutable_keys = set() | ||
| extra_items_type = None | ||
| for base in bases: | ||
| base_dict = base.__dict__ | ||
| @@ -929,6 +930,26 @@ def __new__(cls, name, bases, ns, *, total=True): | ||
| optional_keys.update(base_dict.get('__optional_keys__', ())) | ||
| readonly_keys.update(base_dict.get('__readonly_keys__', ())) | ||
| mutable_keys.update(base_dict.get('__mutable_keys__', ())) | ||
| base_extra_items_type = base_dict.get('__extra_items__', None) | ||
| if base_extra_items_type is not None: | ||
| extra_items_type = base_extra_items_type | ||
| if closed and extra_items_type is None: | ||
| extra_items_type = Never | ||
| if closed and "__extra_items__" in own_annotations: | ||
| annotation_type = own_annotations.pop("__extra_items__") | ||
| qualifiers = set(_get_typeddict_qualifiers(annotation_type)) | ||
| if Required in qualifiers: | ||
| raise TypeError( | ||
| "Special key __extra_items__ does not support " | ||
| "Required" | ||
| ) | ||
| if NotRequired in qualifiers: | ||
| raise TypeError( | ||
| "Special key __extra_items__ does not support " | ||
| "NotRequired" | ||
| ) | ||
| extra_items_type = annotation_type | ||
| annotations.update(own_annotations) | ||
| for annotation_key, annotation_type in own_annotations.items(): | ||
| @@ -956,6 +977,8 @@ def __new__(cls, name, bases, ns, *, total=True): | ||
| tp_dict.__mutable_keys__ = frozenset(mutable_keys) | ||
| if not hasattr(tp_dict, '__total__'): | ||
| tp_dict.__total__ = total | ||
| tp_dict.__closed__ = closed | ||
| tp_dict.__extra_items__ = extra_items_type | ||
| return tp_dict | ||
| __call__ = dict # static method | ||
| @@ -969,7 +992,7 @@ def __subclasscheck__(cls, other): | ||
| _TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {}) | ||
| @_ensure_subclassable(lambda bases: (_TypedDict,)) | ||
| def TypedDict(typename, fields=_marker, /, *, total=True, **kwargs): | ||
| def TypedDict(typename, fields=_marker, /, *, total=True, closed=False, **kwargs): | ||
PIG208 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """A simple typed namespace. At runtime it is equivalent to a plain dict. | ||
| TypedDict creates a dictionary type such that a type checker will expect all | ||
| @@ -1029,6 +1052,9 @@ class Point2D(TypedDict): | ||
| "using the functional syntax, pass an empty dictionary, e.g. " | ||
| ) + example + "." | ||
| warnings.warn(deprecation_msg, DeprecationWarning, stacklevel=2) | ||
| if closed is not False and closed is not True: | ||
| kwargs["closed"] = closed | ||
| closed = False | ||
| fields = kwargs | ||
| elif kwargs: | ||
| raise TypeError("TypedDict takes either a dict or keyword arguments," | ||
| @@ -1050,7 +1076,7 @@ class Point2D(TypedDict): | ||
| # Setting correct module is necessary to make typed dict classes pickleable. | ||
| ns['__module__'] = module | ||
| td = _TypedDictMeta(typename, (), ns, total=total) | ||
| td = _TypedDictMeta(typename, (), ns, total=total, closed=closed) | ||
| td.__orig_bases__ = (TypedDict,) | ||
| return td | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.