diff --git a/mypy/semanal_typeddict.py b/mypy/semanal_typeddict.py index 1b076ac4835d8..9611dd07e4970 100644 --- a/mypy/semanal_typeddict.py +++ b/mypy/semanal_typeddict.py @@ -307,7 +307,14 @@ def parse_typeddict_fields_with_types( type = expr_to_unanalyzed_type(field_type_expr, self.options, self.api.is_stub_file) except TypeTranslationError: - self.fail_typeddict_arg('Invalid field type', field_type_expr) + if (isinstance(field_type_expr, CallExpr) and + isinstance(field_type_expr.callee, RefExpr) and + field_type_expr.callee.fullname in TPDICT_NAMES): + self.fail_typeddict_arg( + 'Inline TypedDict types not supported; use assignment to define TypedDict', + field_type_expr) + else: + self.fail_typeddict_arg('Invalid field type', field_type_expr) return [], [], False analyzed = self.api.anal_type(type) if analyzed is None: diff --git a/test-data/unit/check-typeddict.test b/test-data/unit/check-typeddict.test index ac4c23461d7ee..776ea8fd7fc7c 100644 --- a/test-data/unit/check-typeddict.test +++ b/test-data/unit/check-typeddict.test @@ -75,6 +75,14 @@ Point = TypedDict('Point', {'x': int, 'y': int}) p = Point(x='meaning_of_life', y=1337) # E: Incompatible types (expression has type "str", TypedDict item "x" has type "int") [builtins fixtures/dict.pyi] +[case testCannotCreateTypedDictInstanceWithInlineTypedDict] +from mypy_extensions import TypedDict +D = TypedDict('D', { + 'x': TypedDict('E', { # E: Inline TypedDict types not supported; use assignment to define TypedDict + 'y': int + }) +}) +[builtins fixtures/dict.pyi] -- Define TypedDict (Class syntax)