From 3544832e0ad651e9cc2fe256635f687b8a4ecfdd Mon Sep 17 00:00:00 2001 From: Bhuvansh855 Date: Wed, 8 Apr 2026 03:14:16 +0530 Subject: [PATCH] Clarify positional argument handling in TypedDict constructor and add conformance tests --- .../tests/typeddict_constructor_positional.py | 18 ++++++++++++++++++ docs/spec/typeddict.rst | 10 +++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 conformance/tests/typeddict_constructor_positional.py diff --git a/conformance/tests/typeddict_constructor_positional.py b/conformance/tests/typeddict_constructor_positional.py new file mode 100644 index 000000000..9a6590a22 --- /dev/null +++ b/conformance/tests/typeddict_constructor_positional.py @@ -0,0 +1,18 @@ +from typing import TypedDict + +class TD1(TypedDict): + a: int + +class TD2(TD1): + b: str + +# keyword arguments (OK) +TD1(a=1) + +# positional TypedDict (OK) +td2: TD2 = {"a": 1, "b": "x"} +TD1(td2) + +# invalid positional arguments +TD1({"a": 1}) # E +TD1([("a", 1)]) # E diff --git a/docs/spec/typeddict.rst b/docs/spec/typeddict.rst index 81b72ea9c..8ec7489c3 100644 --- a/docs/spec/typeddict.rst +++ b/docs/spec/typeddict.rst @@ -266,7 +266,15 @@ The TypedDict constructor TypedDict types are callable at runtime and can be used as a constructor to create values that conform to the TypedDict type. The constructor -takes only keyword arguments, corresponding to the items of the TypedDict. +primarily takes keyword arguments, corresponding to the items of the TypedDict. + +Additionally, type checkers may allow a call with a single positional argument +whose type is a TypedDict. In this case, the resulting value is initialized from +that argument. + +Other forms of positional arguments (such as arbitrary mappings or sequences) +should not be accepted by type checkers. + Example:: m = Movie(name='Blade Runner', year=1982)