2021-08-17 20:19:04 +00:00
|
|
|
from astroid import nodes
|
2006-04-26 10:48:09 +00:00
|
|
|
|
|
|
|
from pylint.checkers import BaseChecker
|
2019-03-09 10:22:36 +00:00
|
|
|
from pylint.interfaces import IAstroidChecker
|
2006-04-26 10:48:09 +00:00
|
|
|
|
2017-07-12 04:45:16 +00:00
|
|
|
|
|
|
|
# This is our checker class.
|
|
|
|
# Checkers should always inherit from `BaseChecker`.
|
2013-06-17 13:06:48 +00:00
|
|
|
class MyAstroidChecker(BaseChecker):
|
2017-07-12 04:45:16 +00:00
|
|
|
"""Add class member attributes to the class locals dictionary."""
|
|
|
|
|
|
|
|
# This class variable defines the type of checker that we are implementing.
|
|
|
|
# In this case, we are implementing an AST checker.
|
2013-06-17 13:06:48 +00:00
|
|
|
__implements__ = IAstroidChecker
|
2006-04-26 10:48:09 +00:00
|
|
|
|
2017-07-12 04:45:16 +00:00
|
|
|
# The name defines a custom section of the config for this checker.
|
2019-03-09 10:23:15 +00:00
|
|
|
name = "custom"
|
2017-07-12 04:45:16 +00:00
|
|
|
# The priority indicates the order that pylint will run the checkers.
|
|
|
|
priority = -1
|
|
|
|
# This class variable declares the messages (ie the warnings and errors)
|
|
|
|
# that the checker can emit.
|
2016-08-14 12:47:18 +00:00
|
|
|
msgs = {
|
2017-07-12 04:45:16 +00:00
|
|
|
# Each message has a code, a message that the user will see,
|
|
|
|
# a unique symbol that identifies the message,
|
|
|
|
# and a detailed help message
|
|
|
|
# that will be included in the documentation.
|
2019-03-09 10:23:15 +00:00
|
|
|
"W0001": ("Message that will be emitted", "message-symbol", "Message help")
|
2016-08-14 12:47:18 +00:00
|
|
|
}
|
2017-07-12 04:45:16 +00:00
|
|
|
# This class variable declares the options
|
|
|
|
# that are configurable by the user.
|
|
|
|
options = (
|
|
|
|
# Each option definition has a name which is used on the command line
|
|
|
|
# and in config files, and a dictionary of arguments
|
|
|
|
# (similar to those to those to
|
|
|
|
# argparse.ArgumentParser.add_argument).
|
2019-03-09 10:23:15 +00:00
|
|
|
(
|
|
|
|
"store-locals-indicator",
|
|
|
|
{
|
|
|
|
"default": "properties",
|
|
|
|
"help": (
|
|
|
|
"The expression name that indicates that the locals should "
|
|
|
|
"be stored"
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
2017-07-12 04:45:16 +00:00
|
|
|
)
|
2006-04-26 10:48:09 +00:00
|
|
|
|
2021-08-17 20:19:04 +00:00
|
|
|
def visit_call(self, node: nodes.Call) -> None:
|
|
|
|
"""Called when a :class:`.nodes.Call` node is visited.
|
2017-07-12 04:45:16 +00:00
|
|
|
|
|
|
|
See :mod:`astroid` for the description of available nodes.
|
2010-03-22 09:33:23 +00:00
|
|
|
|
2017-07-12 04:45:16 +00:00
|
|
|
:param node: The node to check.
|
|
|
|
"""
|
2019-03-09 10:23:15 +00:00
|
|
|
if not (
|
2021-08-17 20:19:04 +00:00
|
|
|
isinstance(node.func, nodes.Attribute)
|
|
|
|
and isinstance(node.func.expr, nodes.Name)
|
2019-03-09 10:23:15 +00:00
|
|
|
and node.func.expr.name == self.config.store_locals_indicator
|
|
|
|
and node.func.attrname == "create"
|
|
|
|
):
|
2006-04-26 10:48:09 +00:00
|
|
|
return
|
|
|
|
in_class = node.frame()
|
|
|
|
for param in node.args:
|
|
|
|
in_class.locals[param.name] = node
|
|
|
|
|
2017-07-12 04:45:16 +00:00
|
|
|
|
2006-04-26 10:48:09 +00:00
|
|
|
def register(linter):
|
2017-07-12 04:45:16 +00:00
|
|
|
"""This required method auto registers the checker.
|
|
|
|
|
|
|
|
:param linter: The linter to register the checker to.
|
|
|
|
:type linter: pylint.lint.PyLinter
|
|
|
|
"""
|
2013-06-17 13:06:48 +00:00
|
|
|
linter.register_checker(MyAstroidChecker(linter))
|