From bd8f1c5752b0c653558743e324ca318cab3a097c Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 17 Feb 2025 19:06:34 +0300 Subject: [PATCH 1/2] gh-129567: Add a note to `typing.TypedDict` docs about name mangling --- Doc/library/typing.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 0fee782121b0af..72c3992f6fce10 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2551,15 +2551,20 @@ types. This functional syntax allows defining keys which are not valid :ref:`identifiers `, for example because they are - keywords or contain hyphens:: + keywords or contain hyphens, or when key names must not be mangled + like regular private names:: # raises SyntaxError class Point2D(TypedDict): in: int # 'in' is a keyword x-y: int # name with hyphens + class Definition(TypedDict): + __schema: str # mangled to `_Definition__schema` + # OK, functional syntax 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 mark individual keys as non-required using :data:`NotRequired`:: From 87db1846bac0f6ac7206445ffd53c2e78fff4533 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 4 Mar 2025 10:47:40 +0300 Subject: [PATCH 2/2] Address review --- Doc/library/typing.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 72c3992f6fce10..19b4081c993e44 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2551,8 +2551,8 @@ types. This functional syntax allows defining keys which are not valid :ref:`identifiers `, for example because they are - keywords or contain hyphens, or when key names must not be mangled - like regular private names:: + keywords or contain hyphens, or when key names must not be + :ref:`mangled ` like regular private names:: # raises SyntaxError class Point2D(TypedDict):