File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed
Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change @@ -266,7 +266,15 @@ The TypedDict constructor
266266
267267TypedDict types are callable at runtime and can be used as a constructor
268268to 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+
270278Example::
271279
272280 m = Movie(name='Blade Runner', year=1982)
You can’t perform that action at this time.
0 commit comments