python / python/mypy

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

Aperta
#9,627 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-plugins
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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?

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo mypy sull'esempio nell'issue con il plugin di plugin_1.py, concentrandoti su get_dynamic_class_hook e sul comportamento differito di add_symbol_table_node. Confronta le diagnosi e i tipi rivelati quando x: MyUnion compare prima rispetto a dopo l'assegnazione, e stabilisci se il risultato è previsto o se si tratta di un bug di mypy/plugin.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.