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

Improper inference via length for a union of tuples #12816

Closed
Fulguritude opened this issue May 19, 2022 · 1 comment
Closed

Improper inference via length for a union of tuples #12816

Fulguritude opened this issue May 19, 2022 · 1 comment
Labels
feature topic-type-narrowing Conditional type narrowing / binder

Comments

@Fulguritude
Copy link

Bug Report

mypy incorrectly infers a Tuple index out of range error when discriminating between two tuples in a union via their length, even when using a literal int when analyzing the length of the tuple instance.

To Reproduce

from typing import Tuple, Union

Tuple3 = Tuple[str, str, str]
Tuple4 = Tuple[str, str, str, str]
Tuples = Union[Tuple3, Tuple4]

def test_tuples(my_tuple: Tuples) -> None:
    if len(my_tuple) >= 4:
        my_tuple[3]  # misc - Tuple index out of range

Expected Behavior
Not returning an "index out of range" error, when it is clearly impossible for it to be the case.

Actual Behavior
mypy infers misc - Tuple index out of range

Your Environment

  • Mypy version used: 0.950 (compiled: yes)
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.10
  • Operating system and version: pop-os 22.04
@Fulguritude Fulguritude added the bug mypy got something wrong label May 19, 2022
@JelleZijlstra JelleZijlstra added topic-type-narrowing Conditional type narrowing / binder feature and removed bug mypy got something wrong labels May 19, 2022
@JelleZijlstra
Copy link
Member

Duplicate of #1178

@JelleZijlstra JelleZijlstra marked this as a duplicate of #1178 May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

2 participants