Python pattern matching is ordered dispatch, not validation
Python’s match statement selects the first pattern and guard that succeeds. Capture names, partial mapping matches, and observable guard effects make pattern order part of the program’s contract.
A match statement chooses a branch in order
Python’s match statement is often introduced as a compact way to compare a value against several shapes. Its more useful mental model is ordered dispatch. Python evaluates the subject once, considers case blocks in source order, and executes the first case whose pattern succeeds and whose optional guard is truthy. A case that looks like a later “more specific” option has no effect if an earlier case already accepts the same value. [1]
That makes case ordering part of observable behavior, much like the order of an if/elif chain. For a protocol message, put narrow operations such as a create command before a broad fallback. If a general case is moved upward during cleanup, the file may still parse and the pattern may still be valid while a more specific operation becomes unreachable. Reviewing a match statement means tracing representative subjects from the first case down, not just checking that each case looks plausible in isolation.
A bare name captures; a dotted name compares
The most consequential visual trap is a bare identifier. In a pattern, case READY: does not mean “compare with the variable READY.” A capture pattern always succeeds and binds the subject to that name. A single underscore is the wildcard: it always succeeds without binding. To compare with a constant or enum member, use a dotted value pattern such as case Status.READY:. The spelling difference changes the branch from a test into an unconditional capture. [1]
For example, a message dispatcher that intends to recognize Status.DELETE can accidentally treat every value as a delete request if it writes a bare DELETE capture. A small review habit catches this: identify every pattern name and ask whether it is meant to bind new data or refer to an existing value. Then check the following cases. Since capture patterns cannot fail, a bare name before another case can make every later case dead code. Static reading is especially useful here because the erroneous code can be syntactically valid and the bound name can be used normally in its suite.
A mapping pattern describes required keys, not an exact schema
A mapping pattern is a convenient way to select a message by a few fields. case {"kind": "delete", "path": path}: requires a mapping with those keys and matching values, then binds the path. It does not require the mapping to contain only those two keys. If additional fields matter, capture them explicitly with **rest and decide what to do with them; otherwise the case deliberately tolerates extensions. Mapping keys and value subpatterns have defined matching rules in the language specification. [1]
This is useful for version-tolerant dispatch, but it is not a complete validation policy. A matching path could still be the wrong type, empty, too long, or outside the application’s allowed namespace. A message may also need a supported version, an authorization check, or relationships between fields that a simple shape does not express. Pattern matching establishes that the subject has enough structure for the chosen branch; application code must still enforce the invariants that make the operation safe.
Guards are ordinary expressions with visible effects
A guard runs only after its pattern succeeds. Guards are evaluated in case order, and an exception raised by a guard propagates out of the match statement. Because a guard is an expression, it can also have side effects. That means a pattern can run first, then a guard can write a log entry, increment a counter, or call another function before a later case is considered. [1]
Prefer guards that are short and predictable, such as a range check on a captured integer or a membership check against an already-built set. Put operations that can fail, perform I/O, or mutate state in the selected case body, where the branch is explicit. This keeps it clear whether a failed guard merely means “try the next shape” or has already changed program state. It also makes the order easy to test: cover subjects that fail the pattern, subjects that pass the pattern but fail the guard, and subjects that select each intended branch.
Separate parsing, validation and dispatch
For data arriving from a socket, file, or HTTP request, use separate steps. First parse the bytes into ordinary values and reject malformed input. Next validate the application contract: required fields, types, supported versions, size limits, and any cross-field or permission rules. Then dispatch the validated representation with match. This separation gives each failure a useful meaning: syntax error, invalid request, or unsupported operation, rather than a single catch-all branch that silently accepts an unexpected shape.
A practical review can list one representative input for each case, one input that should fall through, and one near-miss for every required field. Check that a bare identifier is intentional, that a mapping case’s tolerance for extra keys is deliberate, and that guards are cheap and side-effect free. Python’s pattern matching is expressive precisely because it combines comparison and binding. Treating those two effects as separate review questions preserves that clarity as the dispatch table grows. [2]