jepperaskdk / pydoctest

Python docstring signature verification
MIT License
20 stars 3 forks source link

Handling overloaded functions? #54

Open Risitop opened 2 months ago

Risitop commented 2 months ago

Pydocstest does not seem to be able to handle @typing.overload-ed functions right now. According to Ruff, only the actual implementation should be endowed with a docstring:

https://docs.astral.sh/ruff/rules/overload-with-docstring/

Currently, pydoctest does not seem to be able to handle this case:

import typing

@typing.overload
def timestwo(x: int) -> int: ...

@typing.overload
def timestwo(x: float) -> float: ...

def timestwo(x):
    """blabla

    Parameters
    ----------
    x: int | float
        Input scalar

    Returns
    -------
    int | float
        x times two.
    """
    return 2 * x

$ pydoctest --parser numpy -> /test.py::timestwo FAIL | Return type differ. Expected (from signature) <class 'inspect._empty'>, but got (in docs) int | float.

I think that given the tool logic, those multi-definition cases will be hard to handle. In the case it would be too difficult, I think being able to ignore checking for a specific docstring with a kind of # nopydoctest option would be acceptable for these rare occurences.

Thanks!

jepperaskdk commented 2 months ago

Thanks for posting this. Is it on purpose that the final implementation is untyped? Does your linter/typechecker know the type of x and enforce the return type in the last implementation function?

Risitop commented 2 months ago

Indeed, this pattern allows the typechecker to pick the signature that matches the arguments type in order to infer the correct return type :)