Skip to content

failure to simplify Iterable[T] & tuple[object, ...] to tuple[T, ...] #3890

Description

@h3har

ty 0.0.55 (42869400d 2026-06-26) does not narrow an Iterable[T] type to tuple[T, ...] when an instance check is performed against tuple. I assume this also demonstrates a more general issue of simplifying similar intersections.

ty example

from typing import Iterable, reveal_type

def f(values: Iterable[int]):
    if isinstance(values, tuple):
        reveal_type(values) # revealed: Iterable[int] & tuple[object, ...]
    else:
        reveal_type(values) # revealed: Iterable[int] & ~tuple[object, ...]

pyright example

On the other hand, pyright reveals the following types:

from typing import Iterable, reveal_type

def f(values: Iterable[int]):
    if isinstance(x, tuple):
        reveal_type(x) # revealed: tuple[int, ...]
    else:
        reveal_type(x) # revealed: Iterable[int]

Why this is a problem

Here is a simplified example of a downstream false positive type error that I encountered:

from typing import Iterable, reveal_type

def g(values: Iterable[int]):
    if not isinstance(values, tuple):
        values = tuple(values)
    reveal_type(values[1:-1]) # revealed: tuple[object, ...]
    return (0, *(x + 1 for x in values[1:-1]), 0)

ty output:

info[revealed-type]: Revealed type
 --> test.py:6:17
  |
6 |     reveal_type(values[1:-1]) # revealed: tuple[object, ...]
  |                 ^^^^^^^^^^^^ `tuple[object, ...]`
  |

error[unsupported-operator]: Unsupported `+` operation
 --> test.py:7:18
  |
7 |     return (0, *(x + 1 for x in values[1:-1]), 0)
  |                  -^^^-
  |                  |   |
  |                  |   Has type `Literal[1]`
  |                  Has type `object`
  |

Found 2 diagnostics

Metadata

Metadata

Assignees

Labels

narrowingrelated to flow-sensitive type narrowingset-theoretic typesunions, intersections and more

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions