python / python/mypy

False-positive [attr-defined] when importing classes and functions from a file with the same name as the parent package

Aperta
#19,410 0 commenti 0 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

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

  1. mkdir thebug
  2. cd thebug
  3. touch __init__.py
  4. vim thebug.py and type in the following contents:
class A:
    def __init__(self) -> None:
        self.val = "a"
  1. vim run.py and type in the following contents:
from thebug import A
var: A = A()
print(var.val)
  1. Run it to make sure it works:
$ python3 run.py
a
  1. 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

  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

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.