fname
stringlengths
63
176
rel_fname
stringclasses
706 values
line
int64
-1
4.5k
name
stringlengths
1
81
kind
stringclasses
2 values
category
stringclasses
2 values
info
stringlengths
0
77.9k
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/classes/special_methods_checker.py
pylint/checkers/classes/special_methods_checker.py
344
add_message
ref
function
self.add_message("invalid-length-hint-returned", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/classes/special_methods_checker.py
pylint/checkers/classes/special_methods_checker.py
346
add_message
ref
function
self.add_message("invalid-length-hint-returned", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/classes/special_methods_checker.py
pylint/checkers/classes/special_methods_checker.py
348
_check_format
def
function
def _check_format(self, node, inferred): if not self._is_str(inferred): self.add_message("invalid-format-returned", node=node) def _check_getnewargs(self, node, inferred): if not self._is_tuple(inferred): self.add_message("invalid-getnewargs-returned", node=node) def _check_getnewargs_ex(self, node, inferred): if not self._is_tuple(inferred): self.add_message("invalid-getnewargs-ex-returned", node=node) return if not isinstance(inferred, nodes.Tuple): # If it's not an astroid.Tuple we can't analyze it further return found_error = _False if len(inferred.elts) != 2: found_error = _True else: for arg, check in ( (inferred.elts[0], self._is_tuple), (inferred.elts[1], self._is_dict), ): if isinstance(arg, nodes.Call): arg = safe_infer(arg) if arg and arg is not astroid.Uninferable: if not check(arg): found_error = _True break if found_error: self.add_message("invalid-getnewargs-ex-returned", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/classes/special_methods_checker.py
pylint/checkers/classes/special_methods_checker.py
349
_is_str
ref
function
if not self._is_str(inferred):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/classes/special_methods_checker.py
pylint/checkers/classes/special_methods_checker.py
350
add_message
ref
function
self.add_message("invalid-format-returned", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/classes/special_methods_checker.py
pylint/checkers/classes/special_methods_checker.py
352
_check_getnewargs
def
function
def _check_getnewargs(self, node, inferred): if not self._is_tuple(inferred): self.add_message("invalid-getnewargs-returned", node=node) def _check_getnewargs_ex(self, node, inferred): if not self._is_tuple(inferred): self.add_message("invalid-getnewargs-ex-returned", node=node) return if not isinstance(inferred, nodes.Tuple): # If it's not an astroid.Tuple we can't analyze it further return found_error = _False if len(inferred.elts) != 2: found_error = _True else: for arg, check in ( (inferred.elts[0], self._is_tuple), (inferred.elts[1], self._is_dict), ): if isinstance(arg, nodes.Call): arg = safe_infer(arg) if arg and arg is not astroid.Uninferable: if not check(arg): found_error = _True break if found_error: self.add_message("invalid-getnewargs-ex-returned", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/classes/special_methods_checker.py
pylint/checkers/classes/special_methods_checker.py
353
_is_tuple
ref
function
if not self._is_tuple(inferred):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/classes/special_methods_checker.py
pylint/checkers/classes/special_methods_checker.py
354
add_message
ref
function
self.add_message("invalid-getnewargs-returned", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/classes/special_methods_checker.py
pylint/checkers/classes/special_methods_checker.py
356
_check_getnewargs_ex
def
function
def _check_getnewargs_ex(self, node, inferred): if not self._is_tuple(inferred): self.add_message("invalid-getnewargs-ex-returned", node=node) return if not isinstance(inferred, nodes.Tuple): # If it's not an astroid.Tuple we can't analyze it further return found_error = _False if len(inferred.elts) != 2: found_error = _True else: for arg, check in ( (inferred.elts[0], self._is_tuple), (inferred.elts[1], self._is_dict), ): if isinstance(arg, nodes.Call): arg = safe_infer(arg) if arg and arg is not astroid.Uninferable: if not check(arg): found_error = _True break if found_error: self.add_message("invalid-getnewargs-ex-returned", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/classes/special_methods_checker.py
pylint/checkers/classes/special_methods_checker.py
357
_is_tuple
ref
function
if not self._is_tuple(inferred):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/classes/special_methods_checker.py
pylint/checkers/classes/special_methods_checker.py
358
add_message
ref
function
self.add_message("invalid-getnewargs-ex-returned", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/classes/special_methods_checker.py
pylint/checkers/classes/special_methods_checker.py
376
safe_infer
ref
function
arg = safe_infer(arg)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/classes/special_methods_checker.py
pylint/checkers/classes/special_methods_checker.py
379
check
ref
function
if not check(arg):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/classes/special_methods_checker.py
pylint/checkers/classes/special_methods_checker.py
384
add_message
ref
function
self.add_message("invalid-getnewargs-ex-returned", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
22
DeprecatedMixin
def
class
visit_call visit_import deprecated_decorators visit_decorators visit_importfrom deprecated_methods deprecated_arguments deprecated_modules deprecated_classes check_deprecated_module check_deprecated_method check_deprecated_class check_deprecated_class_in_call
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
55
check_messages
ref
function
@utils.check_messages(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
60
visit_call
def
function
def visit_call(self, node: nodes.Call) -> None: """Called when a :class:`nodes.Call` node is visited.""" self.check_deprecated_class_in_call(node) for inferred in infer_all(node.func): # Calling entry point for deprecation check logic. self.check_deprecated_method(node, inferred) @utils.check_messages( "deprecated-module", "deprecated-class", ) def visit_import(self, node: nodes.Import) -> None: """Triggered when an import statement is seen.""" for name in (name for name, _ in node.names): self.check_deprecated_module(node, name) if "." in name: # Checking deprecation for import module with class mod_name, class_name = name.split(".", 1) self.check_deprecated_class(node, mod_name, (class_name,)) def deprecated_decorators(self) -> Iterable: """Callback returning the deprecated decorators. Returns: collections.abc.Container of deprecated decorator names. """ # pylint: disable=no-self-use return () @utils.check_messages("deprecated-decorator") def visit_decorators(self, node: nodes.Decorators) -> None: """Triggered when a decorator statement is seen.""" children = list(node.get_children()) if not children: return if isinstance(children[0], nodes.Call): inf = safe_infer(children[0].func) else: inf = safe_infer(children[0]) qname = inf.qname() if inf else None if qname in self.deprecated_decorators(): self.add_message("deprecated-decorator", node=node, args=qname) @utils.check_messages( "deprecated-module", "deprecated-class", ) def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Triggered when a from statement is seen.""" basename = node.modname basename = get_import_name(node, basename) self.check_deprecated_module(node, basename) class_names = (name for name, _ in node.names) self.check_deprecated_class(node, basename, class_names) def deprecated_methods(self) -> Container[str]: """Callback returning the deprecated methods/functions. Returns: collections.abc.Container of deprecated function/method names. """ # pylint: disable=no-self-use return () def deprecated_arguments( self, method: str ) -> Iterable[Tuple[Union[int, None], str]]: """Callback returning the deprecated arguments of method/function. Args: method (str): name of function/method checked for deprecated arguments Returns: collections.abc.Iterable in form: ((POSITION1, PARAM1), (POSITION2: PARAM2) ...) where * POSITIONX - position of deprecated argument PARAMX in function definition. If argument is keyword-only, POSITIONX should be None. * PARAMX - name of the deprecated argument. E.g. suppose function: .. code-block:: python def bar(arg1, arg2, arg3, arg4, arg5='spam') with deprecated arguments `arg2` and `arg4`. `deprecated_arguments` should return: .. code-block:: python ((1, 'arg2'), (3, 'arg4')) """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def deprecated_modules(self) -> Iterable: """Callback returning the deprecated modules. Returns: collections.abc.Container of deprecated module names. """ # pylint: disable=no-self-use return () def deprecated_classes(self, module: str) -> Iterable: """Callback returning the deprecated classes of module. Args: module (str): name of module checked for deprecated classes Returns: collections.abc.Container of deprecated class names. """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def check_deprecated_module(self, node, mod_path): """Checks if the module is deprecated.""" for mod_name in self.deprecated_modules(): if mod_path == mod_name or mod_path.startswith(mod_name + "."): self.add_message("deprecated-module", node=node, args=mod_path) def check_deprecated_method(self, node, inferred): """Executes the checker for the given node. This method should be called from the checker implementing this mixin. """ # Reject nodes which aren't of interest to us. if not isinstance(inferred, ACCEPTABLE_NODES): return if isinstance(node.func, nodes.Attribute): func_name = node.func.attrname elif isinstance(node.func, nodes.Name): func_name = node.func.name else: # Not interested in other nodes. return if hasattr(inferred.parent, "qname") and inferred.parent.qname(): # Handling the situation when deprecated function is # alias to existing function. qnames = { inferred.qname(), f"{inferred.parent.qname()}.{func_name}", func_name, } else: qnames = {inferred.qname(), func_name} if any(name in self.deprecated_methods() for name in qnames): self.add_message("deprecated-method", node=node, args=(func_name,)) return num_of_args = len(node.args) kwargs = {kw.arg for kw in node.keywords} if node.keywords else {} deprecated_arguments = (self.deprecated_arguments(qn) for qn in qnames) for position, arg_name in chain(*deprecated_arguments): if arg_name in kwargs: # function was called with deprecated argument as keyword argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) elif position is not None and position < num_of_args: # function was called with deprecated argument as positional argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) def check_deprecated_class(self, node, mod_name, class_names): """Checks if the class is deprecated.""" for class_name in class_names: if class_name in self.deprecated_classes(mod_name): self.add_message( "deprecated-class", node=node, args=(class_name, mod_name) ) def check_deprecated_class_in_call(self, node): """Checks if call the deprecated class.""" if isinstance(node.func, nodes.Attribute) and isinstance( node.func.expr, nodes.Name ): mod_name = node.func.expr.name class_name = node.func.attrname self.check_deprecated_class(node, mod_name, (class_name,))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
62
check_deprecated_class_in_call
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
63
infer_all
ref
function
for inferred in infer_all(node.func):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
65
check_deprecated_method
ref
function
self.check_deprecated_method(node, inferred)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
67
check_messages
ref
function
@utils.check_messages(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
71
visit_import
def
function
def visit_import(self, node: nodes.Import) -> None: """Triggered when an import statement is seen.""" for name in (name for name, _ in node.names): self.check_deprecated_module(node, name) if "." in name: # Checking deprecation for import module with class mod_name, class_name = name.split(".", 1) self.check_deprecated_class(node, mod_name, (class_name,)) def deprecated_decorators(self) -> Iterable: """Callback returning the deprecated decorators. Returns: collections.abc.Container of deprecated decorator names. """ # pylint: disable=no-self-use return () @utils.check_messages("deprecated-decorator") def visit_decorators(self, node: nodes.Decorators) -> None: """Triggered when a decorator statement is seen.""" children = list(node.get_children()) if not children: return if isinstance(children[0], nodes.Call): inf = safe_infer(children[0].func) else: inf = safe_infer(children[0]) qname = inf.qname() if inf else None if qname in self.deprecated_decorators(): self.add_message("deprecated-decorator", node=node, args=qname) @utils.check_messages( "deprecated-module", "deprecated-class", ) def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Triggered when a from statement is seen.""" basename = node.modname basename = get_import_name(node, basename) self.check_deprecated_module(node, basename) class_names = (name for name, _ in node.names) self.check_deprecated_class(node, basename, class_names) def deprecated_methods(self) -> Container[str]: """Callback returning the deprecated methods/functions. Returns: collections.abc.Container of deprecated function/method names. """ # pylint: disable=no-self-use return () def deprecated_arguments( self, method: str ) -> Iterable[Tuple[Union[int, None], str]]: """Callback returning the deprecated arguments of method/function. Args: method (str): name of function/method checked for deprecated arguments Returns: collections.abc.Iterable in form: ((POSITION1, PARAM1), (POSITION2: PARAM2) ...) where * POSITIONX - position of deprecated argument PARAMX in function definition. If argument is keyword-only, POSITIONX should be None. * PARAMX - name of the deprecated argument. E.g. suppose function: .. code-block:: python def bar(arg1, arg2, arg3, arg4, arg5='spam') with deprecated arguments `arg2` and `arg4`. `deprecated_arguments` should return: .. code-block:: python ((1, 'arg2'), (3, 'arg4')) """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def deprecated_modules(self) -> Iterable: """Callback returning the deprecated modules. Returns: collections.abc.Container of deprecated module names. """ # pylint: disable=no-self-use return () def deprecated_classes(self, module: str) -> Iterable: """Callback returning the deprecated classes of module. Args: module (str): name of module checked for deprecated classes Returns: collections.abc.Container of deprecated class names. """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def check_deprecated_module(self, node, mod_path): """Checks if the module is deprecated.""" for mod_name in self.deprecated_modules(): if mod_path == mod_name or mod_path.startswith(mod_name + "."): self.add_message("deprecated-module", node=node, args=mod_path) def check_deprecated_method(self, node, inferred): """Executes the checker for the given node. This method should be called from the checker implementing this mixin. """ # Reject nodes which aren't of interest to us. if not isinstance(inferred, ACCEPTABLE_NODES): return if isinstance(node.func, nodes.Attribute): func_name = node.func.attrname elif isinstance(node.func, nodes.Name): func_name = node.func.name else: # Not interested in other nodes. return if hasattr(inferred.parent, "qname") and inferred.parent.qname(): # Handling the situation when deprecated function is # alias to existing function. qnames = { inferred.qname(), f"{inferred.parent.qname()}.{func_name}", func_name, } else: qnames = {inferred.qname(), func_name} if any(name in self.deprecated_methods() for name in qnames): self.add_message("deprecated-method", node=node, args=(func_name,)) return num_of_args = len(node.args) kwargs = {kw.arg for kw in node.keywords} if node.keywords else {} deprecated_arguments = (self.deprecated_arguments(qn) for qn in qnames) for position, arg_name in chain(*deprecated_arguments): if arg_name in kwargs: # function was called with deprecated argument as keyword argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) elif position is not None and position < num_of_args: # function was called with deprecated argument as positional argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) def check_deprecated_class(self, node, mod_name, class_names): """Checks if the class is deprecated.""" for class_name in class_names: if class_name in self.deprecated_classes(mod_name): self.add_message( "deprecated-class", node=node, args=(class_name, mod_name) ) def check_deprecated_class_in_call(self, node): """Checks if call the deprecated class.""" if isinstance(node.func, nodes.Attribute) and isinstance( node.func.expr, nodes.Name ): mod_name = node.func.expr.name class_name = node.func.attrname self.check_deprecated_class(node, mod_name, (class_name,))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
74
check_deprecated_module
ref
function
self.check_deprecated_module(node, name)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
78
check_deprecated_class
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
80
deprecated_decorators
def
function
def deprecated_decorators(self) -> Iterable: """Callback returning the deprecated decorators. Returns: collections.abc.Container of deprecated decorator names. """ # pylint: disable=no-self-use return () @utils.check_messages("deprecated-decorator") def visit_decorators(self, node: nodes.Decorators) -> None: """Triggered when a decorator statement is seen.""" children = list(node.get_children()) if not children: return if isinstance(children[0], nodes.Call): inf = safe_infer(children[0].func) else: inf = safe_infer(children[0]) qname = inf.qname() if inf else None if qname in self.deprecated_decorators(): self.add_message("deprecated-decorator", node=node, args=qname) @utils.check_messages( "deprecated-module", "deprecated-class", ) def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Triggered when a from statement is seen.""" basename = node.modname basename = get_import_name(node, basename) self.check_deprecated_module(node, basename) class_names = (name for name, _ in node.names) self.check_deprecated_class(node, basename, class_names) def deprecated_methods(self) -> Container[str]: """Callback returning the deprecated methods/functions. Returns: collections.abc.Container of deprecated function/method names. """ # pylint: disable=no-self-use return () def deprecated_arguments( self, method: str ) -> Iterable[Tuple[Union[int, None], str]]: """Callback returning the deprecated arguments of method/function. Args: method (str): name of function/method checked for deprecated arguments Returns: collections.abc.Iterable in form: ((POSITION1, PARAM1), (POSITION2: PARAM2) ...) where * POSITIONX - position of deprecated argument PARAMX in function definition. If argument is keyword-only, POSITIONX should be None. * PARAMX - name of the deprecated argument. E.g. suppose function: .. code-block:: python def bar(arg1, arg2, arg3, arg4, arg5='spam') with deprecated arguments `arg2` and `arg4`. `deprecated_arguments` should return: .. code-block:: python ((1, 'arg2'), (3, 'arg4')) """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def deprecated_modules(self) -> Iterable: """Callback returning the deprecated modules. Returns: collections.abc.Container of deprecated module names. """ # pylint: disable=no-self-use return () def deprecated_classes(self, module: str) -> Iterable: """Callback returning the deprecated classes of module. Args: module (str): name of module checked for deprecated classes Returns: collections.abc.Container of deprecated class names. """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def check_deprecated_module(self, node, mod_path): """Checks if the module is deprecated.""" for mod_name in self.deprecated_modules(): if mod_path == mod_name or mod_path.startswith(mod_name + "."): self.add_message("deprecated-module", node=node, args=mod_path) def check_deprecated_method(self, node, inferred): """Executes the checker for the given node. This method should be called from the checker implementing this mixin. """ # Reject nodes which aren't of interest to us. if not isinstance(inferred, ACCEPTABLE_NODES): return if isinstance(node.func, nodes.Attribute): func_name = node.func.attrname elif isinstance(node.func, nodes.Name): func_name = node.func.name else: # Not interested in other nodes. return if hasattr(inferred.parent, "qname") and inferred.parent.qname(): # Handling the situation when deprecated function is # alias to existing function. qnames = { inferred.qname(), f"{inferred.parent.qname()}.{func_name}", func_name, } else: qnames = {inferred.qname(), func_name} if any(name in self.deprecated_methods() for name in qnames): self.add_message("deprecated-method", node=node, args=(func_name,)) return num_of_args = len(node.args) kwargs = {kw.arg for kw in node.keywords} if node.keywords else {} deprecated_arguments = (self.deprecated_arguments(qn) for qn in qnames) for position, arg_name in chain(*deprecated_arguments): if arg_name in kwargs: # function was called with deprecated argument as keyword argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) elif position is not None and position < num_of_args: # function was called with deprecated argument as positional argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) def check_deprecated_class(self, node, mod_name, class_names): """Checks if the class is deprecated.""" for class_name in class_names: if class_name in self.deprecated_classes(mod_name): self.add_message( "deprecated-class", node=node, args=(class_name, mod_name) ) def check_deprecated_class_in_call(self, node): """Checks if call the deprecated class.""" if isinstance(node.func, nodes.Attribute) and isinstance( node.func.expr, nodes.Name ): mod_name = node.func.expr.name class_name = node.func.attrname self.check_deprecated_class(node, mod_name, (class_name,))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
89
check_messages
ref
function
@utils.check_messages("deprecated-decorator")
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
90
visit_decorators
def
function
def visit_decorators(self, node: nodes.Decorators) -> None: """Triggered when a decorator statement is seen.""" children = list(node.get_children()) if not children: return if isinstance(children[0], nodes.Call): inf = safe_infer(children[0].func) else: inf = safe_infer(children[0]) qname = inf.qname() if inf else None if qname in self.deprecated_decorators(): self.add_message("deprecated-decorator", node=node, args=qname) @utils.check_messages( "deprecated-module", "deprecated-class", ) def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Triggered when a from statement is seen.""" basename = node.modname basename = get_import_name(node, basename) self.check_deprecated_module(node, basename) class_names = (name for name, _ in node.names) self.check_deprecated_class(node, basename, class_names) def deprecated_methods(self) -> Container[str]: """Callback returning the deprecated methods/functions. Returns: collections.abc.Container of deprecated function/method names. """ # pylint: disable=no-self-use return () def deprecated_arguments( self, method: str ) -> Iterable[Tuple[Union[int, None], str]]: """Callback returning the deprecated arguments of method/function. Args: method (str): name of function/method checked for deprecated arguments Returns: collections.abc.Iterable in form: ((POSITION1, PARAM1), (POSITION2: PARAM2) ...) where * POSITIONX - position of deprecated argument PARAMX in function definition. If argument is keyword-only, POSITIONX should be None. * PARAMX - name of the deprecated argument. E.g. suppose function: .. code-block:: python def bar(arg1, arg2, arg3, arg4, arg5='spam') with deprecated arguments `arg2` and `arg4`. `deprecated_arguments` should return: .. code-block:: python ((1, 'arg2'), (3, 'arg4')) """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def deprecated_modules(self) -> Iterable: """Callback returning the deprecated modules. Returns: collections.abc.Container of deprecated module names. """ # pylint: disable=no-self-use return () def deprecated_classes(self, module: str) -> Iterable: """Callback returning the deprecated classes of module. Args: module (str): name of module checked for deprecated classes Returns: collections.abc.Container of deprecated class names. """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def check_deprecated_module(self, node, mod_path): """Checks if the module is deprecated.""" for mod_name in self.deprecated_modules(): if mod_path == mod_name or mod_path.startswith(mod_name + "."): self.add_message("deprecated-module", node=node, args=mod_path) def check_deprecated_method(self, node, inferred): """Executes the checker for the given node. This method should be called from the checker implementing this mixin. """ # Reject nodes which aren't of interest to us. if not isinstance(inferred, ACCEPTABLE_NODES): return if isinstance(node.func, nodes.Attribute): func_name = node.func.attrname elif isinstance(node.func, nodes.Name): func_name = node.func.name else: # Not interested in other nodes. return if hasattr(inferred.parent, "qname") and inferred.parent.qname(): # Handling the situation when deprecated function is # alias to existing function. qnames = { inferred.qname(), f"{inferred.parent.qname()}.{func_name}", func_name, } else: qnames = {inferred.qname(), func_name} if any(name in self.deprecated_methods() for name in qnames): self.add_message("deprecated-method", node=node, args=(func_name,)) return num_of_args = len(node.args) kwargs = {kw.arg for kw in node.keywords} if node.keywords else {} deprecated_arguments = (self.deprecated_arguments(qn) for qn in qnames) for position, arg_name in chain(*deprecated_arguments): if arg_name in kwargs: # function was called with deprecated argument as keyword argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) elif position is not None and position < num_of_args: # function was called with deprecated argument as positional argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) def check_deprecated_class(self, node, mod_name, class_names): """Checks if the class is deprecated.""" for class_name in class_names: if class_name in self.deprecated_classes(mod_name): self.add_message( "deprecated-class", node=node, args=(class_name, mod_name) ) def check_deprecated_class_in_call(self, node): """Checks if call the deprecated class.""" if isinstance(node.func, nodes.Attribute) and isinstance( node.func.expr, nodes.Name ): mod_name = node.func.expr.name class_name = node.func.attrname self.check_deprecated_class(node, mod_name, (class_name,))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
92
get_children
ref
function
children = list(node.get_children())
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
96
safe_infer
ref
function
inf = safe_infer(children[0].func)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
98
safe_infer
ref
function
inf = safe_infer(children[0])
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
99
qname
ref
function
qname = inf.qname() if inf else None
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
100
deprecated_decorators
ref
function
if qname in self.deprecated_decorators():
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
101
add_message
ref
function
self.add_message("deprecated-decorator", node=node, args=qname)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
103
check_messages
ref
function
@utils.check_messages(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
107
visit_importfrom
def
function
def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Triggered when a from statement is seen.""" basename = node.modname basename = get_import_name(node, basename) self.check_deprecated_module(node, basename) class_names = (name for name, _ in node.names) self.check_deprecated_class(node, basename, class_names) def deprecated_methods(self) -> Container[str]: """Callback returning the deprecated methods/functions. Returns: collections.abc.Container of deprecated function/method names. """ # pylint: disable=no-self-use return () def deprecated_arguments( self, method: str ) -> Iterable[Tuple[Union[int, None], str]]: """Callback returning the deprecated arguments of method/function. Args: method (str): name of function/method checked for deprecated arguments Returns: collections.abc.Iterable in form: ((POSITION1, PARAM1), (POSITION2: PARAM2) ...) where * POSITIONX - position of deprecated argument PARAMX in function definition. If argument is keyword-only, POSITIONX should be None. * PARAMX - name of the deprecated argument. E.g. suppose function: .. code-block:: python def bar(arg1, arg2, arg3, arg4, arg5='spam') with deprecated arguments `arg2` and `arg4`. `deprecated_arguments` should return: .. code-block:: python ((1, 'arg2'), (3, 'arg4')) """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def deprecated_modules(self) -> Iterable: """Callback returning the deprecated modules. Returns: collections.abc.Container of deprecated module names. """ # pylint: disable=no-self-use return () def deprecated_classes(self, module: str) -> Iterable: """Callback returning the deprecated classes of module. Args: module (str): name of module checked for deprecated classes Returns: collections.abc.Container of deprecated class names. """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def check_deprecated_module(self, node, mod_path): """Checks if the module is deprecated.""" for mod_name in self.deprecated_modules(): if mod_path == mod_name or mod_path.startswith(mod_name + "."): self.add_message("deprecated-module", node=node, args=mod_path) def check_deprecated_method(self, node, inferred): """Executes the checker for the given node. This method should be called from the checker implementing this mixin. """ # Reject nodes which aren't of interest to us. if not isinstance(inferred, ACCEPTABLE_NODES): return if isinstance(node.func, nodes.Attribute): func_name = node.func.attrname elif isinstance(node.func, nodes.Name): func_name = node.func.name else: # Not interested in other nodes. return if hasattr(inferred.parent, "qname") and inferred.parent.qname(): # Handling the situation when deprecated function is # alias to existing function. qnames = { inferred.qname(), f"{inferred.parent.qname()}.{func_name}", func_name, } else: qnames = {inferred.qname(), func_name} if any(name in self.deprecated_methods() for name in qnames): self.add_message("deprecated-method", node=node, args=(func_name,)) return num_of_args = len(node.args) kwargs = {kw.arg for kw in node.keywords} if node.keywords else {} deprecated_arguments = (self.deprecated_arguments(qn) for qn in qnames) for position, arg_name in chain(*deprecated_arguments): if arg_name in kwargs: # function was called with deprecated argument as keyword argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) elif position is not None and position < num_of_args: # function was called with deprecated argument as positional argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) def check_deprecated_class(self, node, mod_name, class_names): """Checks if the class is deprecated.""" for class_name in class_names: if class_name in self.deprecated_classes(mod_name): self.add_message( "deprecated-class", node=node, args=(class_name, mod_name) ) def check_deprecated_class_in_call(self, node): """Checks if call the deprecated class.""" if isinstance(node.func, nodes.Attribute) and isinstance( node.func.expr, nodes.Name ): mod_name = node.func.expr.name class_name = node.func.attrname self.check_deprecated_class(node, mod_name, (class_name,))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
110
get_import_name
ref
function
basename = get_import_name(node, basename)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
111
check_deprecated_module
ref
function
self.check_deprecated_module(node, basename)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
113
check_deprecated_class
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
115
deprecated_methods
def
function
def deprecated_methods(self) -> Container[str]: """Callback returning the deprecated methods/functions. Returns: collections.abc.Container of deprecated function/method names. """ # pylint: disable=no-self-use return () def deprecated_arguments( self, method: str ) -> Iterable[Tuple[Union[int, None], str]]: """Callback returning the deprecated arguments of method/function. Args: method (str): name of function/method checked for deprecated arguments Returns: collections.abc.Iterable in form: ((POSITION1, PARAM1), (POSITION2: PARAM2) ...) where * POSITIONX - position of deprecated argument PARAMX in function definition. If argument is keyword-only, POSITIONX should be None. * PARAMX - name of the deprecated argument. E.g. suppose function: .. code-block:: python def bar(arg1, arg2, arg3, arg4, arg5='spam') with deprecated arguments `arg2` and `arg4`. `deprecated_arguments` should return: .. code-block:: python ((1, 'arg2'), (3, 'arg4')) """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def deprecated_modules(self) -> Iterable: """Callback returning the deprecated modules. Returns: collections.abc.Container of deprecated module names. """ # pylint: disable=no-self-use return () def deprecated_classes(self, module: str) -> Iterable: """Callback returning the deprecated classes of module. Args: module (str): name of module checked for deprecated classes Returns: collections.abc.Container of deprecated class names. """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def check_deprecated_module(self, node, mod_path): """Checks if the module is deprecated.""" for mod_name in self.deprecated_modules(): if mod_path == mod_name or mod_path.startswith(mod_name + "."): self.add_message("deprecated-module", node=node, args=mod_path) def check_deprecated_method(self, node, inferred): """Executes the checker for the given node. This method should be called from the checker implementing this mixin. """ # Reject nodes which aren't of interest to us. if not isinstance(inferred, ACCEPTABLE_NODES): return if isinstance(node.func, nodes.Attribute): func_name = node.func.attrname elif isinstance(node.func, nodes.Name): func_name = node.func.name else: # Not interested in other nodes. return if hasattr(inferred.parent, "qname") and inferred.parent.qname(): # Handling the situation when deprecated function is # alias to existing function. qnames = { inferred.qname(), f"{inferred.parent.qname()}.{func_name}", func_name, } else: qnames = {inferred.qname(), func_name} if any(name in self.deprecated_methods() for name in qnames): self.add_message("deprecated-method", node=node, args=(func_name,)) return num_of_args = len(node.args) kwargs = {kw.arg for kw in node.keywords} if node.keywords else {} deprecated_arguments = (self.deprecated_arguments(qn) for qn in qnames) for position, arg_name in chain(*deprecated_arguments): if arg_name in kwargs: # function was called with deprecated argument as keyword argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) elif position is not None and position < num_of_args: # function was called with deprecated argument as positional argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) def check_deprecated_class(self, node, mod_name, class_names): """Checks if the class is deprecated.""" for class_name in class_names: if class_name in self.deprecated_classes(mod_name): self.add_message( "deprecated-class", node=node, args=(class_name, mod_name) ) def check_deprecated_class_in_call(self, node): """Checks if call the deprecated class.""" if isinstance(node.func, nodes.Attribute) and isinstance( node.func.expr, nodes.Name ): mod_name = node.func.expr.name class_name = node.func.attrname self.check_deprecated_class(node, mod_name, (class_name,))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
124
deprecated_arguments
def
function
def deprecated_arguments( self, method: str ) -> Iterable[Tuple[Union[int, None], str]]: """Callback returning the deprecated arguments of method/function. Args: method (str): name of function/method checked for deprecated arguments Returns: collections.abc.Iterable in form: ((POSITION1, PARAM1), (POSITION2: PARAM2) ...) where * POSITIONX - position of deprecated argument PARAMX in function definition. If argument is keyword-only, POSITIONX should be None. * PARAMX - name of the deprecated argument. E.g. suppose function: .. code-block:: python def bar(arg1, arg2, arg3, arg4, arg5='spam') with deprecated arguments `arg2` and `arg4`. `deprecated_arguments` should return: .. code-block:: python ((1, 'arg2'), (3, 'arg4')) """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def deprecated_modules(self) -> Iterable: """Callback returning the deprecated modules. Returns: collections.abc.Container of deprecated module names. """ # pylint: disable=no-self-use return () def deprecated_classes(self, module: str) -> Iterable: """Callback returning the deprecated classes of module. Args: module (str): name of module checked for deprecated classes Returns: collections.abc.Container of deprecated class names. """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def check_deprecated_module(self, node, mod_path): """Checks if the module is deprecated.""" for mod_name in self.deprecated_modules(): if mod_path == mod_name or mod_path.startswith(mod_name + "."): self.add_message("deprecated-module", node=node, args=mod_path) def check_deprecated_method(self, node, inferred): """Executes the checker for the given node. This method should be called from the checker implementing this mixin. """ # Reject nodes which aren't of interest to us. if not isinstance(inferred, ACCEPTABLE_NODES): return if isinstance(node.func, nodes.Attribute): func_name = node.func.attrname elif isinstance(node.func, nodes.Name): func_name = node.func.name else: # Not interested in other nodes. return if hasattr(inferred.parent, "qname") and inferred.parent.qname(): # Handling the situation when deprecated function is # alias to existing function. qnames = { inferred.qname(), f"{inferred.parent.qname()}.{func_name}", func_name, } else: qnames = {inferred.qname(), func_name} if any(name in self.deprecated_methods() for name in qnames): self.add_message("deprecated-method", node=node, args=(func_name,)) return num_of_args = len(node.args) kwargs = {kw.arg for kw in node.keywords} if node.keywords else {} deprecated_arguments = (self.deprecated_arguments(qn) for qn in qnames) for position, arg_name in chain(*deprecated_arguments): if arg_name in kwargs: # function was called with deprecated argument as keyword argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) elif position is not None and position < num_of_args: # function was called with deprecated argument as positional argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) def check_deprecated_class(self, node, mod_name, class_names): """Checks if the class is deprecated.""" for class_name in class_names: if class_name in self.deprecated_classes(mod_name): self.add_message( "deprecated-class", node=node, args=(class_name, mod_name) ) def check_deprecated_class_in_call(self, node): """Checks if call the deprecated class.""" if isinstance(node.func, nodes.Attribute) and isinstance( node.func.expr, nodes.Name ): mod_name = node.func.expr.name class_name = node.func.attrname self.check_deprecated_class(node, mod_name, (class_name,))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
153
deprecated_modules
def
function
def deprecated_modules(self) -> Iterable: """Callback returning the deprecated modules. Returns: collections.abc.Container of deprecated module names. """ # pylint: disable=no-self-use return () def deprecated_classes(self, module: str) -> Iterable: """Callback returning the deprecated classes of module. Args: module (str): name of module checked for deprecated classes Returns: collections.abc.Container of deprecated class names. """ # pylint: disable=no-self-use # pylint: disable=unused-argument return () def check_deprecated_module(self, node, mod_path): """Checks if the module is deprecated.""" for mod_name in self.deprecated_modules(): if mod_path == mod_name or mod_path.startswith(mod_name + "."): self.add_message("deprecated-module", node=node, args=mod_path) def check_deprecated_method(self, node, inferred): """Executes the checker for the given node. This method should be called from the checker implementing this mixin. """ # Reject nodes which aren't of interest to us. if not isinstance(inferred, ACCEPTABLE_NODES): return if isinstance(node.func, nodes.Attribute): func_name = node.func.attrname elif isinstance(node.func, nodes.Name): func_name = node.func.name else: # Not interested in other nodes. return if hasattr(inferred.parent, "qname") and inferred.parent.qname(): # Handling the situation when deprecated function is # alias to existing function. qnames = { inferred.qname(), f"{inferred.parent.qname()}.{func_name}", func_name, } else: qnames = {inferred.qname(), func_name} if any(name in self.deprecated_methods() for name in qnames): self.add_message("deprecated-method", node=node, args=(func_name,)) return num_of_args = len(node.args) kwargs = {kw.arg for kw in node.keywords} if node.keywords else {} deprecated_arguments = (self.deprecated_arguments(qn) for qn in qnames) for position, arg_name in chain(*deprecated_arguments): if arg_name in kwargs: # function was called with deprecated argument as keyword argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) elif position is not None and position < num_of_args: # function was called with deprecated argument as positional argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) def check_deprecated_class(self, node, mod_name, class_names): """Checks if the class is deprecated.""" for class_name in class_names: if class_name in self.deprecated_classes(mod_name): self.add_message( "deprecated-class", node=node, args=(class_name, mod_name) ) def check_deprecated_class_in_call(self, node): """Checks if call the deprecated class.""" if isinstance(node.func, nodes.Attribute) and isinstance( node.func.expr, nodes.Name ): mod_name = node.func.expr.name class_name = node.func.attrname self.check_deprecated_class(node, mod_name, (class_name,))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
162
deprecated_classes
def
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
175
check_deprecated_module
def
function
def check_deprecated_module(self, node, mod_path): """Checks if the module is deprecated.""" for mod_name in self.deprecated_modules(): if mod_path == mod_name or mod_path.startswith(mod_name + "."): self.add_message("deprecated-module", node=node, args=mod_path) def check_deprecated_method(self, node, inferred): """Executes the checker for the given node. This method should be called from the checker implementing this mixin. """ # Reject nodes which aren't of interest to us. if not isinstance(inferred, ACCEPTABLE_NODES): return if isinstance(node.func, nodes.Attribute): func_name = node.func.attrname elif isinstance(node.func, nodes.Name): func_name = node.func.name else: # Not interested in other nodes. return if hasattr(inferred.parent, "qname") and inferred.parent.qname(): # Handling the situation when deprecated function is # alias to existing function. qnames = { inferred.qname(), f"{inferred.parent.qname()}.{func_name}", func_name, } else: qnames = {inferred.qname(), func_name} if any(name in self.deprecated_methods() for name in qnames): self.add_message("deprecated-method", node=node, args=(func_name,)) return num_of_args = len(node.args) kwargs = {kw.arg for kw in node.keywords} if node.keywords else {} deprecated_arguments = (self.deprecated_arguments(qn) for qn in qnames) for position, arg_name in chain(*deprecated_arguments): if arg_name in kwargs: # function was called with deprecated argument as keyword argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) elif position is not None and position < num_of_args: # function was called with deprecated argument as positional argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) def check_deprecated_class(self, node, mod_name, class_names): """Checks if the class is deprecated.""" for class_name in class_names: if class_name in self.deprecated_classes(mod_name): self.add_message( "deprecated-class", node=node, args=(class_name, mod_name) ) def check_deprecated_class_in_call(self, node): """Checks if call the deprecated class.""" if isinstance(node.func, nodes.Attribute) and isinstance( node.func.expr, nodes.Name ): mod_name = node.func.expr.name class_name = node.func.attrname self.check_deprecated_class(node, mod_name, (class_name,))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
177
deprecated_modules
ref
function
for mod_name in self.deprecated_modules():
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
179
add_message
ref
function
self.add_message("deprecated-module", node=node, args=mod_path)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
181
check_deprecated_method
def
function
def check_deprecated_method(self, node, inferred): """Executes the checker for the given node. This method should be called from the checker implementing this mixin. """ # Reject nodes which aren't of interest to us. if not isinstance(inferred, ACCEPTABLE_NODES): return if isinstance(node.func, nodes.Attribute): func_name = node.func.attrname elif isinstance(node.func, nodes.Name): func_name = node.func.name else: # Not interested in other nodes. return if hasattr(inferred.parent, "qname") and inferred.parent.qname(): # Handling the situation when deprecated function is # alias to existing function. qnames = { inferred.qname(), f"{inferred.parent.qname()}.{func_name}", func_name, } else: qnames = {inferred.qname(), func_name} if any(name in self.deprecated_methods() for name in qnames): self.add_message("deprecated-method", node=node, args=(func_name,)) return num_of_args = len(node.args) kwargs = {kw.arg for kw in node.keywords} if node.keywords else {} deprecated_arguments = (self.deprecated_arguments(qn) for qn in qnames) for position, arg_name in chain(*deprecated_arguments): if arg_name in kwargs: # function was called with deprecated argument as keyword argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) elif position is not None and position < num_of_args: # function was called with deprecated argument as positional argument self.add_message( "deprecated-argument", node=node, args=(arg_name, func_name) ) def check_deprecated_class(self, node, mod_name, class_names): """Checks if the class is deprecated.""" for class_name in class_names: if class_name in self.deprecated_classes(mod_name): self.add_message( "deprecated-class", node=node, args=(class_name, mod_name) ) def check_deprecated_class_in_call(self, node): """Checks if call the deprecated class.""" if isinstance(node.func, nodes.Attribute) and isinstance( node.func.expr, nodes.Name ): mod_name = node.func.expr.name class_name = node.func.attrname self.check_deprecated_class(node, mod_name, (class_name,))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
198
qname
ref
function
if hasattr(inferred.parent, "qname") and inferred.parent.qname():
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
202
qname
ref
function
inferred.qname(),
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
203
qname
ref
function
f"{inferred.parent.qname()}.{func_name}",
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
207
qname
ref
function
qnames = {inferred.qname(), func_name}
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
208
deprecated_methods
ref
function
if any(name in self.deprecated_methods() for name in qnames):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
209
add_message
ref
function
self.add_message("deprecated-method", node=node, args=(func_name,))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
213
deprecated_arguments
ref
function
deprecated_arguments = (self.deprecated_arguments(qn) for qn in qnames)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
217
add_message
ref
function
self.add_message(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
222
add_message
ref
function
self.add_message(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
226
check_deprecated_class
def
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
230
deprecated_classes
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
231
add_message
ref
function
self.add_message(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
235
check_deprecated_class_in_call
def
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/deprecated.py
pylint/checkers/deprecated.py
243
check_deprecated_class
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
194
_is_exempt_from_public_methods
def
function
def _is_exempt_from_public_methods(node: astroid.ClassDef) -> bool: """Check if a class is exempt from too-few-public-methods.""" # If it's a typing.Namedtuple, typing.TypedDict or an Enum for ancestor in node.ancestors(): if ancestor.name == "Enum" and ancestor.root().name == "enum": return _True if ancestor.qname() in (TYPING_NAMEDTUPLE, TYPING_TYPEDDICT): return _True # Or if it's a dataclass if not node.decorators: return _False root_locals = set(node.root().locals) for decorator in node.decorators.nodes: if isinstance(decorator, astroid.Call): decorator = decorator.func if not isinstance(decorator, (astroid.Name, astroid.Attribute)): continue if isinstance(decorator, astroid.Name): name = decorator.name else: name = decorator.attrname if name in DATACLASSES_DECORATORS and ( root_locals.intersection(DATACLASSES_DECORATORS) or DATACLASS_IMPORT in root_locals ): return _True return _False
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
198
ancestors
ref
function
for ancestor in node.ancestors():
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
199
root
ref
function
if ancestor.name == "Enum" and ancestor.root().name == "enum":
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
201
qname
ref
function
if ancestor.qname() in (TYPING_NAMEDTUPLE, TYPING_TYPEDDICT):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
208
root
ref
function
root_locals = set(node.root().locals)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
226
_count_boolean_expressions
def
function
def _count_boolean_expressions(bool_op): """Counts the number of boolean expressions in BoolOp `bool_op` (recursive). example: a and (b or c or (d and e)) ==> 5 boolean expressions """ nb_bool_expr = 0 for bool_expr in bool_op.get_children(): if isinstance(bool_expr, astroid.BoolOp): nb_bool_expr += _count_boolean_expressions(bool_expr) else: nb_bool_expr += 1 return nb_bool_expr
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
232
get_children
ref
function
for bool_expr in bool_op.get_children():
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
234
_count_boolean_expressions
ref
function
nb_bool_expr += _count_boolean_expressions(bool_expr)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
240
_count_methods_in_class
def
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
241
methods
ref
function
all_methods = sum(1 for method in node.methods() if not method.name.startswith("_"))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
244
mymethods
ref
function
for method in node.mymethods():
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
250
_get_parents_iter
def
function
def _get_parents_iter( node: nodes.ClassDef, ignored_parents: FrozenSet[str]
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
269
ancestors
ref
function
to_explore = cast(List[nodes.ClassDef], list(node.ancestors(recurs=False)))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
272
qname
ref
function
if parent.qname() in ignored_parents:
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
283
ancestors
ref
function
to_explore.extend(parent.ancestors(recurs=False))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
286
_get_parents
def
function
def _get_parents( node: nodes.ClassDef, ignored_parents: FrozenSet[str]
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
289
_get_parents_iter
ref
function
return set(_get_parents_iter(node, ignored_parents))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
292
MisdesignChecker
def
class
__init__ open _inc_all_stmts _ignored_argument_names visit_classdef leave_classdef visit_functiondef leave_functiondef visit_return visit_default visit_tryexcept visit_tryfinally visit_if _check_boolean_expressions visit_while _inc_branch
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
431
reset_node_count
ref
function
self.linter.stats.reset_node_count()
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
435
get_global_option
ref
function
self._exclude_too_few_public_methods = utils.get_global_option(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
439
_inc_all_stmts
def
function
def _inc_all_stmts(self, amount): for i, _ in enumerate(self._stmts): self._stmts[i] += amount @astroid.decorators.cachedproperty def _ignored_argument_names(self): return utils.get_global_option(self, "ignored-argument-names", default=None) @check_messages( "too-many-ancestors", "too-many-instance-attributes", "too-few-public-methods", "too-many-public-methods", ) def visit_classdef(self, node: nodes.ClassDef) -> None: """Check size of inheritance hierarchy and number of instance attributes.""" parents = _get_parents( node, STDLIB_CLASSES_IGNORE_ANCESTOR.union(self.config.ignored_parents) ) nb_parents = len(parents) if nb_parents > self.config.max_parents: self.add_message( "too-many-ancestors", node=node, args=(nb_parents, self.config.max_parents), ) if len(node.instance_attrs) > self.config.max_attributes: self.add_message( "too-many-instance-attributes", node=node, args=(len(node.instance_attrs), self.config.max_attributes), ) @check_messages("too-few-public-methods", "too-many-public-methods") def leave_classdef(self, node: nodes.ClassDef) -> None: """Check number of public methods.""" my_methods = sum( 1 for method in node.mymethods() if not method.name.startswith("_") ) # Does the class contain less than n public methods ? # This checks only the methods defined in the current class, # since the user might not have control over the classes # from the ancestors. It avoids some false positives # for classes such as unittest.TestCase, which provides # a lot of assert methods. It doesn't make sense to warn # when the user subclasses TestCase to add his own tests. if my_methods > self.config.max_public_methods: self.add_message( "too-many-public-methods", node=node, args=(my_methods, self.config.max_public_methods), ) # Stop here if the class is excluded via configuration. if node.type == "class" and self._exclude_too_few_public_methods: for ancestor in node.ancestors(): if any( pattern.match(ancestor.qname()) for pattern in self._exclude_too_few_public_methods ): return # Stop here for exception, metaclass, interface classes and other # classes for which we don't need to count the methods. if node.type != "class" or _is_exempt_from_public_methods(node): return # Does the class contain more than n public methods ? # This checks all the methods defined by ancestors and # by the current class. all_methods = _count_methods_in_class(node) if all_methods < self.config.min_public_methods: self.add_message( "too-few-public-methods", node=node, args=(all_methods, self.config.min_public_methods), ) @check_messages( "too-many-return-statements", "too-many-branches", "too-many-arguments", "too-many-locals", "too-many-statements", "keyword-arg-before-vararg", ) def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Check function name, docstring, arguments, redefinition, variable names, max locals """ # init branch and returns counters self._returns.append(0) # check number of arguments args = node.args.args ignored_argument_names = self._ignored_argument_names if args is not None: ignored_args_num = 0 if ignored_argument_names: ignored_args_num = sum( 1 for arg in args if ignored_argument_names.match(arg.name) ) argnum = len(args) - ignored_args_num if argnum > self.config.max_args: self.add_message( "too-many-arguments", node=node, args=(len(args), self.config.max_args), ) else: ignored_args_num = 0 # check number of local variables locnum = len(node.locals) - ignored_args_num if locnum > self.config.max_locals: self.add_message( "too-many-locals", node=node, args=(locnum, self.config.max_locals) ) # init new statements counter self._stmts.append(1) visit_asyncfunctiondef = visit_functiondef @check_messages( "too-many-return-statements", "too-many-branches", "too-many-arguments", "too-many-locals", "too-many-statements", ) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Most of the work is done here on close: checks for max returns, branch, return in __init__ """ returns = self._returns.pop() if returns > self.config.max_returns: self.add_message( "too-many-return-statements", node=node, args=(returns, self.config.max_returns), ) branches = self._branches[node] if branches > self.config.max_branches: self.add_message( "too-many-branches", node=node, args=(branches, self.config.max_branches), ) # check number of statements stmts = self._stmts.pop() if stmts > self.config.max_statements: self.add_message( "too-many-statements", node=node, args=(stmts, self.config.max_statements), ) leave_asyncfunctiondef = leave_functiondef def visit_return(self, _: nodes.Return) -> None: """Count number of returns.""" if not self._returns: return # return outside function, reported by the base checker self._returns[-1] += 1 def visit_default(self, node: nodes.NodeNG) -> None: """Default visit method -> increments the statements counter if necessary """ if node.is_statement: self._inc_all_stmts(1) def visit_tryexcept(self, node: nodes.TryExcept) -> None: """Increments the branches counter.""" branches = len(node.handlers) if node.orelse: branches += 1 self._inc_branch(node, branches) self._inc_all_stmts(branches) def visit_tryfinally(self, node: nodes.TryFinally) -> None: """Increments the branches counter.""" self._inc_branch(node, 2) self._inc_all_stmts(2) @check_messages("too-many-boolean-expressions") def visit_if(self, node: nodes.If) -> None: """Increments the branches counter and checks boolean expressions.""" self._check_boolean_expressions(node) branches = 1 # don't double count If nodes coming from some 'elif' if node.orelse and ( len(node.orelse) > 1 or not isinstance(node.orelse[0], astroid.If) ): branches += 1 self._inc_branch(node, branches) self._inc_all_stmts(branches) def _check_boolean_expressions(self, node): """Go through "if" node `node` and count its boolean expressions if the 'if' node test is a BoolOp node """ condition = node.test if not isinstance(condition, astroid.BoolOp): return nb_bool_expr = _count_boolean_expressions(condition) if nb_bool_expr > self.config.max_bool_expr: self.add_message( "too-many-boolean-expressions", node=condition, args=(nb_bool_expr, self.config.max_bool_expr), ) def visit_while(self, node: nodes.While) -> None: """Increments the branches counter.""" branches = 1 if node.orelse: branches += 1 self._inc_branch(node, branches) visit_for = visit_while def _inc_branch(self, node, branchesnum=1): """Increments the branches counter.""" self._branches[node.scope()] += branchesnum
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
444
_ignored_argument_names
def
function
def _ignored_argument_names(self): return utils.get_global_option(self, "ignored-argument-names", default=None) @check_messages( "too-many-ancestors", "too-many-instance-attributes", "too-few-public-methods", "too-many-public-methods", ) def visit_classdef(self, node: nodes.ClassDef) -> None: """Check size of inheritance hierarchy and number of instance attributes.""" parents = _get_parents( node, STDLIB_CLASSES_IGNORE_ANCESTOR.union(self.config.ignored_parents) ) nb_parents = len(parents) if nb_parents > self.config.max_parents: self.add_message( "too-many-ancestors", node=node, args=(nb_parents, self.config.max_parents), ) if len(node.instance_attrs) > self.config.max_attributes: self.add_message( "too-many-instance-attributes", node=node, args=(len(node.instance_attrs), self.config.max_attributes), ) @check_messages("too-few-public-methods", "too-many-public-methods") def leave_classdef(self, node: nodes.ClassDef) -> None: """Check number of public methods.""" my_methods = sum( 1 for method in node.mymethods() if not method.name.startswith("_") ) # Does the class contain less than n public methods ? # This checks only the methods defined in the current class, # since the user might not have control over the classes # from the ancestors. It avoids some false positives # for classes such as unittest.TestCase, which provides # a lot of assert methods. It doesn't make sense to warn # when the user subclasses TestCase to add his own tests. if my_methods > self.config.max_public_methods: self.add_message( "too-many-public-methods", node=node, args=(my_methods, self.config.max_public_methods), ) # Stop here if the class is excluded via configuration. if node.type == "class" and self._exclude_too_few_public_methods: for ancestor in node.ancestors(): if any( pattern.match(ancestor.qname()) for pattern in self._exclude_too_few_public_methods ): return # Stop here for exception, metaclass, interface classes and other # classes for which we don't need to count the methods. if node.type != "class" or _is_exempt_from_public_methods(node): return # Does the class contain more than n public methods ? # This checks all the methods defined by ancestors and # by the current class. all_methods = _count_methods_in_class(node) if all_methods < self.config.min_public_methods: self.add_message( "too-few-public-methods", node=node, args=(all_methods, self.config.min_public_methods), ) @check_messages( "too-many-return-statements", "too-many-branches", "too-many-arguments", "too-many-locals", "too-many-statements", "keyword-arg-before-vararg", ) def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Check function name, docstring, arguments, redefinition, variable names, max locals """ # init branch and returns counters self._returns.append(0) # check number of arguments args = node.args.args ignored_argument_names = self._ignored_argument_names if args is not None: ignored_args_num = 0 if ignored_argument_names: ignored_args_num = sum( 1 for arg in args if ignored_argument_names.match(arg.name) ) argnum = len(args) - ignored_args_num if argnum > self.config.max_args: self.add_message( "too-many-arguments", node=node, args=(len(args), self.config.max_args), ) else: ignored_args_num = 0 # check number of local variables locnum = len(node.locals) - ignored_args_num if locnum > self.config.max_locals: self.add_message( "too-many-locals", node=node, args=(locnum, self.config.max_locals) ) # init new statements counter self._stmts.append(1) visit_asyncfunctiondef = visit_functiondef @check_messages( "too-many-return-statements", "too-many-branches", "too-many-arguments", "too-many-locals", "too-many-statements", ) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Most of the work is done here on close: checks for max returns, branch, return in __init__ """ returns = self._returns.pop() if returns > self.config.max_returns: self.add_message( "too-many-return-statements", node=node, args=(returns, self.config.max_returns), ) branches = self._branches[node] if branches > self.config.max_branches: self.add_message( "too-many-branches", node=node, args=(branches, self.config.max_branches), ) # check number of statements stmts = self._stmts.pop() if stmts > self.config.max_statements: self.add_message( "too-many-statements", node=node, args=(stmts, self.config.max_statements), ) leave_asyncfunctiondef = leave_functiondef def visit_return(self, _: nodes.Return) -> None: """Count number of returns.""" if not self._returns: return # return outside function, reported by the base checker self._returns[-1] += 1 def visit_default(self, node: nodes.NodeNG) -> None: """Default visit method -> increments the statements counter if necessary """ if node.is_statement: self._inc_all_stmts(1) def visit_tryexcept(self, node: nodes.TryExcept) -> None: """Increments the branches counter.""" branches = len(node.handlers) if node.orelse: branches += 1 self._inc_branch(node, branches) self._inc_all_stmts(branches) def visit_tryfinally(self, node: nodes.TryFinally) -> None: """Increments the branches counter.""" self._inc_branch(node, 2) self._inc_all_stmts(2) @check_messages("too-many-boolean-expressions") def visit_if(self, node: nodes.If) -> None: """Increments the branches counter and checks boolean expressions.""" self._check_boolean_expressions(node) branches = 1 # don't double count If nodes coming from some 'elif' if node.orelse and ( len(node.orelse) > 1 or not isinstance(node.orelse[0], astroid.If) ): branches += 1 self._inc_branch(node, branches) self._inc_all_stmts(branches) def _check_boolean_expressions(self, node): """Go through "if" node `node` and count its boolean expressions if the 'if' node test is a BoolOp node """ condition = node.test if not isinstance(condition, astroid.BoolOp): return nb_bool_expr = _count_boolean_expressions(condition) if nb_bool_expr > self.config.max_bool_expr: self.add_message( "too-many-boolean-expressions", node=condition, args=(nb_bool_expr, self.config.max_bool_expr), ) def visit_while(self, node: nodes.While) -> None: """Increments the branches counter.""" branches = 1 if node.orelse: branches += 1 self._inc_branch(node, branches) visit_for = visit_while def _inc_branch(self, node, branchesnum=1): """Increments the branches counter.""" self._branches[node.scope()] += branchesnum
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
445
get_global_option
ref
function
return utils.get_global_option(self, "ignored-argument-names", default=None)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
447
check_messages
ref
function
@check_messages(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
453
visit_classdef
def
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
455
_get_parents
ref
function
parents = _get_parents(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
460
add_message
ref
function
self.add_message(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
467
add_message
ref
function
self.add_message(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
473
check_messages
ref
function
@check_messages("too-few-public-methods", "too-many-public-methods")
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
474
leave_classdef
def
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
477
mymethods
ref
function
1 for method in node.mymethods() if not method.name.startswith("_")
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
488
add_message
ref
function
self.add_message(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
496
ancestors
ref
function
for ancestor in node.ancestors():
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
498
qname
ref
function
pattern.match(ancestor.qname())
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
505
_is_exempt_from_public_methods
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
511
_count_methods_in_class
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
513
add_message
ref
function
self.add_message(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
519
check_messages
ref
function
@check_messages(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
527
visit_functiondef
def
function
def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Check function name, docstring, arguments, redefinition, variable names, max locals """ # init branch and returns counters self._returns.append(0) # check number of arguments args = node.args.args ignored_argument_names = self._ignored_argument_names if args is not None: ignored_args_num = 0 if ignored_argument_names: ignored_args_num = sum( 1 for arg in args if ignored_argument_names.match(arg.name) ) argnum = len(args) - ignored_args_num if argnum > self.config.max_args: self.add_message( "too-many-arguments", node=node, args=(len(args), self.config.max_args), ) else: ignored_args_num = 0 # check number of local variables locnum = len(node.locals) - ignored_args_num if locnum > self.config.max_locals: self.add_message( "too-many-locals", node=node, args=(locnum, self.config.max_locals) ) # init new statements counter self._stmts.append(1) visit_asyncfunctiondef = visit_functiondef @check_messages( "too-many-return-statements", "too-many-branches", "too-many-arguments", "too-many-locals", "too-many-statements", ) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Most of the work is done here on close: checks for max returns, branch, return in __init__ """ returns = self._returns.pop() if returns > self.config.max_returns: self.add_message( "too-many-return-statements", node=node, args=(returns, self.config.max_returns), ) branches = self._branches[node] if branches > self.config.max_branches: self.add_message( "too-many-branches", node=node, args=(branches, self.config.max_branches), ) # check number of statements stmts = self._stmts.pop() if stmts > self.config.max_statements: self.add_message( "too-many-statements", node=node, args=(stmts, self.config.max_statements), ) leave_asyncfunctiondef = leave_functiondef def visit_return(self, _: nodes.Return) -> None: """Count number of returns.""" if not self._returns: return # return outside function, reported by the base checker self._returns[-1] += 1 def visit_default(self, node: nodes.NodeNG) -> None: """Default visit method -> increments the statements counter if necessary """ if node.is_statement: self._inc_all_stmts(1) def visit_tryexcept(self, node: nodes.TryExcept) -> None: """Increments the branches counter.""" branches = len(node.handlers) if node.orelse: branches += 1 self._inc_branch(node, branches) self._inc_all_stmts(branches) def visit_tryfinally(self, node: nodes.TryFinally) -> None: """Increments the branches counter.""" self._inc_branch(node, 2) self._inc_all_stmts(2) @check_messages("too-many-boolean-expressions") def visit_if(self, node: nodes.If) -> None: """Increments the branches counter and checks boolean expressions.""" self._check_boolean_expressions(node) branches = 1 # don't double count If nodes coming from some 'elif' if node.orelse and ( len(node.orelse) > 1 or not isinstance(node.orelse[0], astroid.If) ): branches += 1 self._inc_branch(node, branches) self._inc_all_stmts(branches) def _check_boolean_expressions(self, node): """Go through "if" node `node` and count its boolean expressions if the 'if' node test is a BoolOp node """ condition = node.test if not isinstance(condition, astroid.BoolOp): return nb_bool_expr = _count_boolean_expressions(condition) if nb_bool_expr > self.config.max_bool_expr: self.add_message( "too-many-boolean-expressions", node=condition, args=(nb_bool_expr, self.config.max_bool_expr), ) def visit_while(self, node: nodes.While) -> None: """Increments the branches counter.""" branches = 1 if node.orelse: branches += 1 self._inc_branch(node, branches) visit_for = visit_while def _inc_branch(self, node, branchesnum=1): """Increments the branches counter.""" self._branches[node.scope()] += branchesnum
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/design_analysis.py
pylint/checkers/design_analysis.py
545
add_message
ref
function
self.add_message(