jepperaskdk / pydoctest

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

Missing support for optional argument in numpy format? #50

Closed Risitop closed 1 month ago

Risitop commented 1 month ago

Hi, thank you very much for developing this great tool.

One question though, it seems like pydoctest is right now unable to parse optional arguments:

def f(a: int = 0) -> int:
    """blabla

    Parameters
    ----------
    a: int, optional
        blabla, by default 0

    Returns
    -------
    int
        Adds one to the result
    """
    return a + 1

This , optional keyword being standard in numpy, would you have any tip to avoid this issue and allowing the package to parse functions with optional parameters? Thanks!

jepperaskdk commented 1 month ago

Thanks for posting this. Do you get an error? It might be that the numpy parser attempts to parse the whole int, optional as a type, but instead it should account for this exception.

So as a minimum it should verify the type (int), but I guess it would be cool if it also spotted misplaced ", optional"? Not sure if possible though - I will have a look!

Risitop commented 1 month ago

Hi Jeppe, thanks a lot for this fast answer. I will try to replicate the issue on a minimal example, as it seems to be a little bit more complicated than what I first thought to make a simple failing case.

Edit: ok, so the aforementioned example indeed seems to trigger the following error

/test.py::f FAIL | Unable to parse docstring: 

Corresponding config file:

{
    "fail_on_missing_docstring": true,
    "fail_on_raises_section": false,
    "parser": "numpy",
    "exclude_methods": ["_*"],
    "exclude_functions": ["_*"],
    "verbosity": 1
}

I think a possible simple fix would indeed be to add a handler for a possible ", optional" suffix, so that the numpy parser does not try to parse it as part of the parameter type. :)

And yes, as you suggested it could even go one step further and check that every , optional argument indeed has a set default value in the function definition.

jepperaskdk commented 1 month ago

I've created a PR here: https://github.com/jepperaskdk/pydoctest/pull/51

There's a problem with the code-coverage tool - I will try to resolve it.

jepperaskdk commented 1 month ago

I've disabled codecov since it seems to be broken. I've released pydoctest 0.2.0 which should support optional - can you verify that it solves your problem? :)

Risitop commented 1 month ago

Yes, works well, thanks! 🙏

jepperaskdk commented 1 month ago

Great, thanks for finding this. I'll close this then - let me know if you find something else. 😁