Skip to content

Missing annotation in function passed in a click decorator #3869

Closed as not planned

Description

@diamond-lizard

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.

Activity

  1. emmatyping commented on Aug 24, 2017

    @emmatyping
    Member

    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.

  2. diamond-lizard commented on Aug 24, 2017

    @diamond-lizard
    Author

    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.

  3. emmatyping commented on Aug 24, 2017

    @emmatyping
    Member

    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.

  4. gvanrossum commented on Aug 25, 2017

    @gvanrossum
    Member
  5. emmatyping commented on Aug 25, 2017

    @emmatyping
    Member

    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.

  6. diamond-lizard commented on Aug 25, 2017

    @diamond-lizard
    Author

    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.

  7. emmatyping commented on Aug 25, 2017

    @emmatyping
    Member

    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.

  8. gvanrossum commented on Aug 25, 2017

    @gvanrossum
    Member
  9. hauntsaninja commented on Aug 3, 2022

    @hauntsaninja
    Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions