python / python/mypy

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

Abierto
#9,627 2 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug topic-plugins
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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?

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza ejecutando mypy sobre el ejemplo del issue con el plugin de plugin_1.py, centrándote en get_dynamic_class_hook y en el comportamiento diferido de add_symbol_table_node. Compara los diagnósticos y los tipos revelados cuando x: MyUnion aparece antes y después de la asignación, y determina si el resultado es el esperado o si se trata de un error de mypy/plugin.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.