Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wyattscarpenter opened this issue Mar 12, 2025 · 1 comment
Open
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code

Comments

@wyattscarpenter
Copy link
Contributor

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.

@wyattscarpenter wyattscarpenter added the bug mypy got something wrong label Mar 12, 2025
@sterliakov sterliakov added topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code labels Mar 12, 2025
@sterliakov
Copy link
Collaborator

Destructuring the tuple did not actually change the type of the tuple. Is this a special case?

Not exactly, assert_never line in your snippet is unreachable. You can replace it with 1 + 'a' and see no errors as well (or enable --warn-unreachable). It becomes unreachable because all tuple components are Never, see this playground.

The problem is exactly that mypy doesn't consider n-tuple of Never's uninhabited as a whole; probably the same underlying reason as #16650.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code
Projects
None yet
Development

No branches or pull requests

2 participants