Skip to content

Can't determine the length of a Tuple from a Union type properly #9513

Closed
@skytreader

Description

@skytreader

Bug Report

For a Union type of two possible Tuple lengths, mypy reports index out of range for Tuples of the longer type even with a proper explicit check.

To Reproduce

from typing import Tuple, Union

SpamType = Union[
    Tuple[str, str, str],
    Tuple[str, str]
]

def egg(spam: SpamType) -> str:
    if len(spam) == 3:
        return spam[2]
    else:
        return spam[1]

print(egg(("a", "b")))
print(egg(("a", "b", "c")))

Expected Behavior

The code listing above should be perfectly typed.

Actual Behavior

mypy reports,

mypybug.py:10: error: Tuple index out of range
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used:
mypy==0.782
mypy-extensions==0.4.3
  • Mypy command-line flags: --ignore-missing-imports
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.7.0
  • Operating system and version: Ubuntu 16.04.7 LTS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions