diff options
author | Andreas Finkler <3929834+DudeNr33@users.noreply.github.com> | 2022-05-29 14:56:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-29 14:56:52 +0200 |
commit | 995ebf23906afdd10263e79e35cb5186c16c65f6 (patch) | |
tree | a9e034d8b107b62133ca20eb726f7c550300eebb /pylint/pyreverse | |
parent | 528e81b703d30996676dec419404230daf457b10 (diff) | |
download | pylint-git-995ebf23906afdd10263e79e35cb5186c16c65f6.tar.gz |
Fix `pyreverse` type annotations for attributes using alternative union syntax (#6720)
* Fix `pyreverse` type annotations for attributes using alternative union syntax.
* Fix `dot` output and add additional test cases
* Apply suggestions from code review
* Add changelog entry in `full.rst`
Diffstat (limited to 'pylint/pyreverse')
-rw-r--r-- | pylint/pyreverse/diagrams.py | 4 | ||||
-rw-r--r-- | pylint/pyreverse/dot_printer.py | 11 | ||||
-rw-r--r-- | pylint/pyreverse/utils.py | 4 |
3 files changed, 9 insertions, 10 deletions
diff --git a/pylint/pyreverse/diagrams.py b/pylint/pyreverse/diagrams.py index 7cd5333f3..7b15bf816 100644 --- a/pylint/pyreverse/diagrams.py +++ b/pylint/pyreverse/diagrams.py @@ -159,7 +159,9 @@ class ClassDiagram(Figure, FilterMixIn): if isinstance(node, astroid.Instance): node = node._proxied if ( - isinstance(node, (nodes.ClassDef, nodes.Name, nodes.Subscript)) + isinstance( + node, (nodes.ClassDef, nodes.Name, nodes.Subscript, nodes.BinOp) + ) and hasattr(node, "name") and not self.has_node(node) ): diff --git a/pylint/pyreverse/dot_printer.py b/pylint/pyreverse/dot_printer.py index df4e94a00..883682704 100644 --- a/pylint/pyreverse/dot_printer.py +++ b/pylint/pyreverse/dot_printer.py @@ -81,24 +81,19 @@ class DotPrinter(Printer): f'"{name}" [color="{color}"{fontcolor_part}{label_part}, shape="{shape}", style="{style}"];' ) - def _build_label_for_node( - self, properties: NodeProperties, is_interface: bool | None = False - ) -> str: + def _build_label_for_node(self, properties: NodeProperties) -> 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|" + attrs_string = r"\l".join(attr.replace("|", r"\|") for attr in attrs) + label = rf"{{{label}|{attrs_string}\l|" # Add class methods methods: list[nodes.FunctionDef] = properties.methods or [] diff --git a/pylint/pyreverse/utils.py b/pylint/pyreverse/utils.py index 7052791ec..b1be195e3 100644 --- a/pylint/pyreverse/utils.py +++ b/pylint/pyreverse/utils.py @@ -218,7 +218,9 @@ def infer_node(node: nodes.AssignAttr | nodes.AssignName) -> set[Any]: ann = get_annotation(node) try: if ann: - if isinstance(ann, nodes.Subscript): + if isinstance(ann, nodes.Subscript) or ( + isinstance(ann, nodes.BinOp) and ann.op == "|" + ): return {ann} return set(ann.infer()) return set(node.infer()) |