Skip to content

Commit

Permalink
Allow TypedDict initialization from Type (#16963)
Browse files Browse the repository at this point in the history
Fixes #11644
  • Loading branch information
cdce8p committed Mar 1, 2024
1 parent 055184f commit 3c87af2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,8 @@ def analyze_type_type_callee(self, item: ProperType, context: Context) -> Type:
# We support Type of namedtuples but not of tuples in general
if isinstance(item, TupleType) and tuple_fallback(item).type.fullname != "builtins.tuple":
return self.analyze_type_type_callee(tuple_fallback(item), context)
if isinstance(item, TypedDictType):
return self.typeddict_callable_from_context(item)

self.msg.unsupported_type_type(item, context)
return AnyType(TypeOfAny.from_error)
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -3447,3 +3447,19 @@ class Params(TypedDict("Params", {'x': int})):
p: Params = {'x': 2}
reveal_type(p) # N: Revealed type is "TypedDict('__main__.Params', {'x': builtins.int})"
[builtins fixtures/dict.pyi]

[case testInitTypedDictFromType]
from typing import TypedDict, Type

class Point(TypedDict):
x: int
y: int

def func(cls: Type[Point]) -> None:
reveal_type(cls) # N: Revealed type is "Type[TypedDict('__main__.Point', {'x': builtins.int, 'y': builtins.int})]"
cls(x=1, y=2)
cls(1, 2) # E: Too many positional arguments
cls(x=1) # E: Missing named argument "y"
cls(x=1, y=2, error="") # E: Unexpected keyword argument "error"
[typing fixtures/typing-full.pyi]
[builtins fixtures/tuple.pyi]

0 comments on commit 3c87af2

Please sign in to comment.