Skip to content

Prefer if over match simple sequences and mappings #3527

@ZhdanovAM72

Description

@ZhdanovAM72

Rule request

Thesis

We should support simplifying match statements that use simple sequence and mapping patterns:

Example:

match data:
    case [1, 2]:
        handle_pair()
    case _:
        ignore()

Can be rewritten as:

if data == [1, 2]:
    handle_pair()
else:
    ignore()

Reasoning

Using match for exact structural comparisons of simple literals is unnecessarily verbose. While match excels at deconstruction, using it to check for an exact list or dict value is better expressed with a direct equality comparison (==), which is more readable and performant.

These patterns are common in configuration checks, API responses, and state validation.

When should this be allowed?

Only when:

  • There are exactly two cases (with case _:)
  • The first pattern is a simple sequence like [1, "a"] or a simple mapping like {"x": 1}
  • All elements/keys/values are simple (literals, constants, names)
  • No starred patterns (*rest) or variable bindings (case [x, y]:)
  • Keys in mappings are constants (not variables)

Not allowed if:

  • Uses deconstruction (e.g. case [x, y]:)
  • Contains *args or **kwargs
  • Has guards (if ...)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions