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

feature request/idea: tuple length inference from assertions/tests on len() #9120

Closed
aecay opened this issue Jul 9, 2020 · 2 comments
Closed

Comments

@aecay
Copy link
Contributor

aecay commented Jul 9, 2020

Consider this code:

from typing import List, Any, Tuple

def fn_returning_sequence() -> List[Any]:
    return [1, 2, 3]

x = tuple(fn_returning_sequence())
assert len(x) == 3
reveal_type(x)

It would be nice if mypy's type inference was extended so that it knew that x was a Tuple[Any, Any, Any] rather than the less specific Tuple[Any, ...]. And similarly for cases like if len(x) == 2: ...; in the body of the if mypy can be sure that x is a Tuple[Any, Any].

(Confusingly, the actual value printed by reveal_type in the example is builtins.tuple[Any], but as far as I can tell this is a synonym of Tuple[Any, ...])

@hauntsaninja
Copy link
Collaborator

Similar to #7509 and #8733

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 22, 2020

Closing as duplicate of #8733.

@JukkaL JukkaL closed this as completed Jul 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants