python / python/mypy

Imports that shadow names cannot be made to type-check

Aperta
#16,972 2 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

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:

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

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 riproducendo il problema con init.py, foo.py e bar.py usando i comandi mypy mostrati, incluso --allow-redefinition. Traccia come mypy gestisce il primo import eliminato e la sostituzione incompatibile, quindi aggiungi un test di regressione che copra entrambi gli esempi. Il lavoro è completato quando gli import vengono verificati dal punto di vista dei tipi senza errori in entrambe le configurazioni dei flag.

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
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.