Skip to content

assert_never fails on match-casing enum with only one member #16922

Closed
@otaj

Description

@otaj

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions