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

Preserve strings inside Literal type annotations #423

Merged
merged 1 commit into from
Mar 26, 2024

Conversation

GeeTransit
Copy link
Contributor

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.

Copy link
Collaborator

@AWhetter AWhetter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for making this change!

@AWhetter
Copy link
Collaborator

The test failure has been addressed in main already.

@AWhetter AWhetter merged commit 0d69974 into readthedocs:main Mar 26, 2024
12 of 15 checks passed
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants