Repository: python/mypy
Pull request: Better error message for nested TypedDict
### Description

Closes https://github.com/python/mypy/issues/4299

## Test Plan

Add a new test case `testCannotCreateTypedDictInstanceWithInlineTypedDict`


Problem statement:
Better message for nested TypedDict
An attempt to define a nested TypedDict results in a not very helpful message:

```py
from mypy_extensions import TypedDict

D = TypedDict('D', {
    'x': TypedDict('E', {  # Invalid field type
        'y': int
    })
})
```

A better message could be something like "Inline TypedDict types not supported; use assignment" statement to define a TypedDict".

Hints from issue discussion:
Inlining seems to be supported now. Can close.
@GuSuku do you mean that this should be supported now?
I tried to run it in MyPy but it still gives me the same error: 

MyPy Playground:
https://mypy-play.net/?mypy=latest&python=3.9&gist=29faabf0dd252d7772d2d9e01e8ce5a4

Commit message: Fix

Patch to review:
diff --git a/mypy/semanal_typeddict.py b/mypy/semanal_typeddict.py
index 1b076ac4835d..5898175a879a 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',
+                        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 ac4c23461d7e..5cbed4394f73 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)
 


Code review comments:
diff_hunk: @@ -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',
path: mypy/semanal_typeddict.py
text: @user1:
Bikeshedding: What about making this more detailed, such as like this:

> Inline TypedDict types not supported; use assignment to define TypedDict