mypy icon indicating copy to clipboard operation
mypy copied to clipboard

`assert_never` fails on match-casing `enum` with only one member

Open otaj opened this issue 1 year ago • 3 comments

Bug Report

When there is only one member of the enum, mypy doesn't perform full exhaustiveness checking and reaches assert_never statement, although it shouldn't.

To Reproduce

mypy-play link

Expected Behavior

mypy does not complain and reports no errors

Actual Behavior

mypy reaches unreachable code and complains

main.py:15: error: Argument 1 to "assert_never" has incompatible type "A"; expected "NoReturn" [arg-type]

Your Environment

Playground

Rationale

While having an enum with only one member is definitely a contrived example, it is a valid example nonetheless. This popped up for me while I was implementing a feature partially for just one case of an enum first with the plan of filing up more enum cases later on.

Note that this is specific for enums with one member only, if you have a Literal with only one member, mypy correctly deduces that unreachable code is truly unreachable. If you have an enum with multiple members (and an appropriate match-case block), the unreachable code is also correctly deduced.

otaj avatar Feb 16 '24 12:02 otaj

I also just spent 10-minutes fighting this and thought I was going crazy. Single-item enums are typically intended to eventually have more than one item; so exhaustiveness checks on their use are valid and useful (to keep things in sync).

athuras avatar Apr 09 '24 19:04 athuras

This seems to be fixed in version 1.10. I've just tried the example in mypy-play, and it passes just fine.

jakubbortlik avatar May 09 '24 09:05 jakubbortlik

Fixed here: https://github.com/python/mypy/pull/16966

Duplicate with: https://github.com/python/mypy/issues/14109

superosku avatar Jun 13 '24 00:06 superosku