pyment icon indicating copy to clipboard operation
pyment copied to clipboard

Type annotations cause converted docstrings to break

Open cubrink opened this issue 4 years ago • 2 comments

Edit:

It seems this has been fixed in this repo, but on the version from PyPi does not include this fix. I was able to install from source to resolve the issue.

I'm trying to convert docstrings from reST to Google format but the output is broken in functions with type annotations.

Example input file foo.py

from typing import Tuple, Optional

# With type annotations (Broken)
def foo(bar: int, baz: Tuple[int, str], qux: Optional[int] = None) -> str:
    """
    Main description.

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis molestie 
    neque ac dolor hendrerit molestie. Integer eu placerat nisl. Ut vel tellus sem. 
    Nunc ornare ultrices quam, at venenatis est blandit sed.

    :param bar: does a bar
    :type bar: int
    :param baz: does a baz
    :type baz: Tuple[int, str]
    :param qux: does a qux
    :type qux: Optional[int]

    :returns: hello world
    :rtype: str
    """
    return "Hello world"


# No type annotations (working)
def bar(bar, baz, qux = None) -> str:
    """
    Main description.

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis molestie 
    neque ac dolor hendrerit molestie. Integer eu placerat nisl. Ut vel tellus sem. 
    Nunc ornare ultrices quam, at venenatis est blandit sed.

    :param bar: does a bar
    :type bar: int
    :param baz: does a baz
    :type baz: Tuple[int, str]
    :param qux: does a qux
    :type qux: Optional[int]

    :returns: hello world
    :rtype: str
    """
    return "Hello world"

Then, we run the command

pyment -i reST -o google -w foo.py

and we get the resulting file:

from typing import Tuple, Optional

# With type annotations (Broken)
def foo(bar: int, baz: Tuple[int, str], qux: Optional[int] = None) -> str:
    """Main description.
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis molestie
    neque ac dolor hendrerit molestie. Integer eu placerat nisl. Ut vel tellus sem.
    Nunc ornare ultrices quam, at venenatis est blandit sed.

    Args:
      bar(int): does a bar
      baz(Tuple[int, str]): does a baz
      qux(Optional[int]): does a qux
      bar: int: 
      baz: Tuple[int: 
      str]: 
      qux: Optional[int]:  (Default value = None)

    Returns:
      str: hello world

    """
    return "Hello world"


# No type annotations (working)
def bar(bar, baz, qux = None) -> str:
    """Main description.
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis molestie
    neque ac dolor hendrerit molestie. Integer eu placerat nisl. Ut vel tellus sem.
    Nunc ornare ultrices quam, at venenatis est blandit sed.

    Args:
      bar(int): does a bar
      baz(Tuple[int, str]): does a baz
      qux(Optional[int], optional): does a qux (Default value = None)

    Returns:
      str: hello world

    """
    return "Hello world"

In the resulting file the type annotated function has extra lines added for each parameter. Additionally, the default value is placed on the extraneous line.

cubrink avatar May 26 '21 15:05 cubrink

I have the same problem for the pyment installed in conda.

andreikee avatar Sep 24 '21 20:09 andreikee

Problem still persist, but I am not sure if pyment is currently under active development (see #111)

stangier avatar May 16 '22 18:05 stangier