Skip to content

Incorrect type narrowing when unpacking union of tuple on an existing variable #12915

@nils-van-zuijlen

Description

@nils-van-zuijlen

Bug Report

When unpacking a tuple with Union types over existing variables, some of the types disappear.

To Reproduce

import random


def foo() -> tuple[int, None] | tuple[int, str]:
    if random.choice((True, False)):
        return 0, None
    return 5, "thing"


res = None

status, res = foo()
reveal_type(res)  # builtins.str

Expected Behavior

res should be an Optional[str], not a str.

Actual Behavior

The Optional gets lost in the assignment.

Your Environment

  • Mypy version used: 0.950
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.10
  • Operating system and version: Manjaro Linux x86_64 5.15.41-1-MANJARO

This may be related to #9731, but I'm not sure about it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions