prefer-match-value-dispatch
Prefer match/case for repeated dispatch on one value with a fallback.
Why
A single dispatch subject makes distinct value cases and their fallback easier to review.
Fix
Consider match/case while preserving fallback and constant semantics; use literal or dotted value patterns, never bare constant names that capture instead of compare. Review membership versus equality, subject evaluation, and guard order before rewriting.
Examples
from typing import Literal
Format = Literal["pcm", "wav", "mp3", "flac"]
def encode(value: Format): if value in ("pcm", "wav"): return direct(value) return compressed(value)from typing import Literal
Format = Literal["pcm", "wav", "mp3", "flac"]
def encode(value: Format): match value: case "pcm" | "wav": return direct(value) case "mp3" | "flac": return compressed(value)def parse(kind): if kind == "json": return json_parser() elif kind == "yaml": return yaml_parser() else: return fallback()[project]requires-python = ">=3.10"def migrate(path): match path.name: case "eslint.json": return rewrite_eslint(path) case "ratchet.json": return rewrite_ratchet(path) case _: return rewrite(path)from provider import ErrorId
def classify(error_id): if error_id == ErrorId.INSUFFICIENT: return insufficient() if error_id == ErrorId.INVALID_OPTION: return invalid_option() return rejected()[project]requires-python = ">=3.10"from provider import ErrorId
def classify(error_id): match error_id: case ErrorId.INSUFFICIENT: return insufficient() case ErrorId.INVALID_OPTION: return invalid_option() case _: return rejected()