Skip to content

Commit 3c87af2

Browse files
authored
Allow TypedDict initialization from Type (#16963)
Fixes #11644
1 parent 055184f commit 3c87af2

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

mypy/checkexpr.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,8 @@ def analyze_type_type_callee(self, item: ProperType, context: Context) -> Type:
18551855
# We support Type of namedtuples but not of tuples in general
18561856
if isinstance(item, TupleType) and tuple_fallback(item).type.fullname != "builtins.tuple":
18571857
return self.analyze_type_type_callee(tuple_fallback(item), context)
1858+
if isinstance(item, TypedDictType):
1859+
return self.typeddict_callable_from_context(item)
18581860

18591861
self.msg.unsupported_type_type(item, context)
18601862
return AnyType(TypeOfAny.from_error)

test-data/unit/check-typeddict.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3447,3 +3447,19 @@ class Params(TypedDict("Params", {'x': int})):
34473447
p: Params = {'x': 2}
34483448
reveal_type(p) # N: Revealed type is "TypedDict('__main__.Params', {'x': builtins.int})"
34493449
[builtins fixtures/dict.pyi]
3450+
3451+
[case testInitTypedDictFromType]
3452+
from typing import TypedDict, Type
3453+
3454+
class Point(TypedDict):
3455+
x: int
3456+
y: int
3457+
3458+
def func(cls: Type[Point]) -> None:
3459+
reveal_type(cls) # N: Revealed type is "Type[TypedDict('__main__.Point', {'x': builtins.int, 'y': builtins.int})]"
3460+
cls(x=1, y=2)
3461+
cls(1, 2) # E: Too many positional arguments
3462+
cls(x=1) # E: Missing named argument "y"
3463+
cls(x=1, y=2, error="") # E: Unexpected keyword argument "error"
3464+
[typing fixtures/typing-full.pyi]
3465+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)