mypy gets confused between non-re-exported import and a submodule of the same name
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
This is happening in opencv, relevant issue: https://github.com/opencv/opencv-python/issues/901
Type stub cv2 imports typing
Module cv2.typing also exists
The following raises a mypy error, as expected
import cv2
_ = cv2.typing.MatLike # Module "cv2" does not explicitly export attribute "typing" Mypy [attr-defined]
(note that this will work at runtime, by that's because cv2 doesn't actually import typing at runtime AND does dynamic imports to expose all its submodules, so mypy is correct here in terms of static type-checking)
The following works, and is accepted by mypy, as expected
import cv2.typing
_ = cv2.typing.MatLike
The issue comes from importing both:
import cv2
import cv2.typing
Note that pyright handles this case correctly.
(A clear and concise description of what the bug is.)
To Reproduce
Download the reproduction, then run mypy . at the root. See error
Run python test.py, see the symbol printed out.
Minimal repro.zip
Expected Behavior
No error in repro
Actual Behavior
PS C:\Users\Avasam\Desktop\Minimal repro> mypy .
test.py:4: error: Module has no attribute "Foo" [attr-defined]
Found 1 error in 1 file (checked 3 source files)
Your Environment
- Mypy version used: mypy 1.5.1 (compiled: yes)
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: Python 3.9.13
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
Estrai il file Minimal repro.zip collegato ed esegui prima mypy . dalla sua radice, quindi esegui python test.py per confermare il comportamento a runtime. Traccia il modo in cui mypy gestisce import cv2, import cv2.typing e il successivo accesso a cv2.typing.MatLike. Il lavoro è completato quando la riproduzione non segnala errori, preservando al contempo il risultato atteso a runtime.
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
- 45/100