Python · maintainability
prefer-or-pattern
python:prefer-or-pattern Merge adjacent `case` arms with identical bodies into one or-pattern.
- Code
- SARJ070
- Default
- error
- Fix
- none
- Languages
- python
Why
An or-pattern expresses shared handling once and prevents identical arms from drifting apart.
Fix
Join the equivalent patterns with `|` and keep their shared body under the merged arm.
Before / after
Executed by this rule’s unit tests.
Before
Adjacent match arms repeat one body
def configure(value):
match value:
case LocalSettings():
return build(value)
case RemoteSettings():
return build(value)
After
One or-pattern owns the shared body
def configure(value):
match value:
case LocalSettings() | RemoteSettings():
return build(value)
Limits
- Only adjacent, unguarded, refutable arms with structurally identical non-empty bodies are compared.
- Arms with different bound names or comments are excluded because merging can change meaning or intent.