python / python/mypy

False-positive when using Union[Tuple[...]]

Open
#9,791 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report / To Reproduce

I have a incorrect Unsupported operand types for + ("int" and "str") when using Union[Tuple[...]].

To Reproduce

Have a look at this example:

from typing import Union, Tuple, List

variable: List[Union[Tuple[int, int], Tuple[str, str]]] = []

element1 = variable[0]

print(element1[0] + element1[1])

Run mypy.

Expected Behavior

No warning.

Actual Behavior

I have:

Unsupported operand types for + ("int" and "str")

But it's wrong due to the fact that element1 is a Union (right) but it's the same variable on both side. So both type on the left and the right of the "+" should be the same.

Your Environment

  • Mypy version used: mypy 0.790
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
python_version = 3.8
ignore_missing_imports = True
follow_imports = silent
show_column_numbers = True
disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
check_untyped_defs = True
no_implicit_optional = True
warn_redundant_casts = True
warn_unused_ignores = True
warn_unreachable = True
strict_equality = True
  • Python version used: Python 3.7.3
  • Operating system and version: Windows 10 20H2 (19042.630)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running mypy on the reported Python reproducer with the stated Python 3.8 configuration and confirm the false positive. Trace how the type checker handles repeated indexing of a Union of tuples, then verify that the example produces no warning while preserving correct errors for genuinely incompatible operands.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.