readthedocs / sphinx-autoapi

A new approach to API documentation in Sphinx.
https://sphinx-autoapi.readthedocs.io/
MIT License
415 stars 126 forks source link

Preserve strings inside Literal type annotations #423

Closed GeeTransit closed 3 months ago

GeeTransit commented 4 months ago

Hello,

I really appreciate the work put into this extension. It's very easy to set up and to customize.

Currently, if one has a type annotation with Literal, the strings inside are treated as if they were forward references, rather than constant strings.

Given this function:

def func(arg: Literal['one', 'two', 'three']) -> str:
    return arg

The generated documentation is this:

func(arg: Literal[one, two, three]) → str

Rather than what would be expected:

func(arg: Literal['one', 'two', 'three']) → str

I changed astroid_utils._resolve_annotation to check if the subscript value is Literal and if so, return .as_string() for constants such as strings and bytes. I also added a few tests and a news fragment.

AWhetter commented 3 months ago

The test failure has been addressed in main already.