Skip to content

Unpacking doesn't do proper type narrowing #17240

Closed as duplicate of#13218
Closed as duplicate of#13218
@RbCaVi

Description

@RbCaVi

Bug Report

When you unpack a tuple with a union type after an if statement that causes narrowing, the unpacked elements have a different type than the elements of the tuple.

To Reproduce

Playground

Expected Behavior

I expected the revealed types of x[0] and y to be the same as each other in all three cases.

Actual Behavior

../test.py:9: note: Revealed type is "Union[tuple[builtins.int], tuple[builtins.float]]"
../test.py:10: note: Revealed type is "builtins.float"
../test.py:11: note: Revealed type is "Union[builtins.int, builtins.float]"

../test.py:20: note: Revealed type is "Union[tuple[Literal['X']], tuple[test.S]]"
../test.py:21: note: Revealed type is "test.S"
../test.py:22: note: Revealed type is "Union[Literal['X'], test.S]"

../test.py:30: note: Revealed type is "tuple[Union[Literal['X'], test.S]]"
../test.py:31: note: Revealed type is "test.S"
../test.py:32: note: Revealed type is "Union[Literal['X'], test.S]"

Your Environment

  • Mypy version used: Github (mypy 1.11.0+dev.35fbd2a852be2c47e8006429afe9b3ad4b1bfac2 (compiled: no))
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions