Skip to content

Type checking fails to distribute across Tuple types #18922

Open
@bcmills

Description

@bcmills

Bug Report

Union types fail to distribute across Tuple types as they ought to when the set of concrete values of each type are identical.

To Reproduce

(https://mypy-play.net/?mypy=latest&python=3.12&gist=9d0052b0ed30856e5ad3985237f8754b)

from typing import Union, Tuple, Optional

def id(x: Tuple[float, Optional[float]]) -> Union[Tuple[float, float], Tuple[float, None]]:
    return x

Expected Behavior

Values of type Tuple[float, Optional[float]] and Union[Tuple[float, float], Tuple[float, None]] should be mutually compatible.

That is: Union ought to distribute across Tuple.

Actual Behavior

main.py:4: error: Incompatible return value type (got "tuple[float, float | None]", expected "tuple[float, float] | tuple[float, None]")  [return-value]

Your Environment

(See Playground link.)

  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions