Skip to content

"invalid syntax" with type annotation on return statement #8218

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

Closed
blueyed opened this issue Dec 30, 2019 · 3 comments
Closed

"invalid syntax" with type annotation on return statement #8218

blueyed opened this issue Dec 30, 2019 · 3 comments

Comments

@blueyed
Copy link
Contributor

blueyed commented Dec 30, 2019

Given t-return.py:

from typing import List


def getfoo():
    return ["foo"]


def foo() -> List[str]:
    return getfoo()  # type: List[str]

mypy --show-column-numbers t-return.py shows:

t-return.py:9:31: error: invalid syntax

Note that the reported column is the "i" in "List", which appears to be off in general.

The workaround is to split the return into an assignment (with the annotation), and then returning the variable. But I think it would be nice if the annotation would work with the return statement already.
If that's not going to be supported, the error should be more descriptive probably.

mypy 0.770+dev.9101707bd0c96624d09cb31fe573d7e25c89a35c.dirty

@Cito
Copy link

Cito commented Jan 24, 2020

I get the same error with Python 3.8, but with Python 3.7 I get this:

t-return.py:9:39: error: misplaced type annotation

Both with the same mypy version 0.761.

@adithyabsk
Copy link

One can use the cast operation here but due to the differences in using cast versus the type comment it might be preferable to use the type comment. Thought I'd add a +1 from me as well.

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 19, 2020

Mypy doesn't implement it's own parser, so this would need to be added to CPython for it to be supported. I don't think that it's a likely addition, however, since comment-based type annotations are primarily for backwards compatibility with older Python versions, and the variable annotation syntax introduced in Python 3.6 is recommended for new code.

@JukkaL JukkaL closed this as completed Jun 19, 2020
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

No branches or pull requests

4 participants