python / python/mypy

Inconsistent module import behavior when `follow_imports=skip` and __init__ files are omitted from check list

Offen
#10,009 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
Ø Merge
1 T. 18 Std.
Gemergte PRs (30 T.)
54

Beschreibung

Bug Report

Inconsistent module import behavior when follow_imports=skip and files are passed explicitly on the command line

To Reproduce

mypy.ini

[mypy]
follow_imports = skip

a.py

import mod.b
reveal_type(mod.b.UserId)
from mod.b import UserId
reveal_type(UserId)
import sib
reveal_type(sib.Sib)
from sib import Sib
reveal_type(Sib)

sib.py

class Sib:
    pass

mod/b.py

class UserId:
    pass

and a blank mod/__init__.py

nipunn-mbp:mypy_repro nipunn$ mypy a.py sib.py mod/b.py
a.py:2: note: Revealed type is 'Any'
a.py:4: note: Revealed type is 'def () -> mod.b.UserId'
a.py:7: note: Revealed type is 'def () -> sib.Sib'
a.py:9: note: Revealed type is 'def () -> sib.Sib'

nipunn-mbp:mypy_repro nipunn$ tree .
.
├── a.py
├── mod
│   ├── __init__.py
│   └── b.py
├── mypy.ini
└── sib.py
1 directory, 5 files

notably, you get the expected behavior when __init__.py is passed explicitly

nipunn-mbp:mypy_repro nipunn$ mypy .
a.py:2: note: Revealed type is 'def () -> mod.b.UserId'
a.py:4: note: Revealed type is 'def () -> mod.b.UserId'
a.py:7: note: Revealed type is 'def () -> sib.Sib'
a.py:9: note: Revealed type is 'def () -> sib.Sib'
nipunn-mbp:mypy_repro nipunn$ mypy a.py sib.py mod/b.py mod/__init__.py
a.py:2: note: Revealed type is 'def () -> mod.b.UserId'
a.py:4: note: Revealed type is 'def () -> mod.b.UserId'
a.py:7: note: Revealed type is 'def () -> sib.Sib'
a.py:9: note: Revealed type is 'def () -> sib.Sib'

Expected Behavior

It appears inconsistent that mypy would believe in mod.b.UserId while not believing in mod.b under these circumstances - giving different behavior for UserId depending on how it is imported. Even with follow_imports=skip - I would expect __init__.py files to be followed when a file within is explicitly declared.

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

Beginne damit, die Reproduktion mit mypy.ini, a.py, sib.py, mod/b.py und mod/init.py auszuführen, und vergleiche anschließend die im Bericht angezeigten expliziten Dateilisten. Verfolge, wie explizit geprüfte Module und Paketdateien init.py mit follow_imports=skip behandelt werden. Abgeschlossen ist die Aufgabe, wenn die offengelegten Typen unabhängig davon konsistent sind, ob mod/init.py explizit aufgelistet ist oder nicht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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