Inspectopedia 2026.1 Help

Invalid pattern in pattern matching

Reports incorrect or ambiguous use of structural pattern matching in Python class patterns and __match_args__ definitions.

This inspection helps you catch issues such as:

  • Using positional patterns for a class that does not define __match_args__.

  • Providing more positional patterns than the class exposes via __match_args__.

  • Specifying the same attribute both positionally and by keyword in a single class pattern.

  • Defining __match_args__ with invalid type (must be tuple[str, ...]).

For certain simple as-patterns with built-in classes, it also suggests a quick fix to simplify the pattern.

# Too many positional patterns match p: case Point(x, y, z): # expected only 2 according to __match_args__ ... # Positional + keyword conflict match p: case Point(x, y, x=0): # 'x' is already specified positionally ... # Class without __match_args__ but used with positional patterns class C: def __init__(self, a, b): self.a = a; self.b = b match obj: case C(1, 2): # C does not support positional patterns ... # Invalid __match_args__ declaration class D: __match_args__ = ["a", 42] # must be tuple[str, ...]

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

PyPattern
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Python

The inspection provides quick fixes where possible, for example to add __match_args__, remove redundant patterns, or simplify certain as-patterns involving built-in classes.

Inspection ID: PyPatternInspection

Suppressing Inspection

You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:

//noinspection PyPattern

More detailed instructions as well as other ways and options that you have can be found in the product documentation:

Last modified: 31 March 2026