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

Missing annotation in function passed in a click decorator #3869

Closed
diamond-lizard opened this issue Aug 24, 2017 · 9 comments
Closed

Missing annotation in function passed in a click decorator #3869

diamond-lizard opened this issue Aug 24, 2017 · 9 comments
Labels
bug mypy got something wrong needs discussion

Comments

@diamond-lizard
Copy link

diamond-lizard commented Aug 24, 2017

When I run "mypy --disallow-any unannotated foo.py", mypy tells me that "foo.py:6: error: Function is missing a return type annotation".

Here is the output of running "mypy -v --disallow-any unannotated foo.py"

I'm not sure if this is a bug or if there's some special way I'm supposed to annotate the line mypy is complaining about.

@emmatyping
Copy link
Collaborator

The issue is cli is supposed to be annotated with a return type. This appears to be an issue with the line number of the reported error.

@diamond-lizard
Copy link
Author

diamond-lizard commented Aug 24, 2017

Thank you for your help.

When I annotated the cli function itself with a -> None annotation, the error disappeared. The issue does seem to be with the reported line number, as you say. If the line number had been reported correctly, the necessary annotation would have been obvious.

@emmatyping emmatyping added the bug mypy got something wrong label Aug 24, 2017
@emmatyping
Copy link
Collaborator

I believe we just increment the line number of the last decorator to find the function line number, which is not always accurate. We also cannot decrement from the first statement, as there may be a blank line between the nodes. Looking at the raw information of the ast parsing, I don't believe there is information on the line the annotation should be if it is absent (since the attribute returns is None). This might require a change to typed_ast.

@gvanrossum
Copy link
Member

gvanrossum commented Aug 25, 2017 via email

@emmatyping
Copy link
Collaborator

emmatyping commented Aug 25, 2017

The FunctionDef node has a line number of 4, the line number of the first decorator. The line number of the first def token is 10. However this is also not the logical place, consider:

def foo(a: int,
        b: str): ...

The return should be on line 2, however the lineno of the FunctionDef would be line 1.

@diamond-lizard
Copy link
Author

diamond-lizard commented Aug 25, 2017

When mypy informs me that a "Function is missing a return type annotation", all I'm wondering is "which function?" Telling me which line the def is on would answer that question. From there, I can figure out on my own that the type annotation itself should go after the parenthesis that closes the parameter list, whichever line that might be on.

If mypy pointed to the exact place where the annotation should go it would indeed be a nice bit of sugar, but the essential information seems to be which line the def of the function in question is on.

What confused me about the error pointing to line 6 in the original foo.py program was that it pointed a place in the decorator that contained a function call, so I (and another person helping me out on #python) both wondered if mypy was complaining that it was the function called on line 6 -- click.File() -- that mypy thought was lacked a type annotation. If the error had just pointed to the def instead, there would have been no such confusion.

@emmatyping
Copy link
Collaborator

Yes, I understand the issue, I am unsure of how to solve it internally in mypy. We use the typed_ast module to get the line number information. I don't think the AST exposes the location of the return type, which we want.

Also, in future, if you want help on mypy, you may be interested in the chat https://gitter.com/python/typing . (If you prefer IRC, there is an IRC bridge for gitter). There is almost always someone around who can help out with this.

@gvanrossum
Copy link
Member

gvanrossum commented Aug 25, 2017 via email

fabaff added a commit to home-assistant-ecosystem/home-assistant-cli that referenced this issue Feb 14, 2019
fabaff added a commit to home-assistant-ecosystem/home-assistant-cli that referenced this issue Feb 14, 2019
@hauntsaninja
Copy link
Collaborator

A lot has changed since 2017, e.g. see #6753

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Aug 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong needs discussion
Projects
None yet
Development

No branches or pull requests

4 participants