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

Enums do not exhaustively match correctly #14393

Closed
Be3y4uu-K0T opened this issue Jan 4, 2023 · 2 comments
Closed

Enums do not exhaustively match correctly #14393

Be3y4uu-K0T opened this issue Jan 4, 2023 · 2 comments
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code

Comments

@Be3y4uu-K0T
Copy link

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
@Be3y4uu-K0T Be3y4uu-K0T added the bug mypy got something wrong label Jan 4, 2023
@JelleZijlstra JelleZijlstra added topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code labels Jan 4, 2023
@MattiasOlla
Copy link

Any updates on this? This bug has been open for months now.

@Hnasar
Copy link
Contributor

Hnasar commented Mar 24, 2024

This is a dupe of #12998 and fixed by #16503 and is no longer reproducible on master. Can someone close this issue?

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

4 participants