python / python/mypy

stubgen cannot infer precise literal string types from docstring

Open
#13,996 0 comments 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

I'm using stubgen to generate pyi files for a python c extension module.
Some functions take arguments of type Literal["xxx", "yyy"]; such constraints are written in the docstring of those functions.
After stubgen, these annotations are missing; however this only happens when the arguments of Literal contain str or bytes type.
This problem can be reproduced using infer_sig_from_docstring (see below).

To Reproduce

from mypy.stubdoc import infer_sig_from_docstring

# literal int and bool works as expected
print(infer_sig_from_docstring("f(x: Literal[42]) -> None", "f"))
print(infer_sig_from_docstring("f(x: Literal[False]) -> None", "f"))

# doesn't work
print(infer_sig_from_docstring("f(x: Literal['4_2']) -> None", "f"))
print(infer_sig_from_docstring('f(x: Literal["4_2"]) -> None', "f"))
print(infer_sig_from_docstring("f(x: Literal[b'4_2']) -> None", "f"))
print(infer_sig_from_docstring('f(x: Literal[b"4_2"]) -> None', "f"))

Expected Behavior

[FunctionSig(name='f', args=[ArgSig(name='x', type='Literal[42]', default=False)], ret_type='None')]
[FunctionSig(name='f', args=[ArgSig(name='x', type='Literal[False]', default=False)], ret_type='None')]
[FunctionSig(name='f', args=[ArgSig(name='x', type='Literal["4_2"]', default=False)], ret_type='None')]
[FunctionSig(name='f', args=[ArgSig(name='x', type='Literal["4_2"]', default=False)], ret_type='None')]
[FunctionSig(name='f', args=[ArgSig(name='x', type='Literal[b"4_2"]', default=False)], ret_type='None')]
[FunctionSig(name='f', args=[ArgSig(name='x', type='Literal[b"4_2"]', default=False)], ret_type='None')]

Actual Behavior

[FunctionSig(name='f', args=[ArgSig(name='x', type='Literal[42]', default=False)], ret_type='None')]
[FunctionSig(name='f', args=[ArgSig(name='x', type='Literal[False]', default=False)], ret_type='None')]
[FunctionSig(name='f', args=[ArgSig(name='x', type=None, default=False)], ret_type='None')]
[FunctionSig(name='f', args=[ArgSig(name='x', type=None, default=False)], ret_type='None')]
[FunctionSig(name='f', args=[ArgSig(name='x', type=None, default=False)], ret_type='None')]
[FunctionSig(name='f', args=[ArgSig(name='x', type=None, default=False)], ret_type='None')]

Your Environment

  • Mypy version used: 0.982
  • Python version used: 3.10.8

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 the reported infer_sig_from_docstring entry point and reproduce the six examples from the issue. Trace how Literal annotations are parsed, then add coverage for string and bytes values so the returned signatures preserve the expected Literal types.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.