summaryrefslogtreecommitdiff
path: root/pylint
diff options
context:
space:
mode:
authorTéo Bouvard <teobouvard@gmail.com>2022-03-09 17:43:40 +0100
committerGitHub <noreply@github.com>2022-03-09 17:43:40 +0100
commit1ebdc8c59f31962db0244a79eaab9b8d90a87baf (patch)
tree30e3ec2963f3109a8371b38217f4318aca04835e /pylint
parentb1bb7f86bf08876e909fe135802a262fb2e5d82f (diff)
downloadpylint-git-1ebdc8c59f31962db0244a79eaab9b8d90a87baf.tar.gz
Fix pyreverse type hinting for class methods (#5881)
* Fix pyreverse type hinting for class methods This commit fixes the alignment of arguments and their type annotations in pyreverse printer output. It does so by checking for the type of the current function rather than the name of the first argument. This allows class methods having a non-standard first argument (different from "self" or "cls") to be correctly serialized in class diagrams. * Add test for method with None args According to astroid docs, this happens for builtin functions implemented in C. In this case, we return an empty argument list.
Diffstat (limited to 'pylint')
-rw-r--r--pylint/pyreverse/printer.py15
1 files changed, 7 insertions, 8 deletions
diff --git a/pylint/pyreverse/printer.py b/pylint/pyreverse/printer.py
index 559e456be..ca7f9b0a2 100644
--- a/pylint/pyreverse/printer.py
+++ b/pylint/pyreverse/printer.py
@@ -99,14 +99,13 @@ class Printer(ABC):
@staticmethod
def _get_method_arguments(method: nodes.FunctionDef) -> List[str]:
- if method.args.args:
- arguments: List[nodes.AssignName] = [
- arg for arg in method.args.args if arg.name != "self"
- ]
- else:
- arguments = []
-
- annotations = dict(zip(arguments, method.args.annotations[1:]))
+ if method.args.args is None:
+ return []
+
+ first_arg = 0 if method.type in {"function", "staticmethod"} else 1
+ arguments: List[nodes.AssignName] = method.args.args[first_arg:]
+
+ annotations = dict(zip(arguments, method.args.annotations[first_arg:]))
for arg in arguments:
annotation_label = ""
ann = annotations.get(arg)