python / python/mypy

Stub generation adds too many explicit re-exports

Offen
#18,113 2 Kommentare 1 Reaktion 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

mypy stubgen re-exports more names than needed (despite --export-less). It seems that imports that are only used in function parameter type annotations (see FirstErrorType below) are always re-exported and imports from 'if TYPE_CHECKING' clauses (see SecondErrorType below) as well.

To Reproduce

# example.py
from typing import TYPE_CHECKING
from b import FirstErrorType, WorkingType
if TYPE_CHECKING:
    from c import SecondErrorType

def foobar(one: FirstErrorType, two: "SecondErrorType") -> None:
    pass

def spamandeggs() -> WorkingType:
    return WorkingType()

Expected Behavior

Nether FirstErrorType nor SecondErrorType should be re-exported according to the documentation of the --export-less switch, which states

only export imported names that are not referenced in the module that contains the import.

Actual Behavior

the output of stubgen --export-less example.py

# example.pyi
from b import FirstErrorType as FirstErrorType, WorkingType
from c import SecondErrorType as SecondErrorType

def foobar(one: FirstErrorType, two: SecondErrorType) -> None: ...
def spamandeggs() -> WorkingType: ...

Your Environment

  • Mypy version used: 1.13.0
  • Mypy command-line flags: stubgen --export-less
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10

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

Reproduziere das Problem mit der gezeigten example.py und führe stubgen --export-less aus. Untersuche anschließend die generierte example.pyi. Verfolge stubgens Behandlung von nur für Annotationen verwendeten Imports und Imports unter TYPE_CHECKING; abgeschlossen ist die Aufgabe, wenn FirstErrorType und SecondErrorType keine expliziten Re-Exports sind, während WorkingType weiterhin exportiert wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 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.