Imports that shadow names cannot be made to type-check
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
Bug Report
--allow-redefinition does not come into effect for imports that shadow a previous import with incompatible types. Even if the previous name is first del'd the error still persists, whether --allow-redefinition is enabled or not.
I found this during work on https://github.com/python/typeshed/pull/11374#issuecomment-1968999481. The upstream library first does a star-import re-exporting hundreds of names, then shadows at least one of them with a wrapper type. The star-import cannot be rewritten due to the sheer number of imported names, but then the name shadowing cannot be prevented.
To Reproduce
Minimized multi-file example
# $ tree .
# .
# ├── bar.py
# ├── foo.py
# ├── __init__.py
# └── py.typed
# foo.py & bar.py
class Repository:
pass
# __init__.py
from .foo import Repository
del Repository # this doesn't work
from .bar import Repository
# Both fails in the same way:
# $ mypy .
# $ mypy --allow-redefinition .
#
# __init__.py:3: error: Incompatible import of "Repository" (imported name has type "type[p.bar.Repository]", local name has type "type[p.foo.Repository]") [assignment]
# Found 1 error in 1 file (checked 3 source files)
One-file example:
- Gist URL: https://gist.github.com/mypy-play/826f927d282282e8f8d51bc4411b4b94
- Playground URL: https://mypy-play.net/?mypy=master&python=3.12&flags=allow-redefinition&gist=826f927d282282e8f8d51bc4411b4b94
from hashlib import md5 as foo
del foo
from sys import path as foo
Expected Behavior
No errors.
Actual Behavior
main.py:3: error: Incompatible import of "foo" (imported name has type "list[str]", local name has type "Callable[[Buffer, DefaultNamedArg(bool, 'usedforsecurity')], _Hash]") [assignment]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.8.0 & master branch
- Mypy command-line flags: none &
--allow-redefinition - Mypy configuration options from
mypy.ini(and other config files): n/a - Python version used: 3.11 & 3.12
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza reproduciendo el problema con init.py, foo.py y bar.py usando los comandos de mypy mostrados, incluido --allow-redefinition. Rastrea cómo mypy gestiona la primera importación eliminada y el reemplazo incompatible, y después añade una prueba de regresión que cubra ambos ejemplos. Se considera terminado cuando las importaciones pasan la comprobación de tipos sin errores en ambas configuraciones de flags.
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
- Bien especificado
- Aptitud para principiantes
- 35/100