"Cannot assign multiple types to name" error when using get_dynamic_class_hook
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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:
- it is currently broken for the usage we need (there's an issue for that)
- we want to keep
strawberry.unionas 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?
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, mypy für das Beispiel im Issue mit dem Plugin aus plugin_1.py auszuführen, mit Fokus auf get_dynamic_class_hook und das verzögerte Verhalten von add_symbol_table_node. Vergleiche die Diagnosen und die aufgedeckten Typen, wenn x: MyUnion vor beziehungsweise nach der Zuweisung erscheint, und stelle fest, ob das Ergebnis erwartet wird oder ein mypy/plugin-Fehler ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100