autoDocstring icon indicating copy to clipboard operation
autoDocstring copied to clipboard

Incorrect usage of "optional" in argument type

Open willemvdb42 opened this issue 2 years ago • 4 comments

When writing the following function

def my_function(arg: int = 3) -> int:
    return arg

autoDocstring generates the following docstring automatically (using Google style docstrings):

def my_function(arg: int = 3) -> int:
    """
    [summary]

    Args:
        arg (int, optional): [description]. Defaults to 3.

    Returns:
        int: [description]
    """
    return arg

The type for arg is incorrect. It is not optional. Optional does not mean there is a default value. Optional means the argument can also be None. So if the type of arg is (int, optional), then the corresponding type hint would have to be Optional[int] or Union[int, None], which is not the case here.

See https://docs.python.org/3/library/typing.html#special-forms for more info. (Look for Optional right below.)

Ps: I'm using v0.4.1 on vscode. (Which vscode is somehow assuring me is the latest version.)

willemvdb42 avatar Mar 10 '22 14:03 willemvdb42

Yes I always have this issue and would be great to see it fixed! 😄

As @willemvdb42 mentions the (<type>, optional) in the google docstring style examples is because the param is either None or the other type, not that it has a default (although often None is used as a default arg).

I use the darglint tool to detect discrepancies between the type hints and the docstring and I always have to fix this error manually: image

jpy-git avatar Mar 19 '22 18:03 jpy-git

Perhaps this is a different issue, but Optional[str] = None also becomes (Optional[str], optional) - I guess it is redundant

jepperaskdk avatar Oct 06 '22 14:10 jepperaskdk

Perhaps this is a different issue, but Optional[str] = None also becomes (Optional[str], optional) - I guess it is redundant

I am having the same issue, responding to the thread to ensure it stays open.

dvorst avatar May 02 '23 07:05 dvorst

Still same issue here with latest autodocstring + darglint2.

LambdaFlipper avatar Nov 28 '23 15:11 LambdaFlipper