Skip to content

types

types

Violation dataclass

Violation(line: int, label: str)

A single style violation, located by line so the runner can scope it to the edit.

Attributes:

Name Type Description
line int

1-based line number of the offending construct in the post-edit file.

label str

Short human-readable description, rendered as "{label} (line {line})".

StyleRule

Bases: ABC

Base class for a single-tree AST style rule applied to Python edits and writes.

Subclass it and write the rule's message as the class docstring ({violations} is substituted at fire time). Declare the rule as data by setting match to a Matcher (and optionally label); override check only for logic a matcher can't express. The class name is the rule's identity — NoNestedImports becomes "no-nested-imports".

Example
from captain_hook.style import matchers as M

class NoNestedImports(StyleRule):
    """Lazy imports belong at the top of the function body: {violations}"""

    match = M.imports & M.child_of(M.control_flow) & ~M.under(M.type_checking)

StyleDiffRule

Bases: StyleRule

Base class for a diff rule: flags constructs newly introduced by the change.

Like StyleRule, but it compares the pre-edit and post-edit trees. The declarative form flags nodes matching match in the new tree that were absent from the old tree (by unparsed source); override check when the "newly introduced" identity needs custom logic.

Example
from captain_hook.style import matchers as M

class NoNewWildcardImport(StyleDiffRule):
    """Wildcard import added by this edit: {violations}"""

    match = M.imports.where(lambda n: any(a.name == "*" for a in n.names))