Visitors¶
Contains detailed documentation about how to write a visitor.
Creating new visitor¶
First of all, you have to decide what base class do you want to use?
Available base classes¶
BaseNodeVisitor |
Allows to store violations while traversing node tree. |
BaseFilenameVisitor |
Abstract base class that allows to visit and check module file names. |
BaseTokenVisitor |
Allows to check tokenize sequences. |
The decision relies on what parameters do you need for the task. It is highly unlikely that you will need two parameters at the same time.
Visitors API¶
-
class
BaseVisitor
(options, filename='stdin')[source]¶ Bases:
object
Abstract base class for different types of visitors.
-
options
¶ contains the options objects passed and parsed by
flake8
.
-
filename
¶ filename passed by
flake8
, each visitor has a file name.
-
violations
¶ list of violations for the specific visitor.
-
classmethod
from_checker
(checker)[source]¶ Constructs visitor instance from the checker.
Each unique visitor class should know how to construct itself from the
checker
instance.Generally speaking, each visitor class needs to eject required parameters from checker and then run its constructor with these parameters.
Return type: BaseVisitor
-
-
class
BaseNodeVisitor
(options, tree, **kwargs)[source]¶ Bases:
ast.NodeVisitor
,wemake_python_styleguide.visitors.base.BaseVisitor
Allows to store violations while traversing node tree.
This class should be used as a base class for all
ast
based checkers. Methodvisit()
is defined inNodeVisitor
class.-
tree
¶ ast
tree to be checked.
-
classmethod
from_checker
(checker)[source]¶ Constructs visitor instance from the checker.
Return type: BaseNodeVisitor
-
-
class
BaseFilenameVisitor
(options, filename='stdin')[source]¶ Bases:
wemake_python_styleguide.visitors.base.BaseVisitor
Abstract base class that allows to visit and check module file names.
Has
visit_filename()
method that should be defined in subclasses.
-
class
BaseTokenVisitor
(options, file_tokens, **kwargs)[source]¶ Bases:
wemake_python_styleguide.visitors.base.BaseVisitor
Allows to check
tokenize
sequences.-
file_tokens
¶ tokenize.TokenInfo
sequence to be checked.
-
classmethod
from_checker
(checker)[source]¶ Constructs
tokenize
based visitor instance from the checker.Return type: BaseTokenVisitor
-
visit
(token)[source]¶ Runs custom defined handlers in a visitor for each specific token type.
Uses
.exact_type
property to fetch the token name. So, you have to be extra careful with tokens like->
and other operators, since they might resolve in justOP
name.Does nothing if handler for any token type is not defined.
Return type: None
-