Violations

Contains detailed technical information about violation internals.

Violations API

graph TD; _BaseASTViolation ASTViolation BaseViolation ViolationPostfixes MaybeASTViolation SimpleViolation TokenizeViolation Enum --> ViolationPostfixes BaseViolation --> _BaseASTViolation _BaseASTViolation --> ASTViolation _BaseASTViolation --> MaybeASTViolation BaseViolation --> TokenizeViolation BaseViolation --> SimpleViolation

ASTViolation

Violation for ast based style visitors.

MaybeASTViolation

Violation for ast and modules visitors.

TokenizeViolation

Violation for tokenize based visitors.

SimpleViolation

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 depracated, then assign deprecated boolean flag to it:

@final
class SomeViolation(ASTViolation):
    depracated = True

Reference

ErrorNode

General type for all possible nodes where error happens.

alias of Optional[Union[_ast.AST, tokenize.TokenInfo]]

ErrorCallback

We use this type to define helper classes with callbacks to add violations.

alias of Callable[[BaseViolation], None]

class ViolationPostfixes(value)[source]

Bases: enum.Enum

String values of postfixes used for violation baselines.

value

This field is required for mypy plugin that types field values.

bigger_than
less_than
class BaseViolation(node, text=None, baseline=None)[source]

Bases: object

Abstract 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_template and code fields.

error_template

message that will be shown to user after formatting.

code

violation unique number. Used to identify the violation.

previous_codes

just a documentation thing to track changes in time.

deprecated

indicates that this violation will be removed soon.

postfix_template

indicates message that we show at the very end.

error_template
code
previous_codes
deprecated
postfix_template
message()[source]

Returns error’s formatted message with code and reason.

Conditionally formats the error_template if it is required.

Return type

str

node_items()[source]

Returns tuple to match flake8 API format.

Return type

Tuple[int, int, str]

class ASTViolation(node, text=None, baseline=None)[source]

Bases: wemake_python_styleguide.violations.base._BaseASTViolation

Violation for ast based style visitors.

class MaybeASTViolation(node=None, text=None, baseline=None)[source]

Bases: wemake_python_styleguide.violations.base._BaseASTViolation

Violation for ast and 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: wemake_python_styleguide.violations.base.BaseViolation

Violation for tokenize based visitors.

class SimpleViolation(node=None, text=None, baseline=None)[source]

Bases: wemake_python_styleguide.violations.base.BaseViolation

Violation for cases where there’s no associated nodes.