Skip to content

Commit 872bc86

Browse files
Better error message for nested TypedDict (#11658)
1 parent 2b7e2df commit 872bc86

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

mypy/semanal_typeddict.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,14 @@ def parse_typeddict_fields_with_types(
307307
type = expr_to_unanalyzed_type(field_type_expr, self.options,
308308
self.api.is_stub_file)
309309
except TypeTranslationError:
310-
self.fail_typeddict_arg('Invalid field type', field_type_expr)
310+
if (isinstance(field_type_expr, CallExpr) and
311+
isinstance(field_type_expr.callee, RefExpr) and
312+
field_type_expr.callee.fullname in TPDICT_NAMES):
313+
self.fail_typeddict_arg(
314+
'Inline TypedDict types not supported; use assignment to define TypedDict',
315+
field_type_expr)
316+
else:
317+
self.fail_typeddict_arg('Invalid field type', field_type_expr)
311318
return [], [], False
312319
analyzed = self.api.anal_type(type)
313320
if analyzed is None:

test-data/unit/check-typeddict.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ Point = TypedDict('Point', {'x': int, 'y': int})
7575
p = Point(x='meaning_of_life', y=1337) # E: Incompatible types (expression has type "str", TypedDict item "x" has type "int")
7676
[builtins fixtures/dict.pyi]
7777

78+
[case testCannotCreateTypedDictInstanceWithInlineTypedDict]
79+
from mypy_extensions import TypedDict
80+
D = TypedDict('D', {
81+
'x': TypedDict('E', { # E: Inline TypedDict types not supported; use assignment to define TypedDict
82+
'y': int
83+
})
84+
})
85+
[builtins fixtures/dict.pyi]
7886

7987
-- Define TypedDict (Class syntax)
8088

0 commit comments

Comments
 (0)