Skip to content

Commit 3544832

Browse files
committed
Clarify positional argument handling in TypedDict constructor and add conformance tests
1 parent 9ba065d commit 3544832

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import TypedDict
2+
3+
class TD1(TypedDict):
4+
a: int
5+
6+
class TD2(TD1):
7+
b: str
8+
9+
# keyword arguments (OK)
10+
TD1(a=1)
11+
12+
# positional TypedDict (OK)
13+
td2: TD2 = {"a": 1, "b": "x"}
14+
TD1(td2)
15+
16+
# invalid positional arguments
17+
TD1({"a": 1}) # E
18+
TD1([("a", 1)]) # E

docs/spec/typeddict.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,15 @@ The TypedDict constructor
266266

267267
TypedDict types are callable at runtime and can be used as a constructor
268268
to create values that conform to the TypedDict type. The constructor
269-
takes only keyword arguments, corresponding to the items of the TypedDict.
269+
primarily takes keyword arguments, corresponding to the items of the TypedDict.
270+
271+
Additionally, type checkers may allow a call with a single positional argument
272+
whose type is a TypedDict. In this case, the resulting value is initialized from
273+
that argument.
274+
275+
Other forms of positional arguments (such as arbitrary mappings or sequences)
276+
should not be accepted by type checkers.
277+
270278
Example::
271279

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

0 commit comments

Comments
 (0)