Skip to content

Commit

Permalink
Better error message for nested TypedDict (python#11658)
Browse files Browse the repository at this point in the history
  • Loading branch information
97littleleaf11 authored and tushar-deepsource committed Jan 20, 2022
1 parent a3ab996 commit 51e1427
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion mypy/semanal_typeddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 51e1427

Please sign in to comment.