Repository: strawberry-graphql/strawberry

Issue:
Mypy plugin crashes when converting Django choices to strawberry enums
Hi,

I'm passing a Django `TextChoices` to `strawberry.enum` to convert my models enums to GraphQL but it makes mypy plugin crash... Using a regular `enum.Enum` works fine (no crash).

This is the traceback:

```
nextbackend/common/schema.py:11: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.910
Traceback (most recent call last):
  File "mypy/semanal.py", line 4872, in accept
  File "mypy/nodes.py", line 1073, in accept
  File "mypy/semanal.py", line 2023, in visit_assignment_stmt
  File "mypy/semanal.py", line 2278, in apply_dynamic_class_hook
  File "/Users/edouard/.pyenv/versions/3.9.6/envs/next-backend-3.9.6/lib/python3.9/site-packages/strawberry/ext/mypy_plugin.py", line 222, in enum_hook
    type_alias = TypeAlias(
  File "mypy/nodes.py", line 2805, in __init__
TypeError: mypy.types.Type object expected; got None
nextbackend/common/schema.py:11: : note: use --pdb to drop into pdb
```

As discussed I tried to debug without success and the GitHub version of mypy doesn't help.


Discussion:

Resolving patch:
diff --git a/strawberry/ext/mypy_plugin.py b/strawberry/ext/mypy_plugin.py
--- a/strawberry/ext/mypy_plugin.py
+++ b/strawberry/ext/mypy_plugin.py
@@ -92,7 +92,7 @@ def _get_named_type(name: str, api: SemanticAnalyzerPluginInterface):
     return api.named_type(name)
 
 
-def _get_type_for_expr(expr: Expression, api: SemanticAnalyzerPluginInterface):
+def _get_type_for_expr(expr: Expression, api: SemanticAnalyzerPluginInterface) -> Type:
     if isinstance(expr, NameExpr):
         # guarding agains invalid nodes, still have to figure out why this happens
         # but sometimes mypy crashes because the internal node of the named type
@@ -109,7 +109,7 @@ def _get_type_for_expr(expr: Expression, api: SemanticAnalyzerPluginInterface):
 
     if isinstance(expr, IndexExpr):
         type_ = _get_type_for_expr(expr.base, api)
-        type_.args = (_get_type_for_expr(expr.index, api),)
+        type_.args = (_get_type_for_expr(expr.index, api),)  # type: ignore
 
         return type_
 
@@ -216,22 +216,22 @@ def enum_hook(ctx: DynamicClassDefContext) -> None:
             )
             return
 
+    enum_type: Optional[Type]
+
     try:
         enum_type = _get_type_for_expr(first_argument, ctx.api)
-
-        type_alias = TypeAlias(
-            enum_type,
-            fullname=ctx.api.qualified_name(ctx.name),
-            line=ctx.call.line,
-            column=ctx.call.column,
-        )
     except InvalidNodeTypeException:
-        type_alias = TypeAlias(
-            AnyType(TypeOfAny.from_error),
-            fullname=ctx.api.qualified_name(ctx.name),
-            line=ctx.call.line,
-            column=ctx.call.column,
-        )
+        enum_type = None
+
+    if not enum_type:
+        enum_type = AnyType(TypeOfAny.from_error)
+
+    type_alias = TypeAlias(
+        enum_type,
+        fullname=ctx.api.qualified_name(ctx.name),
+        line=ctx.call.line,
+        column=ctx.call.column,
+    )
 
     ctx.api.add_symbol_table_node(
         ctx.name, SymbolTableNode(GDEF, type_alias, plugin_generated=False)


Test patch: