Skip to content

Commit a6250f4

Browse files
enforce singleton qualities on NoExtraItems
1 parent 611b3e6 commit a6250f4

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Lib/typing.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3056,18 +3056,31 @@ def _namedtuple_mro_entries(bases):
30563056
NamedTuple.__mro_entries__ = _namedtuple_mro_entries
30573057

30583058

3059-
class _NoExtraItemsType:
3059+
class _SingletonMeta(type):
3060+
def __setattr__(cls, attr, value):
3061+
# TypeError is consistent with the behavior of NoneType
3062+
raise TypeError(
3063+
f"cannot set {attr!r} attribute of immutable type {cls.__name__!r}"
3064+
)
3065+
3066+
3067+
class _NoExtraItemsType(metaclass=_SingletonMeta):
30603068
"""The type of the NoExtraItems singleton."""
30613069

30623070
__slots__ = ()
30633071

3072+
def __new__(cls):
3073+
return globals().get("NoExtraItems") or object.__new__(cls)
3074+
30643075
def __repr__(self):
30653076
return 'typing.NoExtraItems'
30663077

30673078
def __reduce__(self):
30683079
return 'NoExtraItems'
30693080

30703081
NoExtraItems = _NoExtraItemsType()
3082+
del _NoExtraItemsType
3083+
del _SingletonMeta
30713084

30723085

30733086
def _get_typeddict_qualifiers(annotation_type):

0 commit comments

Comments
 (0)