mirror of https://github.com/python/cpython.git
gh-129567: Add a note to `typing.TypedDict` docs about name mangling (#130233)
This commit is contained in:
parent
f67ff9e820
commit
63ffb406bb
|
@ -2551,15 +2551,20 @@ types.
|
||||||
|
|
||||||
This functional syntax allows defining keys which are not valid
|
This functional syntax allows defining keys which are not valid
|
||||||
:ref:`identifiers <identifiers>`, for example because they are
|
:ref:`identifiers <identifiers>`, for example because they are
|
||||||
keywords or contain hyphens::
|
keywords or contain hyphens, or when key names must not be
|
||||||
|
:ref:`mangled <private-name-mangling>` like regular private names::
|
||||||
|
|
||||||
# raises SyntaxError
|
# raises SyntaxError
|
||||||
class Point2D(TypedDict):
|
class Point2D(TypedDict):
|
||||||
in: int # 'in' is a keyword
|
in: int # 'in' is a keyword
|
||||||
x-y: int # name with hyphens
|
x-y: int # name with hyphens
|
||||||
|
|
||||||
|
class Definition(TypedDict):
|
||||||
|
__schema: str # mangled to `_Definition__schema`
|
||||||
|
|
||||||
# OK, functional syntax
|
# OK, functional syntax
|
||||||
Point2D = TypedDict('Point2D', {'in': int, 'x-y': int})
|
Point2D = TypedDict('Point2D', {'in': int, 'x-y': int})
|
||||||
|
Definition = TypedDict('Definition', {'__schema': str}) # not mangled
|
||||||
|
|
||||||
By default, all keys must be present in a ``TypedDict``. It is possible to
|
By default, all keys must be present in a ``TypedDict``. It is possible to
|
||||||
mark individual keys as non-required using :data:`NotRequired`::
|
mark individual keys as non-required using :data:`NotRequired`::
|
||||||
|
|
Loading…
Reference in New Issue