Rules¶
Rule Base Classes¶
- class LintRule[source]¶
Lint rule implemented using LibCST.
To build a new lint rule, subclass this and Implement a CST visitor. When a lint rule violation should be reported, use the
report()method.- METADATA_DEPENDENCIES: ClassVar[Collection[ProviderT]] = ()¶
Required LibCST metadata providers
- NAME: ClassVar[str] = ''¶
Explicit public rule name. Defaults to kebab-case generated from the class name.
- PYTHON_VERSION: str = ''¶
Compatible target Python versions, in PEP 440 version specifier format.
- INVALID: ClassVar[Sequence[str | Invalid]]¶
Test cases that are expected to produce errors, with optional replacements
- SETTINGS: ClassVar[dict[str, RuleSetting]] = {}¶
Optional typed configuration settings for this lint rule.
- REFERENCES: ClassVar[Sequence[RuleReference]] = ()¶
External references for documentation, as URLs or
(label, URL)pairs.
- AUTOFIX = False¶
Whether the lint rule contains an autofix.
Set to
Trueautomatically whenINVALIDcontains at least one test case that provides an expected replacement.
- node_comments(node)[source]¶
Yield all comments associated with the given node.
Includes comments from both leading comments and trailing inline comments.
- ignore_lint(node)[source]¶
Whether to ignore a violation for a given node.
Returns true if any
# rattle: ignore[...]directive matches the current rule by name, or if the directive has no rule names listed.
- report(
- node,
- message,
- *,
- position=None,
- position_node=None,
- replacement=None,
Report a lint rule violation.
The optional position parameter can override the location where the violation is reported. By default, the entire span of node is used. If position is a CodePosition, only a single character is marked.
The optional replacement parameter can be used to provide an auto-fix for this lint violation. Replacing node with replacement should make the lint violation go away.
Rule Test Cases¶
- class Valid[source]¶
Valid(code: str, options: dict[str, str | int | float | bool | list[typing.Any] | dict[str, typing.Any]] | None = None)
Rule Metadata¶
- RuleReference = str | tuple[str, str]¶
Represent a PEP 604 union type
E.g. for int | str