You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Missing annotation in function passed in a click decorator #3869
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.
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.
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:
deffoo(a: int,
b: str): ...
The return should be on line 2, however the lineno of the FunctionDef would be line 1.
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.
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.
Dang. I didn't remember the Python AST doesn't have a separate node for the
'def' (it exists in the grammar but it's optimized out in the AST). Can you
file a new mypy issue and one in typed_ast?
Activity
emmatyping commentedon Aug 24, 2017
The issue is
cliis supposed to be annotated with a return type. This appears to be an issue with the line number of the reported error.diamond-lizard commentedon Aug 24, 2017
Thank you for your help.
When I annotated the
clifunction itself with a-> Noneannotation, 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 commentedon Aug 24, 2017
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
returnsisNone). This might require a change to typed_ast.gvanrossum commentedon Aug 25, 2017
emmatyping commentedon Aug 25, 2017
The
FunctionDefnode has a line number of 4, the line number of the first decorator. The line number of the firstdeftoken is 10. However this is also not the logical place, consider:The return should be on line 2, however the
linenoof theFunctionDefwould be line 1.diamond-lizard commentedon 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 commentedon Aug 25, 2017
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 commentedon Aug 25, 2017
Add annotation
[Update help output (fixes #173)
hauntsaninja commentedon Aug 3, 2022
A lot has changed since 2017, e.g. see #6753