Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

# invalid positional arguments
TD1({"a": 1}) # E
TD1([("a", 1)]) # E
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.

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)
Expand Down