python / python/mypy

mypy gets confused between non-re-exported import and a submodule of the same name

Offen
#16,216 0 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Extrahiere die verknüpfte Minimal repro.zip und führe zuerst mypy . aus ihrem Stammverzeichnis aus, dann python test.py, um das Laufzeitverhalten zu bestätigen. Verfolge, wie mypy import cv2, import cv2.typing und den späteren Zugriff auf cv2.typing.MatLike behandelt. Erledigt ist die Aufgabe, wenn die Reproduktion keinen Fehler meldet und dabei das erwartete Laufzeitergebnis erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.