Skip to content

Commit 8ff2fdd

Browse files
committed
Implement a cleaner solution for extracting the return types of properties
1 parent a407835 commit 8ff2fdd

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

pylint/pyreverse/diagrams.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from astroid import nodes, util
1414

1515
from pylint.checkers.utils import decorated_with_property, in_type_checking_block
16-
from pylint.pyreverse.utils import FilterMixIn
16+
from pylint.pyreverse.utils import FilterMixIn, get_annotation_label
1717

1818

1919
class Figure:
@@ -121,12 +121,16 @@ def get_relationship(
121121
def get_attrs(self, node: nodes.ClassDef) -> list[str]:
122122
"""Return visible attributes, possibly with class name."""
123123
attrs = []
124+
125+
# Collect functions decorated with @property
124126
properties = {
125127
local_name: local_node
126128
for local_name, local_node in node.items()
127129
if isinstance(local_node, nodes.FunctionDef)
128130
and decorated_with_property(local_node)
129131
}
132+
133+
# Add instance attributes to properties
130134
for attr_name, attr_type in list(node.locals_type.items()) + list(
131135
node.instance_attrs_type.items()
132136
):
@@ -136,9 +140,21 @@ def get_attrs(self, node: nodes.ClassDef) -> list[str]:
136140
for node_name, associated_nodes in properties.items():
137141
if not self.show_attr(node_name):
138142
continue
139-
names = self.class_names(associated_nodes)
140-
if names:
141-
node_name = f"{node_name} : {', '.join(names)}"
143+
144+
# Handle property methods differently to correctly extract return type
145+
if isinstance(
146+
associated_nodes, nodes.FunctionDef
147+
) and decorated_with_property(associated_nodes):
148+
if associated_nodes.returns:
149+
type_annotation = get_annotation_label(associated_nodes.returns)
150+
node_name = f"{node_name} : {type_annotation}"
151+
152+
# Handle regular attributes
153+
else:
154+
names = self.class_names(associated_nodes)
155+
if names:
156+
node_name = f"{node_name} : {', '.join(names)}"
157+
142158
attrs.append(node_name)
143159
return sorted(attrs)
144160

0 commit comments

Comments
 (0)