The Wayback Machine - https://web.archive.org/web/20230105073600/https://github.com/python/mypy/issues/9627
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Cannot assign multiple types to name" error when using get_dynamic_class_hook #9627

Open
patrick91 opened this issue Oct 22, 2020 · 2 comments
Labels
bug mypy got something wrong topic-plugins The plugin API and ideas for new plugins

Comments

@patrick91
Copy link
Contributor

patrick91 commented Oct 22, 2020

Hello there, I'm working on a plugin that allows this syntax:

MyUnion = strawberry.union("Entity", types=(str, int))

to be equivalent of

MyUnion = Union[str, int]

We do this mainly because we need to add some metadata to the union, and we can't use Annotated for two reasons:

  1. it is currently broken for the usage we need (there's an issue for that)
  2. we want to keep strawberry.union as the API as it looks better than the alternative to annotated.

That being said we managed to get a basic plugin working, but it breaks in some cases, here's the first one as I was able to reproduce it in a minimal repo.

We running mypy on this example:

from __future__ import annotations

import strawberry

x: MyUnion

MyUnion = strawberry.union("Entity", types=(str, int))


reveal_type(MyUnion)
reveal_type(x)

we get this error:

example.py:7: error: Cannot assign multiple types to name "MyUnion" without an explicit "Type[...]" annotation
example.py:10: note: Revealed type is 'builtins.object'
example.py:11: note: Revealed type is 'Union[builtins.str, builtins.int]'
Found 1 error in 1 file (checked 1 source file)

if I move x: MyUnion below, everything works as expected, but I still think there's a bug in either mypy or the plugin I'm writing as we managed to get this issue in a different scenario too.

I've set up a repo with the minimal code for this issue:

https://github.com/patrick91/mypy-issue/blob/main/plugin_1.py

I've also tried to defer the add_symbol_table_node to the final iteration, but looks like that did't work.

Is this a bug in mypy or me using it wrong?

@patrick91 patrick91 added the bug mypy got something wrong label Oct 22, 2020
@syastrov
Copy link
Contributor

syastrov commented Oct 22, 2020

It's just a guess, but try adding plugin_generated=True as an argument to your SymbolTableNode

@patrick91
Copy link
Contributor Author

patrick91 commented Oct 22, 2020

@syastrov same issue:

image

I can't remember the reason behing me adding that flag as False I guess it should be true in this case, but looks like it isn't doing much (for this specific use case)

EDIT:

I don't know if it helps, but this is what happens with defer:

image

@AlexWaygood AlexWaygood added the topic-plugins The plugin API and ideas for new plugins label Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-plugins The plugin API and ideas for new plugins
Projects
None yet
Development

No branches or pull requests

3 participants