style.StyleRule

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

Usage

Source

style.StyleRule()

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][captain_hook.style.matchers.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)

Attributes

Name Description
label The type of the None singleton.
match The type of the None singleton.
sep str(object=’’) -> str
tests dict() -> new empty dictionary

label

The type of the None singleton.

label: str | Callable[[ast.AST], str] | None = None


match

The type of the None singleton.

match: Matcher | None = None


sep

str(object=’’) -> str

sep: str = "- "

str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to ‘utf-8’. errors defaults to ‘strict’.


tests

dict() -> new empty dictionary

tests: InlineTests = {}

dict(mapping) -> new dictionary initialized from a mapping object’s (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)