Skip to content

Commit caed9b0

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

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-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 dot files
13+
14+
Closes #1548
15+
1216
* Added ``deprecated-decorator``: Emitted when deprecated decorator is used.
1317

1418
Closes #4429

doc/whatsnew/2.9.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ New checkers
4040
Other Changes
4141
=============
4242

43+
* Add type annotations to pyreverse dot files
44+
4345
* Pylint's tags are now the standard form ``vX.Y.Z`` and not ``pylint-X.Y.Z`` anymore.
4446

4547
* Fix false-positive ``too-many-ancestors`` when inheriting from builtin classes,

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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,10 @@ def visit_assignname(self, node):
218218
self.visit_module(frame)
219219

220220
current = frame.locals_type[node.name]
221-
values = set(node.infer())
221+
ann = getattr(node.parent, "annotation", None)
222+
values = {ann} if ann else set(node.infer())
222223
frame.locals_type[node.name] = list(set(current) | values)
224+
223225
except astroid.InferenceError:
224226
pass
225227

@@ -229,8 +231,18 @@ def handle_assignattr_type(node, parent):
229231
230232
handle instance_attrs_type
231233
"""
234+
235+
ann = None
236+
if isinstance(node.parent.value, astroid.Name):
237+
try:
238+
idx = list(node.parent.parent.locals).index(node.parent.value.name)
239+
ann = node.parent.parent.args.annotations[idx]
240+
except IndexError:
241+
pass
242+
elif hasattr(node.parent, "annotation"):
243+
ann = node.parent.annotation
232244
try:
233-
values = set(node.infer())
245+
values = {ann} if ann else set(node.infer())
234246
current = set(parent.instance_attrs_type[node.attrname])
235247
parent.instance_attrs_type[node.attrname] = list(current | values)
236248
except astroid.InferenceError:

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)