Skip to content

Commit 63ffb40

Browse files
authored
gh-129567: Add a note to typing.TypedDict docs about name mangling (#130233)
1 parent f67ff9e commit 63ffb40

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Doc/library/typing.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -2551,15 +2551,20 @@ types.
25512551

25522552
This functional syntax allows defining keys which are not valid
25532553
:ref:`identifiers <identifiers>`, for example because they are
2554-
keywords or contain hyphens::
2554+
keywords or contain hyphens, or when key names must not be
2555+
:ref:`mangled <private-name-mangling>` like regular private names::
25552556

25562557
# raises SyntaxError
25572558
class Point2D(TypedDict):
25582559
in: int # 'in' is a keyword
25592560
x-y: int # name with hyphens
25602561

2562+
class Definition(TypedDict):
2563+
__schema: str # mangled to `_Definition__schema`
2564+
25612565
# OK, functional syntax
25622566
Point2D = TypedDict('Point2D', {'in': int, 'x-y': int})
2567+
Definition = TypedDict('Definition', {'__schema': str}) # not mangled
25632568

25642569
By default, all keys must be present in a ``TypedDict``. It is possible to
25652570
mark individual keys as non-required using :data:`NotRequired`::

0 commit comments

Comments
 (0)