# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE # Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt from __future__ import annotations from typing import TYPE_CHECKING from astroid import Instance, nodes from astroid.util import UninferableBase from pylint.checkers import BaseChecker from pylint.checkers.utils import safe_infer from pylint.constants import DUNDER_METHODS from pylint.interfaces import HIGH if TYPE_CHECKING: from pylint.lint import PyLinter class DunderCallChecker(BaseChecker): """Check for unnecessary dunder method calls. Docs: https://docs.python.org/3/reference/datamodel.html#basic-customization We exclude names in list pylint.constants.EXTRA_DUNDER_METHODS such as __index__ (see https://github.com/PyCQA/pylint/issues/6795) since these either have no alternative method of being called or have a genuine use case for being called manually. Additionally, we exclude classes that are not instantiated since these might be used to access the dunder methods of a base class of an instance. We also exclude dunder method calls on super() since these can't be written in an alternative manner. """ name = "unnecessary-dunder-call" priority = -1 msgs = { "C2801": ( "Unnecessarily calls dunder method %s. %s.", "unnecessary-dunder-call", "Used when a dunder method is manually called instead " "of using the corresponding function/method/operator.", ), } options = () def open(self) -> None: self._dunder_methods: dict[str, str] = {} for since_vers, dunder_methods in DUNDER_METHODS.items(): if since_vers <= self.linter.config.py_version: self._dunder_methods.update(dunder_methods) @staticmethod def within_dunder_def(node: nodes.NodeNG) -> bool: """Check if dunder method call is within a dunder method definition.""" parent = node.parent while parent is not None: if ( isinstance(parent, nodes.FunctionDef) and parent.name.startswith("__") and parent.name.endswith("__") ): return True parent = parent.parent return False def visit_call(self, node: nodes.Call) -> None: """Check if method being called is an unnecessary dunder method.""" if ( isinstance(node.func, nodes.Attribute) and node.func.attrname in self._dunder_methods and not self.within_dunder_def(node) and not ( isinstance(node.func.expr, nodes.Call) and isinstance(node.func.expr.func, nodes.Name) and node.func.expr.func.name == "super" ) ): inf_expr = safe_infer(node.func.expr) if not ( inf_expr is None or isinstance(inf_expr, (Instance, UninferableBase)) ): # Skip dunder calls to non instantiated classes. return self.add_message( "unnecessary-dunder-call", node=node, args=(node.func.attrname, self._dunder_methods[node.func.attrname]), confidence=HIGH, ) def register(linter: PyLinter) -> None: linter.register_checker(DunderCallChecker(linter))
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
base | Folder | 0755 |
|
|
classes | Folder | 0755 |
|
|
refactoring | Folder | 0755 |
|
|
__init__.py | File | 4.26 KB | 0644 |
|
async.py | File | 3.83 KB | 0644 |
|
bad_chained_comparison.py | File | 2.18 KB | 0644 |
|
base_checker.py | File | 10.67 KB | 0644 |
|
deprecated.py | File | 9.43 KB | 0644 |
|
design_analysis.py | File | 21.62 KB | 0644 |
|
dunder_methods.py | File | 3.43 KB | 0644 |
|
ellipsis_checker.py | File | 1.97 KB | 0644 |
|
exceptions.py | File | 26.05 KB | 0644 |
|
format.py | File | 26.91 KB | 0644 |
|
imports.py | File | 41.31 KB | 0644 |
|
lambda_expressions.py | File | 3.38 KB | 0644 |
|
logging.py | File | 15.84 KB | 0644 |
|
mapreduce_checker.py | File | 1.08 KB | 0644 |
|
method_args.py | File | 4.68 KB | 0644 |
|
misc.py | File | 4.87 KB | 0644 |
|
modified_iterating_checker.py | File | 7.67 KB | 0644 |
|
nested_min_max.py | File | 3.63 KB | 0644 |
|
newstyle.py | File | 4.46 KB | 0644 |
|
non_ascii_names.py | File | 6.98 KB | 0644 |
|
raw_metrics.py | File | 3.81 KB | 0644 |
|
similar.py | File | 33.29 KB | 0644 |
|
spelling.py | File | 16.17 KB | 0644 |
|
stdlib.py | File | 31.28 KB | 0644 |
|
strings.py | File | 40.28 KB | 0644 |
|
threading_checker.py | File | 1.9 KB | 0644 |
|
typecheck.py | File | 86.83 KB | 0644 |
|
unicode.py | File | 18.05 KB | 0644 |
|
unsupported_version.py | File | 2.93 KB | 0644 |
|
utils.py | File | 77.26 KB | 0644 |
|
variables.py | File | 126.57 KB | 0644 |
|