summaryrefslogtreecommitdiff
path: root/pylint/pyreverse/dot_printer.py
diff options
context:
space:
mode:
authorDudeNr33 <3929834+DudeNr33@users.noreply.github.com>2021-08-07 14:40:20 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-08-08 09:13:43 +0200
commit51e4e69ec3e497781641bc657ad8f41ac90ab6b5 (patch)
tree8b1d56b019dd48c2ab98d23709d981f9a0ea316c /pylint/pyreverse/dot_printer.py
parent4c659d252230604fa12f537873e7838c3b801d1d (diff)
downloadpylint-git-51e4e69ec3e497781641bc657ad8f41ac90ab6b5.tar.gz
Refactor to reduce indentation and pull all "label building" logic together
Diffstat (limited to 'pylint/pyreverse/dot_printer.py')
-rw-r--r--pylint/pyreverse/dot_printer.py81
1 files changed, 47 insertions, 34 deletions
diff --git a/pylint/pyreverse/dot_printer.py b/pylint/pyreverse/dot_printer.py
index 37c21562a..eb1a744d6 100644
--- a/pylint/pyreverse/dot_printer.py
+++ b/pylint/pyreverse/dot_printer.py
@@ -69,8 +69,6 @@ class DotPrinter(Printer):
color = properties.color if properties.color is not None else "black"
label = self._build_label_for_node(properties)
if label:
- if type_ is NodeType.INTERFACE:
- label = "<<interface>>\\n" + label
label_part = f', label="{label}"'
else:
label_part = ""
@@ -82,39 +80,54 @@ class DotPrinter(Printer):
)
@staticmethod
- def _build_label_for_node(properties: NodeProperties) -> str:
- label = properties.label
- if label and properties.attrs is not None and properties.methods is not None:
- methods: List[str] = []
- for func in properties.methods:
- return_type = (
- f": {get_annotation_label(func.returns)}" if func.returns else ""
- )
-
- if func.args.args:
- arguments: List[astroid.AssignName] = [
- arg for arg in func.args.args if arg.name != "self"
- ]
- else:
- arguments = []
-
- annotations = dict(zip(arguments, func.args.annotations[1:]))
- for arg in arguments:
- annotation_label = ""
- ann = annotations.get(arg)
- if ann:
- annotation_label = get_annotation_label(ann)
- annotations[arg] = annotation_label
-
- args = ", ".join(
- f"{arg.name}: {ann}" if ann else f"{arg.name}"
- for arg, ann in annotations.items()
- )
-
- methods.append(fr"{func.name}({args}){return_type}\l")
- label = r"{{{}|{}\l|{}}}".format(
- label, r"\l".join(properties.attrs), "".join(methods)
+ def _build_label_for_node(
+ properties: NodeProperties, is_interface: Optional[bool] = False
+ ) -> str:
+ if not properties.label:
+ return ""
+
+ label: str = properties.label
+ if is_interface:
+ # add a stereotype
+ label = "<<interface>>\\n" + label
+
+ if properties.attrs is None and properties.methods is None:
+ # return a "compact" form which only displays the class name in a box
+ return label
+
+ # Add class attributes
+ attrs: List[str] = properties.attrs or []
+ label = "{" + label + "|" + r"\l".join(attrs) + r"\l|"
+
+ # Add class methods
+ methods: List[astroid.FunctionDef] = properties.methods or []
+ for func in methods:
+ return_type = (
+ f": {get_annotation_label(func.returns)}" if func.returns else ""
)
+
+ if func.args.args:
+ arguments: List[astroid.AssignName] = [
+ arg for arg in func.args.args if arg.name != "self"
+ ]
+ else:
+ arguments = []
+
+ annotations = dict(zip(arguments, func.args.annotations[1:]))
+ for arg in arguments:
+ annotation_label = ""
+ ann = annotations.get(arg)
+ if ann:
+ annotation_label = get_annotation_label(ann)
+ annotations[arg] = annotation_label
+
+ args = ", ".join(
+ f"{arg.name}: {ann}" if ann else f"{arg.name}"
+ for arg, ann in annotations.items()
+ )
+
+ label += fr"{func.name}({args}){return_type}\l"
+ label += "}"
return label
def emit_edge(