Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Pyreverse optional annotation bug #9016

Merged
merged 3 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9014.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Don't add `Optional` to `|` annotations with `None` in Pyreverse diagrams.

Closes #9014
22 changes: 15 additions & 7 deletions pylint/pyreverse/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,22 @@ def get_annotation(
default = ""

label = get_annotation_label(ann)
if ann:
label = (
rf"Optional[{label}]"
if getattr(default, "value", "value") is None
and not label.startswith("Optional")
else label

if (
ann
and getattr(default, "value", "value") is None
and not label.startswith("Optional")
and (
not isinstance(ann, nodes.BinOp)
or not any(
isinstance(child, nodes.Const) and child.value is None
for child in ann.get_children()
)
)
if label:
):
label = rf"Optional[{label}]"

if label and ann:
ann.name = label
return ann

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ digraph "classes" {
rankdir=BT
charset="utf-8"
"attributes_annotation.Dummy" [color="black", fontcolor="black", label=<{Dummy|<br ALIGN="LEFT"/>|}>, shape="record", style="solid"];
"attributes_annotation.Dummy2" [color="black", fontcolor="black", label=<{Dummy2|alternative_union_syntax : str \| int<br ALIGN="LEFT"/>class_attr : list[Dummy]<br ALIGN="LEFT"/>optional : Optional[Dummy]<br ALIGN="LEFT"/>param : str<br ALIGN="LEFT"/>union : Union[int, str]<br ALIGN="LEFT"/>|}>, shape="record", style="solid"];
"attributes_annotation.Dummy2" [color="black", fontcolor="black", label=<{Dummy2|alternative_optional : int \| None<br ALIGN="LEFT"/>alternative_optional_swapped : None \| int<br ALIGN="LEFT"/>alternative_union_syntax : str \| int<br ALIGN="LEFT"/>class_attr : list[Dummy]<br ALIGN="LEFT"/>optional : Optional[Dummy]<br ALIGN="LEFT"/>optional_union : Optional[int \| str]<br ALIGN="LEFT"/>param : str<br ALIGN="LEFT"/>union : Union[int, str]<br ALIGN="LEFT"/>|}>, shape="record", style="solid"];
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ classDiagram
class Dummy {
}
class Dummy2 {
alternative_optional : int | None
alternative_optional_swapped : None | int
alternative_union_syntax : str | int
class_attr : list[Dummy]
optional : Optional[Dummy]
optional_union : Optional[int | str]
param : str
union : Union[int, str]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ set namespaceSeparator none
class "Dummy" as attributes_annotation.Dummy {
}
class "Dummy2" as attributes_annotation.Dummy2 {
alternative_optional : int | None
alternative_optional_swapped : None | int
alternative_union_syntax : str | int
class_attr : list[Dummy]
optional : Optional[Dummy]
optional_union : Optional[int | str]
param : str
union : Union[int, str]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ def __init__(self, param: str) -> None:
self.union: Union[int, str] = ""
self.alternative_union_syntax: str | int = 0
self.optional: Optional[Dummy] = None
self.alternative_optional: int | None = None
self.alternative_optional_swapped: None | int = None
self.optional_union: int | str = None