Skip to content

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

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.

Activity

  1. JelleZijlstra commented on May 31, 2022

    @JelleZijlstra
    Member

    Wow, that's pretty bad. It reproduces on all versions in the playground. It happens only with the res = None assignment, so I guess it has something to do with partial types.

  2. added 2 commits that reference this issue on Jan 10, 2023
    fc29b93
    d7a8c04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions