Skip to content

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

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
daveisfera opened this issue Apr 14, 2017 · 4 comments
Closed
Labels

Comments

@daveisfera
Copy link

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
@ilevkivskyi
Copy link
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.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-1-normal labels Apr 14, 2017
@daveisfera
Copy link
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.

@gvanrossum
Copy link
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.

@patrickw276
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants