Violations¶
Contains detailed technical information about violation internals.
Violations API¶
classDiagram
ABC <|-- BaseViolation
BaseViolation <|-- SimpleViolation
BaseViolation <|-- TokenizeViolation
BaseViolation <|-- _BaseASTViolation
Enum <|-- ViolationPostfixes
_BaseASTViolation <|-- ASTViolation
_BaseASTViolation <|-- MaybeASTViolation
Violation for |
|
Violation for |
|
Violation for |
|
Violation for cases where there's no associated nodes. |
Violation cannot have more than one base class. See Tutorial for more information about choosing a correct base class.
Conventions¶
Each violation class name should end with “Violation”
Each violation must have a long docstring with full description
Each violation must have “Reasoning” and “Solution” sections
Each violation must have “versionadded” policy
Each violation should have an example with correct and wrong usages
If violation error template should have a parameter it should be the last part of the text:
: {0}
Deprecating a violation¶
When you want to mark some violation as deprecated and disabled,
then assign disabled_since with a string version number to it:
@final
class SomeViolation(ASTViolation):
disabled_since = '1.0.0'
Reference¶
- ErrorNode: TypeAlias = ast.AST | tokenize.TokenInfo | None¶
General type for all possible nodes where error happens.
- ErrorCallback¶
We use this type to define helper classes with callbacks to add violations.
alias of
Callable[[BaseViolation],None]
- class ViolationPostfixes(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Bases:
EnumString values of postfixes used for violation baselines.
- bigger_than = ' > {0}'¶
- less_than = ' < {0}'¶
- class BaseViolation(node, text=None, baseline=None)[source]¶
Bases:
ABCAbstract base class for all style violations.
It basically just defines how to create any error and how to format this error later on.
Each subclass must define
error_templateandcodefields.-
error_template:
ClassVar[str]¶
-
code:
ClassVar[int]¶
-
disabled_since:
ClassVar[str|None] = None¶
-
full_code:
ClassVar[str]¶
-
summary:
ClassVar[str]¶
-
postfix_template:
ClassVar[ViolationPostfixes] = ' > {0}'¶
-
error_template:
- class ASTViolation(node, text=None, baseline=None)[source]¶
Bases:
_BaseASTViolationViolation for
astbased style visitors.
- class MaybeASTViolation(node=None, text=None, baseline=None)[source]¶
Bases:
_BaseASTViolationViolation for
astand modules visitors.Is used for violations that share the same rule for nodes and module names. Is wildly used for naming rules.
- class TokenizeViolation(node, text=None, baseline=None)[source]¶
Bases:
BaseViolationViolation for
tokenizebased visitors.
- class SimpleViolation(node=None, text=None, baseline=None)[source]¶
Bases:
BaseViolationViolation for cases where there’s no associated nodes.