From 54424d22fe884e409156e930edc3da7809b53026 Mon Sep 17 00:00:00 2001 From: Jingchen Ye <97littleleaf11@gmail.com> Date: Fri, 3 Dec 2021 15:02:25 +0800 Subject: [PATCH 1/3] better message for inline defined TypedDict --- mypy/semanal_typeddict.py | 8 +++++++- test-data/unit/check-typeddict.test | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/mypy/semanal_typeddict.py b/mypy/semanal_typeddict.py index 1b076ac4835d8..5286c09e26f93 100644 --- a/mypy/semanal_typeddict.py +++ b/mypy/semanal_typeddict.py @@ -307,7 +307,13 @@ 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 + field_type_expr.callee.fullname in TPDICT_NAMES): + self.fail_typeddict_arg( + 'Inline TypedDict types not supported; use assignment', + 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..5cbed4394f736 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 + 'y': int + }) +}) +[builtins fixtures/dict.pyi] -- Define TypedDict (Class syntax) From d8256bb1981ab225e57f1657e7ffe6233b2120e6 Mon Sep 17 00:00:00 2001 From: Jingchen Ye <97littleleaf11@gmail.com> Date: Fri, 3 Dec 2021 15:12:54 +0800 Subject: [PATCH 2/3] Fix --- mypy/semanal_typeddict.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mypy/semanal_typeddict.py b/mypy/semanal_typeddict.py index 5286c09e26f93..5898175a879ae 100644 --- a/mypy/semanal_typeddict.py +++ b/mypy/semanal_typeddict.py @@ -308,6 +308,7 @@ def parse_typeddict_fields_with_types( self.api.is_stub_file) except TypeTranslationError: 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', From 11e8be866a6df183d9e58178a5c9a9257d6024db Mon Sep 17 00:00:00 2001 From: Jingchen Ye <97littleleaf11@gmail.com> Date: Fri, 3 Dec 2021 18:40:39 +0800 Subject: [PATCH 3/3] Modify error msg --- mypy/semanal_typeddict.py | 2 +- test-data/unit/check-typeddict.test | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mypy/semanal_typeddict.py b/mypy/semanal_typeddict.py index 5898175a879ae..9611dd07e4970 100644 --- a/mypy/semanal_typeddict.py +++ b/mypy/semanal_typeddict.py @@ -311,7 +311,7 @@ def parse_typeddict_fields_with_types( 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', + 'Inline TypedDict types not supported; use assignment to define TypedDict', field_type_expr) else: self.fail_typeddict_arg('Invalid field type', field_type_expr) diff --git a/test-data/unit/check-typeddict.test b/test-data/unit/check-typeddict.test index 5cbed4394f736..776ea8fd7fc7c 100644 --- a/test-data/unit/check-typeddict.test +++ b/test-data/unit/check-typeddict.test @@ -78,7 +78,7 @@ p = Point(x='meaning_of_life', y=1337) # E: Incompatible types (expression has [case testCannotCreateTypedDictInstanceWithInlineTypedDict] from mypy_extensions import TypedDict D = TypedDict('D', { - 'x': TypedDict('E', { # E: Inline TypedDict types not supported; use assignment + 'x': TypedDict('E', { # E: Inline TypedDict types not supported; use assignment to define TypedDict 'y': int }) })