mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Stubgen infers wrong from doctest blocks

Open jbstand opened this issue 3 years ago • 0 comments

Bug Report

(A clear and concise description of what the bug is.) When generating stub for a C extension, the doctest blocks are parsed and creates a wrong overload.

Example :

>>> class example():
...     def init(self):
...        pass
>>> ex = example()
>>> ex.init()
>>> 

To Reproduce

(Write your steps here:)

  1. Clone the following repository : https://github.com/jbstand/stubgen-issue-example
  2. pip install .
  3. Use stubgen to generate stubs from the module : stubgen -m example
  4. Observe init type overloaded by type without self argument from doctest.

Expected Behavior

(Write what you thought would happen.) Stubgen should only take the first line of the docstring which is the type annotation as docstring or understand self is passed as it is for every class method.

Actual Behavior

(Write what happened.) The following two stubs get generated with the second causing typing conflicts because one version doesn't expect self.

from typing import Any

from typing import overload

class example:
    def __init__(self, *args, **kwargs) -> None: ...
    @overload
    def init(self) -> None: ...
    @overload
    def init() -> Any: ...

Your Environment

  • Mypy version used: 0.971
  • Mypy command-line flags: stubgen -m ex_module
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: Python 3.8.5 also tested in latest 3.10 minor as part of a recurrent CI pipeline
  • Operating system and version: Windows 10 and alpine as part of CI

jbstand avatar Sep 21 '22 13:09 jbstand