False-positive [attr-defined] when importing classes and functions from a file with the same name as the parent package
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
Suppose you have the following directory tree:
thebug/
thebug/run.py
thebug/__init__.py
thebug/thebug.py
Attempting to import anything from thebug.py (using from thebug import whatever) will cause mypy to complain error: Module "thebug" has no attribute "whatever" [attr-defined].
To Reproduce
mkdir thebugcd thebugtouch __init__.pyvim thebug.pyand type in the following contents:
class A:
def __init__(self) -> None:
self.val = "a"
vim run.pyand type in the following contents:
from thebug import A
var: A = A()
print(var.val)
- Run it to make sure it works:
$ python3 run.py
a
- Type-check it:
mypy run.py
Actual Behavior
$ mypy run.py
run.py:1: error: Module "thebug" has no attribute "A" [attr-defined]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.16.1
- Mypy command-line flags: none, as shown above
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.12.3
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci il report utilizzando i file mostrati thebug/init.py, thebug/thebug.py e run.py, quindi esegui mypy run.py. Traccia come viene risolto l’import da thebug quando il pacchetto e il modulo condividono lo stesso nome; il lavoro è completo quando l’esempio supera il controllo dei tipi senza il falso positivo, mantenendo al contempo il controllo degli attributi segnalato.
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
- Abbastanza chiara
- Idoneità per principianti
- 45/100