Skip to content
18 changes: 18 additions & 0 deletions conformance/tests/typeddict_constructor_positional.py
Original file line number Diff line number Diff line change
@@ -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)

# additional positional forms (behavior may vary across type checkers)
TD1({"a": 1})
TD1([("a", 1)])
10 changes: 9 additions & 1 deletion docs/spec/typeddict.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Type checkers may choose to accept additional forms of positional arguments,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Type checkers may choose to accept additional forms of positional arguments,
Type checkers may choose to accept additional forms of positional arguments, and/or mixed positional/keyword calls,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! @carljm

I initially added wording to clarify that type checkers may accept additional positional forms, but based on the discussion, I’ve simplified the spec to avoid implying specific behavior beyond what is clearly supported.

For now, the spec only describes the keyword-based constructor and the case of a single TypedDict positional argument, without constraining or explicitly encouraging other forms.

Happy to refine this further if we reach consensus on specific patterns that should be required.

but such behavior is not required by the specification.

Example::

m = Movie(name='Blade Runner', year=1982)
Expand Down