Skip to content

Untrue "missing return statement" after exhaustive match on enum #16276

Closed as not planned
@aleksanb

Description

@aleksanb

Bug Report

Exhaustive match on enums fails if no intermediate alias of the match variable is given.

To Reproduce

from enum import Enum

class Choices(str, Enum):
    A="A"
    B="B"
    
    
def matcher_implicit(a: str) -> str:
    match Choices(a):
        case Choices.A:
            return 'ok'
        case Choices.B:
            return 'schmok'
            

def matcher_alias(a: str) -> str:
    explicit_alias = Choices(a)
    match explicit_alias:
        case Choices.A:
            return 'ok'
        case Choices.B:
            return 'schmok'

You can run this on https://mypy-play.net/?mypy=latest&python=3.11, and see that only matcher_implicit fails, but there's no reason why it should!

Expected Behavior

Both should typecheck correctly

Actual Behavior

The first one fails typechecking, with a fake "missing return statement" error emitted.

Your Environment

  • Mypy version used: 1.5.1 and 1.6.0 (fails on both).
  • sample fails on mypy-play.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-match-statementPython 3.10's match statement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions