Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow TypedDict initialization from Type #16963

Merged
merged 1 commit into from
Mar 1, 2024
Merged
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
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]
Loading