I am running into an issue with the strawberry plugin when using list unpacking to define a union type.
Command: dmypy run -- --follow-imports=skip
Results:
Daemon crashed!
Traceback (most recent call last):
File "mypy/dmypy_server.py", line 229, in serve
File "mypy/dmypy_server.py", line 272, in run_command
File "mypy/dmypy_server.py", line 331, in cmd_run
File "mypy/dmypy_server.py", line 391, in check
File "mypy/dmypy_server.py", line 528, in fine_grained_increment
File "mypy/server/update.py", line 245, in update
File "mypy/server/update.py", line 328, in update_one
File "mypy/server/update.py", line 393, in update_module
File "mypy/server/update.py", line 602, in update_module_isolated
File "mypy/semanal_main.py", line 78, in semantic_analysis_for_scc
File "mypy/semanal_main.py", line 199, in process_top_levels
File "mypy/semanal_main.py", line 326, in semantic_analyze_target
File "mypy/semanal.py", line 414, in refresh_partial
File "mypy/semanal.py", line 425, in refresh_top_level
File "mypy/semanal.py", line 5222, in accept
File "mypy/errors.py", line 902, in report_internal_error
File "mypy/semanal.py", line 5220, in accept
File "mypy/nodes.py", line 1155, in accept
File "mypy/semanal.py", line 2091, in visit_assignment_stmt
File "mypy/semanal.py", line 2377, in apply_dynamic_class_hook
File "/Users/ryanhoulihan/Baton/anton-v2/.venv/lib/python3.10/site-packages/strawberry/ext/mypy_plugin.py", line 181, in union_hook
tuple(_get_type_for_expr(x, ctx.api) for x in types.items)
File "/Users/ryanhoulihan/Baton/anton-v2/.venv/lib/python3.10/site-packages/strawberry/ext/mypy_plugin.py", line 181, in <genexpr>
tuple(_get_type_for_expr(x, ctx.api) for x in types.items)
File "/Users/ryanhoulihan/Baton/anton-v2/.venv/lib/python3.10/site-packages/strawberry/ext/mypy_plugin.py", line 148, in _get_type_for_expr
raise ValueError(f"Unsupported expression {type(expr)}")
ValueError: Unsupported expression <class 'mypy.nodes.StarExpr'>
Minimal code to reproduce:
import strawberry
@strawberry.type
class UsernameAlreadyTaken():
message: str = "Username taken."
@strawberry.type
class InvalidEmailAddress():
message: str = "Email address is invalid."
@strawberry.type
class User():
name: str
email: str
UserErrors = (
UsernameAlreadyTaken,
InvalidEmailAddress,
)
UserMutationResponse = strawberry.union(
name="UserResponse", types=(User, *UserErrors)
)
Work around to prevent the mypy crash:
UserMutationTypes = (User, *UserErrors)
UserMutationResponse = strawberry.union(name="UserResponse", types=UserMutationTypes)
But this results in the following when I use it as a return type on a mutation:
error: Variable "baton_tms.graphql.user.UserMutationResponse" is not valid as a type
note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
where the offending line is:
@strawberry.mutation
async def create_user(self: StrawberryField, input: UserCreate) -> UserMutationResponse:
...
Versions:
python==3.10.2
strawberry-graphql==0.97.0
mypy==0.942
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too