python / python/mypy

bug inferring signatures from docstrings in stubgenc

Open
#11,092 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-stubgen
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

When generating stubs from c modules, stubgen uses mypy.stubdoc to parse signatures from docstrings, if they are present, however, this does not work if the function takes no arguments

To Reproduce

Function with args:

>>> from mypy.stubdoc import infer_sig_from_docstring
>>> infer_sig_from_docstring('func(x) -> int', 'func')
[FunctionSig(name='func', args=[ArgSig(name='x', type=None, default=False)], ret_type='int')]

Function without args:

>>>: infer_sig_from_docstring('func() -> int', 'func')
[]  # <--- no signature!!!!

The fact that no signatures are returned means that the stubs that are generated look like this:

def func(*args, **kwargs) -> Any
    ...

But given that we have a docstring with a valid signature, the stubs should look like this:

def func() -> int
    ...

This seems like a bug. I'll make a PR to fix this when I have some time.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with mypy.stubdoc.infer_sig_from_docstring and reproduce the two examples from the issue, then trace how stubgen uses the returned signatures. Done means a no-argument docstring such as func() -> int yields a signature and stubgen produces the corresponding typed function stub instead of an *args/**kwargs fallback.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.