Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typing.Literal["some", "string"] types are represented incorrectly in autodocs #9195

Closed
anntzer opened this issue May 9, 2021 · 1 comment

Comments

@anntzer
Copy link
Contributor

anntzer commented May 9, 2021

Describe the bug
Quotes are missing in the representation of typing.Literal types extracted from autodoc typehints.

To Reproduce
Steps to reproduce the behavior:

$ cat >project.py <<EOF
from typing import Literal

def func(x: Literal["a", "b"], y: int):
    """
    :param x: The x.
    :param y: The y.
    """
EOF
sphinx-apidoc . -o . -F -A me -V 0.0 --extensions sphinx.ext.intersphinx
PYTHONPATH=. make O=-Dautodoc_typehints=description html

and open _build/html/project.html

Expected behavior
The Literal type should be represented as Literal["a", "b"], not Literal[a, b].

Your project
N/A

Screenshots
s

Environment info

  • OS: linux
  • Python version: 3.9.4
  • Sphinx version: 4.0.0
  • Sphinx extensions: intersphinx, autodoc
  • Extra tools: N/A

Additional context
N/A

@gousaiyang
Copy link

gousaiyang commented May 10, 2021

I'm having the same issue. Looks like this is because sphinx.util.typing.stringify() function does not correctly handle Literal annotations. According to PEP 586, at type check time Literal only accepts a small set of types: literal ints, byte and unicode strings, bools, Enum values and None. To stringify them we can use repr() (we may handle Enum specially but that looks optional). However Literal can accept any parameters at run time, which may make things more complicated if we want to support such arbitrary parameters for Literal for run time use.

Even if the Literal stringifing bug gets fixed, there is still one more bug to fix to make doc render properly. Variable annotations are not properly escaped (according to reST syntax) when generating reST. Literal['\n', '\r\n', '\r'] is incorrectly rendered like the following HTML:

image

The following test case would be helpful:

from typing import Literal


def func(arg: Literal['\n', '\r\n', '\r']) -> Literal['\n', '\r\n', '\r']:
    """My function.

    Args:
        arg: my arg
    
    Returns:
        my return value
    """
    return arg


#: This is a global variable.
global_var: Literal['\n', '\r\n', '\r'] = '\n'


class Context:
    """My context class."""
    #: This is a class variable defined in class body.
    line_ending: Literal['\n', '\r\n', '\r']

    def __init__(self) -> None:
        """Initialize context."""
        #: This is an attribute defined in __init__().
        self.second_line_ending: Literal['\n', '\r\n', '\r'] = '\r'

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants