python / python/mypy

stubgen: Class docstring not included in stub when combining `--include-docstrings` with `--inspect-mode`

Offen
#16,543 6 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

I'm using stubgen to generate stubs for an untyped package. Because the docstrings are assembled at runtime, I want to include them in the stubs such that IDEs using static type checkers can use them. I therefore invoke stubgen with the --include-docstrings and --inspect-mode parameters. The generated stubs do not include docstrings for classes (i.e., MyClass.__doc__), while docstrings for methods and properties are correctly added to the stubs.

If I remove the --inspect-mode switch, the class docstrings are added to the stubs as expected. But this isn't a viable solution for me, as I need to use inspect mode to generate the full docstrings.

To summarize, there seems to be an interaction between --include-docstrings and --inspect-mode, which causes class docstrings in MyClass.__doc__ to get lost.

To Reproduce

# mwe.py

class MyClass:
    """My test class."""

    def __init__(self):
        pass

    def f(self):
        """My test function."""
        pass

Run:

stubgen --include-docstrings --inspect-mode ./mwe.py

Expected Behavior

The type stub contains the class docstring.

class MyClass:
    """My test class."""
    def __init__(self) -> None: ...
    def f(self) -> None:
        """My test function."""

Actual Behavior

The type stub does not contain the class docstring.

class MyClass:
    def __init__(self) -> None: ...
    def f(self):
        """My test function."""

The expected stub can be produced by omitting the --inspect-mode parameter, i.e., by invoking:

stubgen --include-docstrings ./mwe.py

Your Environment

  • Mypy version used: 1.7.0
  • stubgen command-line flags: --include-docstrings --inspect-mode
  • Python version used: 3.12.0

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

Führe die dokumentierte Reproduktion stubgen --include-docstrings --inspect-mode ./mwe.py aus und vergleiche sie mit dem Aufruf ohne --inspect-mode. Verfolge die CLI-Verarbeitung von stubgen für --include-docstrings und --inspect-mode; erledigt ist die Aufgabe, wenn der generierte Stub MyClass.__doc__ enthält und die vorhandenen Methoden-Docstrings bewahrt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
cli, 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.