python / python/mypy

Spurious redef warning in an odd situation with type(None) plus TypeAlias plus a later # type: ignore

Abierto
#17,593 0 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

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

Descripción

Really bizarre behavior here that I can't figure out. Probably not high priority as it's likely an extreme edge case.

This code:

from typing import TypeAlias

abc: TypeAlias = type(None)

x = y # type: ignore

generates the following error in mypy 1.10.1 running on Python 3.11.8:

tt.py:3: error: Name "abc" already defined on line 3  [no-redef]
Found 1 error in 1 file (checked 1 source file)

This is obviously wrong, as abc is not a redefinition (and you can rename it to whatever you want, you get the same error).

This error does NOT happen if you do any of the following:

  1. Use type instead of TypeAlias as the declared type of abc.
  2. Use any value other than type(None) as the value of the expression (if you use type(5), for example, you get the slightly more reasonable although still inaccurate error message that the expression isn't a valid type; I think it's reasonable to expect type literals only rather than at-runtime types though, so this is understandable.
  3. If you don't include the last line which generates a second mypy error (in that case, you get NO error on the abc line, not even the "not a valid type" error you'd get for assigning to type(5)).

The presence of the # type: ignore comment is optional as it turns out, it doesn't matter whether the second error is generated or suppressed, just that there is a second error in the file.

What should happen? IDK fix the underlying bug here. Probably we should get a "not a valid type" error similar to the type(5) situation, and of course that should happen regardless of whether there are or aren't errors elsewhere in the file.

Workaround: import NoneType from the types module is probably cleanest.

I'll mention here for others who might find this looking for how to use NoneType as a type annotation that you'll get a warning from mypy if you use NoneType as a type hint, saying you should use None instead (and yo should probably do that). The code that inspired this used NoneType not as a type hint but for a runtime isinstance check.

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 reproduciendo el problema con el fragmento mínimo de Python del informe usando mypy 1.10.1 y, después, compara el comportamiento con y sin TypeAlias, type(None) y la asignación posterior ignorada. Sigue el procesamiento del alias y de la línea posterior que produce el error; se considera terminado cuando no aparece ningún error no-redef espurio y el diagnóstico invalid-type es coherente independientemente de los demás errores.

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

Evaluación

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.