Skip to content

Fix pyreverse type hinting for class methods #5881

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

Merged
merged 4 commits into from
Mar 9, 2022
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
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Release date: TBA
..
Put new features here and also in 'doc/whatsnew/2.13.rst'

* Fix pyreverse diagrams type hinting for classmethods and staticmethods.

* Fix matching ``--notes`` options that end in a non-word character.

Closes #5840
Expand Down
15 changes: 7 additions & 8 deletions pylint/pyreverse/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,13 @@ def emit_edge(

@staticmethod
def _get_method_arguments(method: nodes.FunctionDef) -> List[str]:
if method.args.args:
arguments: List[nodes.AssignName] = [
arg for arg in method.args.args if arg.name != "self"
]
else:
arguments = []

annotations = dict(zip(arguments, method.args.annotations[1:]))
if method.args.args is None:
return []

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice cleanup and lowering the indentation level!

first_arg = 0 if method.type in {"function", "staticmethod"} else 1
arguments: List[nodes.AssignName] = method.args.args[first_arg:]

annotations = dict(zip(arguments, method.args.annotations[first_arg:]))
for arg in arguments:
annotation_label = ""
ann = annotations.get(arg)
Expand Down
8 changes: 8 additions & 0 deletions tests/data/clientmodule_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ def __init__(self, value, _id, relation2: DoNothing2):
self._id = _id
self.relation = DoNothing()
self.relation2 = relation2

@classmethod
def from_value(cls, value: int):
return cls(value, 0, DoNothing2())

@staticmethod
def transform_value(value: int) -> int:
return value * 2
2 changes: 1 addition & 1 deletion tests/pyreverse/data/classes_No_Name.dot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ charset="utf-8"
"data.suppliermodule_test.DoSomething" [color="black", fontcolor="black", label="{DoSomething|my_int : Optional[int]\lmy_int_2 : Optional[int]\lmy_string : str\l|do_it(new_int: int): int\l}", shape="record", style="solid"];
"data.suppliermodule_test.Interface" [color="black", fontcolor="black", label="{Interface|\l|get_value()\lset_value(value)\l}", shape="record", style="solid"];
"data.property_pattern.PropertyPatterns" [color="black", fontcolor="black", label="{PropertyPatterns|prop1\lprop2\l|}", shape="record", style="solid"];
"data.clientmodule_test.Specialization" [color="black", fontcolor="black", label="{Specialization|TYPE : str\lrelation\lrelation2\ltop : str\l|}", shape="record", style="solid"];
"data.clientmodule_test.Specialization" [color="black", fontcolor="black", label="{Specialization|TYPE : str\lrelation\lrelation2\ltop : str\l|from_value(value: int)\ltransform_value(value: int): int\l}", shape="record", style="solid"];
"data.clientmodule_test.Specialization" -> "data.clientmodule_test.Ancestor" [arrowhead="empty", arrowtail="none"];
"data.clientmodule_test.Ancestor" -> "data.suppliermodule_test.Interface" [arrowhead="empty", arrowtail="node", style="dashed"];
"data.suppliermodule_test.DoNothing" -> "data.clientmodule_test.Ancestor" [arrowhead="diamond", arrowtail="none", fontcolor="green", label="cls_member", style="solid"];
Expand Down
2 changes: 2 additions & 0 deletions tests/pyreverse/data/classes_No_Name.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
relation
relation2
top : str
from_value(value: int)
transform_value(value: int) -> int
}
Specialization --|> Ancestor
Ancestor ..|> Interface
Expand Down
2 changes: 2 additions & 0 deletions tests/pyreverse/data/classes_No_Name.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ classDiagram
relation
relation2
top : str
from_value(value: int)
transform_value(value: int) -> int
}
Specialization --|> Ancestor
Ancestor ..|> Interface
Expand Down
2 changes: 2 additions & 0 deletions tests/pyreverse/data/classes_No_Name.puml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class "Specialization" as data.clientmodule_test.Specialization {
relation
relation2
top : str
from_value(value: int)
transform_value(value: int) -> int
}
data.clientmodule_test.Specialization --|> data.clientmodule_test.Ancestor
data.clientmodule_test.Ancestor ..|> data.suppliermodule_test.Interface
Expand Down
2 changes: 1 addition & 1 deletion tests/pyreverse/data/classes_No_Name.vcg
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ graph:{
node: {title:"data.property_pattern.PropertyPatterns" label:"\fbPropertyPatterns\fn\n\f__________________\n\f08prop1\n\f08prop2\n\f__________________"
shape:box
}
node: {title:"data.clientmodule_test.Specialization" label:"\fbSpecialization\fn\n\f________________\n\f08TYPE : str\n\f08relation\n\f08relation2\n\f08top : str\n\f________________"
node: {title:"data.clientmodule_test.Specialization" label:"\fbSpecialization\fn\n\f_________________\n\f08TYPE : str\n\f08relation\n\f08relation2\n\f08top : str\n\f_________________\n\f10from_value()\n\f10transform_value()"
shape:box
}
edge: {sourcename:"data.clientmodule_test.Specialization" targetname:"data.clientmodule_test.Ancestor" arrowstyle:solid
Expand Down
2 changes: 1 addition & 1 deletion tests/pyreverse/data/classes_colorized.dot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ charset="utf-8"
"data.suppliermodule_test.DoSomething" [color="aliceblue", fontcolor="black", label="{DoSomething|my_int : Optional[int]\lmy_int_2 : Optional[int]\lmy_string : str\l|do_it(new_int: int): int\l}", shape="record", style="filled"];
"data.suppliermodule_test.Interface" [color="aliceblue", fontcolor="black", label="{Interface|\l|get_value()\lset_value(value)\l}", shape="record", style="filled"];
"data.property_pattern.PropertyPatterns" [color="aliceblue", fontcolor="black", label="{PropertyPatterns|prop1\lprop2\l|}", shape="record", style="filled"];
"data.clientmodule_test.Specialization" [color="aliceblue", fontcolor="black", label="{Specialization|TYPE : str\lrelation\lrelation2\ltop : str\l|}", shape="record", style="filled"];
"data.clientmodule_test.Specialization" [color="aliceblue", fontcolor="black", label="{Specialization|TYPE : str\lrelation\lrelation2\ltop : str\l|from_value(value: int)\ltransform_value(value: int): int\l}", shape="record", style="filled"];
"data.clientmodule_test.Specialization" -> "data.clientmodule_test.Ancestor" [arrowhead="empty", arrowtail="none"];
"data.clientmodule_test.Ancestor" -> "data.suppliermodule_test.Interface" [arrowhead="empty", arrowtail="node", style="dashed"];
"data.suppliermodule_test.DoNothing" -> "data.clientmodule_test.Ancestor" [arrowhead="diamond", arrowtail="none", fontcolor="green", label="cls_member", style="solid"];
Expand Down
2 changes: 2 additions & 0 deletions tests/pyreverse/data/classes_colorized.puml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class "Specialization" as data.clientmodule_test.Specialization #aliceblue {
relation
relation2
top : str
from_value(value: int)
transform_value(value: int) -> int
}
data.clientmodule_test.Specialization --|> data.clientmodule_test.Ancestor
data.clientmodule_test.Ancestor ..|> data.suppliermodule_test.Interface
Expand Down
10 changes: 10 additions & 0 deletions tests/pyreverse/test_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Type

import pytest
from astroid import nodes

from pylint.pyreverse.dot_printer import DotPrinter
from pylint.pyreverse.plantuml_printer import PlantUmlPrinter
Expand Down Expand Up @@ -46,6 +47,15 @@ def test_unsupported_layout(layout: Layout, printer_class: Type[Printer]):
printer_class(title="unittest", layout=layout)


def test_method_arguments_none():
func = nodes.FunctionDef()
args = nodes.Arguments()
args.args = None
func.postinit(args, body=None)
parsed_args = Printer._get_method_arguments(func)
assert parsed_args == []


class TestPlantUmlPrinter:
printer = PlantUmlPrinter(title="unittest", layout=Layout.TOP_TO_BOTTOM)

Expand Down