Skip to content
Sarj Code Standards

prefer-match-assert-never

Typed enum dispatch must not silently ignore unhandled members.

Why

A wildcard or else branch can hide missing enum members and let newly added members inherit default behavior. Suppressing the argument-type error at assert_never also disables its static exhaustiveness guarantee.

Fix

Handle enum members explicitly and group those that share a default. For an exhausted type, bind the catch-all value and pass it to typing.assert_never; raise an explicit exception when static exhaustiveness is unavailable. Narrow the accepted type or handle missing variants instead of suppressing assert_never's argument check.

Examples

Before — flagged An enum wildcard hides the members that share a default
dispatch.py
from enum import StrEnum
class Job(StrEnum):
CALLER_EXPERIENCE = "caller_experience"
JUDGE = "judge"
TRANSCRIPT = "transcript"
REPORT = "report"
def max_output_tokens(job: Job) -> int:
match job:
case Job.CALLER_EXPERIENCE:
return 4_000
case Job.JUDGE:
return 8_000
case _:
return 2_000
After — preferred Group enum members that share a default
dispatch.py
from enum import StrEnum
from typing import assert_never
class Job(StrEnum):
CALLER_EXPERIENCE = "caller_experience"
JUDGE = "judge"
TRANSCRIPT = "transcript"
REPORT = "report"
def max_output_tokens(job: Job) -> int:
match job:
case Job.CALLER_EXPERIENCE:
return 4_000
case Job.JUDGE:
return 8_000
case Job.TRANSCRIPT | Job.REPORT:
return 2_000
case _ as unreachable:
assert_never(unreachable)
Before — flagged Closed-set match silently ignores a variant
dispatch.py
from enum import StrEnum
class Kind(StrEnum):
A = "a"
B = "b"
C = "c"
def handle(kind: Kind) -> None:
match kind:
case Kind.A:
handle_a()
case Kind.B:
handle_b()
case _:
pass
After — preferred Closed-set match rejects an unhandled variant
dispatch.py
from enum import StrEnum
from typing import assert_never
class Kind(StrEnum):
A = "a"
B = "b"
C = "c"
def handle(kind: Kind) -> None:
match kind:
case Kind.A:
handle_a()
case Kind.B:
handle_b()
case Kind.C:
handle_c()
case _ as unreachable:
assert_never(unreachable)