stubgen: Class docstring not included in stub when combining `--include-docstrings` with `--inspect-mode`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the documented stubgen --include-docstrings --inspect-mode ./mwe.py reproduction and compare it with the invocation without --inspect-mode. Trace the stubgen CLI handling for --include-docstrings and --inspect-mode; done means the generated stub includes MyClass.__doc__ while preserving the existing method docstrings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100