FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-137840: Implement PEP 728 (closed and extra_items in typing.TypedDict) by angela-tarantula · Pull Request #137933 · python/cpython · GitHub

Repository navigation

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Prev Previous commit
Next Next commit
Update _TypedDictMeta and TypedDict
They now both accept `closed` and `extra_items` as parameters.
For introspection, these arguments are also mapped to 2 new attributes:
`__closed__` and `__extra_items__`.
  • Loading branch information
angela-tarantula committed Aug 20, 2025
commit f9755d3e05f54478f323cf88ae941e4d9391d56f
13 changes: 10 additions & 3 deletions Lib/typing.py
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
Expand Up @@ -3080,7 +3080,8 @@ def _get_typeddict_qualifiers(annotation_type):


class _TypedDictMeta(type):
def __new__(cls, name, bases, ns, total=True):
def __new__(cls, name, bases, ns, total=True, closed=None,
extra_items=NoExtraItems):
"""Create a new typed dict class object.

This method is called when TypedDict is subclassed,
Expand All @@ -3092,6 +3093,8 @@ def __new__(cls, name, bases, ns, total=True):
if type(base) is not _TypedDictMeta and base is not Generic:
raise TypeError('cannot inherit from both a TypedDict type '
'and a non-TypedDict base class')
if closed is not None and extra_items is not NoExtraItems:
raise TypeError(f"Cannot combine closed={closed!r} and extra_items")

if any(issubclass(b, Generic) for b in bases):
generic_base = (Generic,)
Expand Down Expand Up @@ -3203,6 +3206,8 @@ def __annotate__(format):
tp_dict.__readonly_keys__ = frozenset(readonly_keys)
tp_dict.__mutable_keys__ = frozenset(mutable_keys)
tp_dict.__total__ = total
tp_dict.__closed__ = closed
tp_dict.__extra_items__ = extra_items
return tp_dict

__call__ = dict # static method
Expand All @@ -3214,7 +3219,8 @@ def __subclasscheck__(cls, other):
__instancecheck__ = __subclasscheck__


def TypedDict(typename, fields, /, *, total=True):
def TypedDict(typename, fields, /, *, total=True, closed=None,
extra_items=NoExtraItems):
"""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
Expand Down Expand Up @@ -3275,7 +3281,8 @@ class DatabaseUser(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,
extra_items=extra_items)
td.__orig_bases__ = (TypedDict,)
return td

Expand Down

Back | FazBrowse Home | New Git URL