-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Create common Printer
base class for pyreverse
and improve typing.
#4731
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
Create common Printer
base class for pyreverse
and improve typing.
#4731
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a small comment, and I did not look in detail but the new typing is 🔥 , thanks! I'll review when this is no longer a draft :)
…athlib.Path().suffix``
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, thank you for this work on this and sorry for the delay before reviewing. Could we reorganize and move the code around first then do the modification on it ? I'd be more confident if there was a proper diff for the change in the printer.py file, right now everything is considered new by git and it's hard to tell what changed exactly :)
pylint/pyreverse/printer.py
Outdated
body: Optional[str] = None | ||
|
||
|
||
class Printer: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class Printer: | |
class Printer(abc.ABC): |
It's more explicit than raising NotImplementError
s :)
pylint/pyreverse/printer.py
Outdated
raise NotImplementedError | ||
|
||
|
||
class VCGPrinter(Printer): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can create a new file for VCGPrinter and another one for DotPrinter :) (It would also help with the diff and possible future conflict if vcgutils.py is recognized as the old file for it).
def save(self) -> None: | ||
"""write to disk""" | ||
self.printer.generate(self.file_name) | ||
|
||
|
||
class DotWriter(DiagramWriter): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could start with a first merge request that create the following architecture and move relevant part of the code without modifying anything:
pyreverse:
writter:
diagram_writter.py
dot_writter.py
vcg_writter.py
printer:
dot_printer.py
vcg_printer.py
What do you think ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for reorganizing the code the diff is manageable now :)
Thanks for the feedback. I will implement it when #4745 is merged and I did a rebase. |
…nd move ``VCGPrinter`` back into original ``vcgutils.py`` for easier diffing
…ImplementedError``
super().__init__(title=title, node=node) | ||
self.attrs = None | ||
self.methods = None | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding the type hints revealed that it was necessary to distinguish between a PackageEntity
and a ClassEntity
, because the ClassEntity
has additional attributes that were dynamically added in the previous code, which confused mypy
.
color="black", | ||
) | ||
|
||
def get_class_properties(self, obj: ClassEntity) -> NodeProperties: | ||
"""get label and shape for classes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_values
is now get_class_properties
.
get_package_properties
is currently trivial and the color is hardcoded, but this will change when the new feature for automatic coloring are merged.
dict(arrowstyle="solid", backarrowstyle="none", textcolor="green"), | ||
] | ||
DiagramWriter.__init__(self, config, styles) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The __init__
is now obsolete. It formerly defined the styles to apply to the different types of edges for displaying relationships.
These are now defined in the ARROWS
dictionary in the module of the corresponding printer class.
I implemented most of the feedback. Regarding the last suggestion (doing a first MR to implement the structure
): If the current cleanup is not enough to properly review the changes, let me know and I can do that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your work on this, very impressive ! Ad thank you for making the diff manageable. I left some minor comment. We can still do the architecture change later if you feel like it (probably after merging the other MR as the rebase will be hard to do if we do it before).
) | ||
|
||
def _inc_indent(self): | ||
"""increment indentation""" | ||
self._indent = " %s" % self._indent | ||
self._indent += " " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
pylint/pyreverse/writer.py
Outdated
@@ -140,12 +158,14 @@ def get_values(self, obj): | |||
) | |||
|
|||
if func.args.args: | |||
args = [arg for arg in func.args.args if arg.name != "self"] | |||
argument_list = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
argument_list = [ | |
arguments: List[str] = [ |
Personal preference but when naming an iterable I like a plural word so I can do for argument in arguments
later on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, changed it.
EdgeType.ASSOCIATION: dict( | ||
fontcolor="green", arrowtail="none", arrowhead="diamond", style="solid" | ||
), | ||
EdgeType.USES: dict(arrowtail="none", arrowhead="open"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could create enums for fontcolor, arrowtail, style and arrowhead ?
def save(self) -> None: | ||
"""write to disk""" | ||
self.printer.generate(self.file_name) | ||
|
||
|
||
class DotWriter(DiagramWriter): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for reorganizing the code the diff is manageable now :)
Thank you very much for your review! I also need to rename the |
No problem, just request a new review when the code is ready :) |
I thought about implementing enums for arrowstyle, arrowhead and so on a little longer. # in vcgutils.py
attributes = ARROWS[type_]
if label:
attributes["label"] = label
self._write_attributes(
EDGE_ATTRS,
**attributes,
)
# in dot_printer.py
arrowstyle = ARROWS[type_]
attrs = [f'{prop}="{value}"' for prop, value in arrowstyle.items()] I think we would just clutter the modules with enum definitions, without gaining much in terms of readability or reusability.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for making a first step easier to review :) !
Steps
Description
This is the first MR which shall split MR #4615 into smaller and more easily reviewable parts.
It involves the necessary refactoring which is needed to prepare for the new features (PlantUML support and colorization).
It also adds type annotations.
Summary of changes:
vcgutils.py
toprinter.py
and introduce aPrinter
base class which defines the common interface for the different backends.VCGPrinter
to adhere to the new printer base classDotPrinter
class which essentially implements the features ofpylint.graph.DotBackend
. The latter will be removed in a future MR, as it is currently also used by theImportsChecker
and I wanted to keep this MR smaller and focused (it is already not very small...).Printer
class and its children. This also uncovered some problems wheremypy
correctly complained, so I had to add specialisedPackageEntity
andClassEntity
child classes indiagrams.py
.Printer
backends now outputs some more information. The final image produced by Graphviz is still the same.Type of Changes