From f947191a5f456f2f057f96dc0ff78ecc01395eb4 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Thu, 29 Feb 2024 14:15:12 +0100 Subject: [PATCH] Allow TypedDict initialization from Type --- mypy/checkexpr.py | 2 ++ test-data/unit/check-typeddict.test | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 2842606b7b18d..e893410e2b7df 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -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) diff --git a/test-data/unit/check-typeddict.test b/test-data/unit/check-typeddict.test index adf4d210ed0c8..639be7bde8d8c 100644 --- a/test-data/unit/check-typeddict.test +++ b/test-data/unit/check-typeddict.test @@ -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]