Skip to content

Commit 043c883

Browse files
committed
Add type annotations to pyreverse
Closes #1548
1 parent 3ca2b25 commit 043c883

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Release date: TBA
99
..
1010
Put new features and bugfixes here and also in 'doc/whatsnew/2.9.rst'
1111

12+
* Add type annotations to pyreverse
13+
14+
Closes #1548
15+
1216
* Added ``deprecated-decorator``: Emitted when deprecated decorator is used.
1317

1418
Closes #4429

pylint/pyreverse/diagrams.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def class_names(self, nodes):
122122
if isinstance(node, astroid.Instance):
123123
node = node._proxied
124124
if (
125-
isinstance(node, astroid.ClassDef)
125+
isinstance(node, (astroid.ClassDef, astroid.Name))
126126
and hasattr(node, "name")
127127
and not self.has_node(node)
128128
):

pylint/pyreverse/inspector.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ def visit_assignname(self, node):
219219

220220
current = frame.locals_type[node.name]
221221
values = set(node.infer())
222-
frame.locals_type[node.name] = list(set(current) | values)
222+
ann = getattr(node.parent, "annotation", None)
223+
ann = {ann} if ann else set()
224+
frame.locals_type[node.name] = list(ann) or list(set(current) | values)
225+
223226
except astroid.InferenceError:
224227
pass
225228

@@ -229,10 +232,18 @@ def handle_assignattr_type(node, parent):
229232
230233
handle instance_attrs_type
231234
"""
235+
try:
236+
idx = list(node.parent.parent.locals).index(node.attrname)
237+
ann = node.parent.parent.args.annotations[idx]
238+
except ValueError:
239+
ann = getattr(node.parent, "annotation", None)
232240
try:
233241
values = set(node.infer())
234242
current = set(parent.instance_attrs_type[node.attrname])
235-
parent.instance_attrs_type[node.attrname] = list(current | values)
243+
ann = {ann} if ann else set()
244+
parent.instance_attrs_type[node.attrname] = list(
245+
(current | ann) or (current | values)
246+
)
236247
except astroid.InferenceError:
237248
pass
238249

pylint/pyreverse/writer.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,20 @@ def get_values(self, obj):
134134
if not self.config.only_classnames:
135135
label = r"{}|{}\l|".format(label, r"\l".join(obj.attrs))
136136
for func in obj.methods:
137+
return_type = f": {func.returns.name}" if func.returns else ""
138+
137139
if func.args.args:
138140
args = [arg.name for arg in func.args.args if arg.name != "self"]
139141
else:
140142
args = []
141-
label = r"{}{}({})\l".format(label, func.name, ", ".join(args))
143+
144+
annotations = [a.name if a else None for a in func.args.annotations[1:]]
145+
args = ", ".join(
146+
f"{arg}: {ann}" if ann else f"{arg}"
147+
for arg, ann in zip(args, annotations)
148+
)
149+
150+
label = fr"{label}{func.name}({args}){return_type}\l"
142151
label = "{%s}" % label
143152
if is_exception(obj.node):
144153
return dict(fontcolor="red", label=label, shape="record")

0 commit comments

Comments
 (0)