Skip to content

user-defined-patterns (KPT*)

Pattern rules

This rule is enabled by default.

What it does

Checks every source file against regular expressions (Rust regex syntax) defined in your project configuration, line by line. Each pattern has its own code and is addressable in select, ignore, per-file-ignores and # noqa.

Why is this bad?

Some conventions are specific to a project (no bare print(), no breakpoint() left behind, no unticketed TODO). A pattern rule enforces them without writing a plugin.

Example

print("hello")   # KPT001, with the pattern below

Use instead:

logger.info("hello")

Defining patterns

[[tool.konform.lint.user-defined-patterns.rules]]
id      = "KPT001"
message = "Use the project logger instead of bare print()."
pattern = '^\s*print\s*\('
files   = ["src/**/*.py"]
level   = "warning"

Patterns are loaded from the first available source:

  1. Inline [[tool.konform.lint.user-defined-patterns.rules]] in the config file.
  2. rules_file = "path" in [tool.konform.lint.user-defined-patterns].
  3. konform_patterns.toml, auto-discovered next to the config file.
  4. konform_patterns.yaml (legacy fallback).

A standalone file uses plain [[rules]] tables.

Pattern entry fields

Field Required Description
id yes Code shown in output and used for # noqa
message yes Human-readable description
pattern yes Regex, or a list of regexes (any match fires)
files no Glob filter; omitted means every file
level no "error" or "warning"; inherits the table's level
help no Guidance text shown with the violation
multiline no Match against the whole file instead of line by line
replacement no Replacement string ($1, $2, ... captures); makes the pattern fixable
sub_rules no Refinements overriding message/help; the first match wins

Sub-rules

Sub-rules refine the message and help for more specific matches. The first sub-rule whose pattern(s) match the already-flagged line wins.

[[rules]]
id      = "KPT901"
message = "os.environ found, discouraged in test code."
pattern = 'os\.environ'
sub_rules = [
{
pattern = ['os\.environ\.get\(', 'os\.environ\['],
message = "os.environ special_key access detected.",
help    = "Use the 'shared_fixture' fixture instead.",
},
]

With [[rules.sub_rules]] tables, each sub-rule belongs to the most recently declared [[rules]] entry.

Fix behavior

A pattern with a replacement is fixable; patterns without one are report-only.

Options

Configured in [tool.konform.lint.user-defined-patterns].

Option Type Default Description
level "warning" \| "error" "warning" Default severity for patterns that don't set their own.
rules_file str unset Load patterns from this file instead of auto-discovery.
rules array of tables [] Inline pattern entries.

Suppression

x = 1  # noqa: KPT001
x = 1  # noqa: KPT  (every KPT* rule)