Skip to content

For tuple in match, _ isn't determined to be Never, but _, _ is #18792

Open
@wyattscarpenter

Description

@wyattscarpenter

Bug Report

Consider the following:

from typing import Literal, assert_never
def example(alternate_content: tuple[Literal['normal', 'analyze',], str]) -> None:
    match alternate_content:
      case 'normal', payload:
        pass
      case 'analyze', payload:
        pass
      case _:
        assert_never(alternate_content)

This fails with main.py:9: error: Argument 1 to "assert_never" has incompatible type "tuple[Never, Never]"; expected "Never" [arg-type]. Fair enough, I suppose! But the following code (with two underscore wildcards instead of a single underscore wildcard)

from typing import Literal, assert_never
def example(alternate_content: tuple[Literal['normal', 'analyze',], str]) -> None:
    match alternate_content:
      case 'normal', payload:
        pass
      case 'analyze', payload:
        pass
      case _, _:
        assert_never(alternate_content)

Succeeds with no errors. How is this possible? Destructuring the tuple did not actually change the type of the tuple. Is this a special case? In that case _ should have the special case as well. _, _, _ also succeeds.

There are a number of match-case related tuple-narrowing issues open already, and resolving them may resolve this issue; however, I think this issue is technically a distinct aspect of the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions