Skip to content

Pattern Syntax for Custom Rules

A pattern can contain multiple captures.

Capture Naming

A capture's name can't be a Python keyword or the name of a built-in function.

For example, the following capture names are invalid:

  • in
  • input

Capturing Multiple Positional Arguments with * and +

Capturing Optional Positional Arguments with *

* captures all calls with 0, 1 or more positional arguments.

print(${args*}

For example, this rule captures all calls to print() with 0, 1 or more positional arguments but without keyword arguments:

  - id: print_args_star
    description: print with args*
    pattern: print(${args*})
    tests:
      - match: print("placeholder")
      - match: print(16, 20)
      - match: print()
      - no-match: print(16, 20, sep=";")

Capturing 1 or More Positional Arguments with +

+ captures all calls with 1 or more positional arguments.

print(${args+}

For example, this rule captures all calls to print() with 1 or more positional arguments but without keyword arguments:

  - id: print_args_plus
    description: print with args+
    pattern: print(${args+})
    tests:
      - match: print("placeholder")
      - match: print(16, 20)
      - no-match: print()
      - no-match: print(16, 20, sep=";")