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

BUG: Return literal with double quotes incorrectly raises v203 #105

Closed
ShivMunagala opened this issue Dec 19, 2023 · 3 comments · Fixed by #123
Closed

BUG: Return literal with double quotes incorrectly raises v203 #105

ShivMunagala opened this issue Dec 19, 2023 · 3 comments · Fixed by #123

Comments

@ShivMunagala
Copy link

To reproduce, use the following code in ./tests/data/playground.py and run pytest.

from typing import Literal

def testLiteral() -> Literal["foo"]:
    """
    Test literal.

    Returns:
        Literal["foo"]: Return literal string foo.
    """
    return "foo"

It raises the following error:
DOC203: Function `testLiteral` return type(s) in docstring not consistent with the return annotation. Return annotation types: ["Literal[\'foo\']"]; docstring return section types: [\'Literal["foo"]\']

The issue seems to stem from the fact that when the Python AST unparses code it uses single quotes by default:

>>> code = 'def testLiteral() -> Literal["a"]:\n\treturn "a"'
>>> ast.unparse(ast.parse(code))
"def testLiteral() -> Literal['a']:\n    return 'a'"

Some code formatters like black replace single quotes with double, which will result in violation 203 being thrown. A temporary work around for users is continuing to use the double quotes in the return but using single quotes in the docstring, i.e.

from typing import Literal

def testLiteral() -> Literal["foo"]:
    """
    Test literal.

    Returns:
        Literal['foo']: Return literal string foo.
    """
    return "foo"

as black doesn't touch the single quotes in the docstring. But I'm not a fan of the (slight) inconsistency between docstring and return annotation 😛

@jsh9
Copy link
Owner

jsh9 commented Jan 2, 2024

Hi, thanks for reporting this! I've been quite busy these past few weeks, and I'll try to get to it in the next 1-2 weeks.

@jsh9
Copy link
Owner

jsh9 commented Jan 29, 2024

Sorry for the delay. I'll try my best to look into it in the next few weeks.

@jsh9
Copy link
Owner

jsh9 commented Feb 17, 2024

Hi @ShivMunagala , the fix was published as version 0.4.1.

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 a pull request may close this issue.

2 participants