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

Narrow down tuple types with len check #14019

Closed
ikamensh opened this issue Nov 6, 2022 · 1 comment
Closed

Narrow down tuple types with len check #14019

ikamensh opened this issue Nov 6, 2022 · 1 comment
Labels

Comments

@ikamensh
Copy link

ikamensh commented Nov 6, 2022

Feature
I wish mypy understood that following code is actually correct:

def foo(x: tuple[str, str] | tuple[str, str, str]):
  if len(x) == 2:
     obs, info = x  # error: Too many values to unpack (2 expected, 3 provided)  [misc]
  ...

Pitch

Tuples are universal way to return multiple values in python, and are ubiquitous. It's natural to process cases where data can come in different number of elements with length checks. Currently I could write a custom type guard function (is boilerplate and extra effort) or # type: ignore (but I lose benefit of type checking here)

pyright understands this case, and I wish mypy did as well.

@AlexWaygood
Copy link
Member

Duplicate of #1178

@AlexWaygood AlexWaygood marked this as a duplicate of #1178 Nov 6, 2022
@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Nov 6, 2022
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

2 participants