Skip to content

Commit b0cdd82

Browse files
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__`.
1 parent 3f7146d commit b0cdd82

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Lib/typing.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,7 +3080,8 @@ def _get_typeddict_qualifiers(annotation_type):
30803080

30813081

30823082
class _TypedDictMeta(type):
3083-
def __new__(cls, name, bases, ns, total=True):
3083+
def __new__(cls, name, bases, ns, total=True, closed=None,
3084+
extra_items=NoExtraItems):
30843085
"""Create a new typed dict class object.
30853086
30863087
This method is called when TypedDict is subclassed,
@@ -3092,6 +3093,8 @@ def __new__(cls, name, bases, ns, total=True):
30923093
if type(base) is not _TypedDictMeta and base is not Generic:
30933094
raise TypeError('cannot inherit from both a TypedDict type '
30943095
'and a non-TypedDict base class')
3096+
if closed is not None and extra_items is not NoExtraItems:
3097+
raise TypeError(f"Cannot combine closed={closed!r} and extra_items")
30953098

30963099
if any(issubclass(b, Generic) for b in bases):
30973100
generic_base = (Generic,)
@@ -3203,6 +3206,8 @@ def __annotate__(format):
32033206
tp_dict.__readonly_keys__ = frozenset(readonly_keys)
32043207
tp_dict.__mutable_keys__ = frozenset(mutable_keys)
32053208
tp_dict.__total__ = total
3209+
tp_dict.__closed__ = closed
3210+
tp_dict.__extra_items__ = extra_items
32063211
return tp_dict
32073212

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

32163221

3217-
def TypedDict(typename, fields, /, *, total=True):
3222+
def TypedDict(typename, fields, /, *, total=True, closed=None,
3223+
extra_items=NoExtraItems):
32183224
"""A simple typed namespace. At runtime it is equivalent to a plain dict.
32193225
32203226
TypedDict creates a dictionary type such that a type checker will expect all
@@ -3275,7 +3281,8 @@ class DatabaseUser(TypedDict):
32753281
# Setting correct module is necessary to make typed dict classes pickleable.
32763282
ns['__module__'] = module
32773283

3278-
td = _TypedDictMeta(typename, (), ns, total=total)
3284+
td = _TypedDictMeta(typename, (), ns, total=total, closed=closed,
3285+
extra_items=extra_items)
32793286
td.__orig_bases__ = (TypedDict,)
32803287
return td
32813288

0 commit comments

Comments
 (0)