Skip to content

Enums do not exhaustively match correctly  #14393

Closed
@Be3y4uu-K0T

Description

@Be3y4uu-K0T

Bug Report

When I transform a value into an enum, an error occurs in the match case construct. If you convert the value in advance, there will be no error. Same issue #14109 #14264 .

To Reproduce

import typing
import enum

class Color(enum.StrEnum):
    RED = enum.auto()
    GREEN = enum.auto()
    BLUE = enum.auto()

def test(color: str) -> str:
    match Color(color):
        case Color.RED:
            return 'red'
        case Color.GREEN:
            return 'green'
        case Color.BLUE:
            return 'blue'
        case _ as unreachable:
            typing.assert_never(unreachable)

https://mypy-play.net/?mypy=latest&python=3.11&gist=d28ae2af9f4545ca1c302476d8e4c782

import typing
import enum

class Color(enum.StrEnum):
    RED = enum.auto()
    GREEN = enum.auto()
    BLUE = enum.auto()

def test(color: str) -> str:
    color = Color(color)
    match color:
        case Color.RED:
            return 'red'
        case Color.GREEN:
            return 'green'
        case Color.BLUE:
            return 'blue'
        case _ as unreachable:
            typing.assert_never(unreachable)

https://mypy-play.net/?mypy=latest&python=3.11&gist=eab4deab65eb38a49649a46a2adc1e42

Actual Behavior

In the first example:

main.py:18: error: Argument 1 to "assert_never" has incompatible type "Color"; expected "NoReturn"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

In the second example:

Success: no issues found in 1 source file

Your Environment

  • Mypy version used: mypy 0.991 (compiled: yes)
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.11.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions