Skip to content

Invalid definition of Tuple in return reports on a confusing line number #3177

Closed

Description

@daveisfera

Here's an example:

class TestTuple:
    @staticmethod
    def test(a: str) -> (bool, str):
        return len(a) < 2, a[:2]

And here's the output (I'd expect it to say Line #3):

test_invalid_tuple.py:2: error: Invalid tuple literal type

Activity

  1. ilevkivskyi commented on Apr 14, 2017

    @ilevkivskyi
    Member

    This may be because mypy's parser considers decorator a part of the function definition and assigns the function type the first line of the function definition.

  2. daveisfera commented on Apr 14, 2017

    @daveisfera
    Author

    Yes, I assumed that that was the case but for someone like me that's new to typing in Python and using mypy to verify it, it was confusing. Now that I now that you're supposed to do Tuple(bool, str) and that (bool, str) is invalid I would know how to fix it. But it took me a few minutes of thinking "there's not even a tuple on that line" before I realized what I was supposed to do.

    On a related note, it is also confusing that you can't using the normal tuple syntax for typing.

  3. gvanrossum commented on Apr 15, 2017

    @gvanrossum
    Member

    mypy's parser considers decorator a part of the function definition and assigns the function type the first line of the function definition.

    Yes, that's why, and I agree it would be better to pin-point errors to the specific line where they occur.

    it is also confusing that you can't using the normal tuple syntax for typing.

    Presuming you're referring to the idea of writing (int, str) instead of Tuple[int, str], that ship has sailed -- there are some edge cases here that wouldn't work well (unfortunately I don't have time to rehash the discussion, it's probably in one of the mega-threads we had in 2014/2015 about PEP 484.

  4. patrickw276 commented on May 22, 2017

    @patrickw276
    Contributor

    I'm going to take a stab at this issue.

  5. added a commit that references this issue on May 22, 2017
    3602a95
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions