Skip to content
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

Pyreverse autocolor #4521

Closed
wants to merge 15 commits into from
Closed

Conversation

DudeNr33
Copy link
Collaborator

Steps

  • Add a ChangeLog entry describing what your PR does.
  • If it's a new feature or an important bug fix, add a What's New entry in
    doc/whatsnew/<current release.rst>.
  • Write a good description on what the PR does.

Description

Running pyreverse on large projects can lead to diagrams with low clarity. Classes or modules might be scattered across the diagram.
By coloring the diagrams, modules or classes of the same package can be easily spotted visually. For an example, see issue #4488.

This new feature is optional and can be activated by passing the --colorized option.
The granularity can be influenced by passing --max-color-depth.
Classes/modules from the standard library are always colored in grey.
However, this seems to not work reliably, at least it did not work inside the tox test environments (I therefore had to delete the unittest for it). It worked great in my venv that I use for development.

This feature only works with dot output. I could not find a suitable program that correctly displays the vcg files.
As there were no unittests for the VCG output as well (are VCG graphs even still a thing? Google finds relatively few discussions for it...), I chose to not implement it for this output format.
I added a regression test which ensures that the VCG files produced with the current Pylint release and after this PR are the same.

Type of Changes

Type
✨ New feature
🔨 Refactoring

Related Issue

Closes #4488

@coveralls
Copy link

Coverage Status

Coverage increased (+0.4%) to 92.227% when pulling 2dcfdb0 on DudeNr33:pyreverse-autocolor into 4f8ab69 on PyCQA:master.

@DudeNr33 DudeNr33 marked this pull request as ready for review May 29, 2021 10:06
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

Hey sorry for the delay in order to review. This looks nice and well tested, I think the pytest fixture were probably not used before (I see import from unittest_pyreverse_writer that would not happen if there were proper fixtures already). You can do a small refactor to move to pytest first if you have time for that or simply introduce a default_config fixture instead of the new _DEFAULT import.

@@ -123,7 +153,41 @@ def get_title(self, obj):
"""get project title"""
return obj.title

def get_values(self, obj):
def get_style(self):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def get_style(self):
def get_style(self) -> str:

Could you add typing on new functions please ? :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will do!

def get_values(self, obj):
def get_package_properties(self, obj):
"""get label and shape for packages."""
return dict(
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be better to use NamedTuple (or a child class) here, so we can have proper typing and not use dict everywhere.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree that a NamedTuple would be the cleaner solution. However, the current (as in: this PR) implementation of the DiagramWriter class and implementations of DotBackend and VCGPrinter do not allow for this. DotBackend and VCGPrinter rely on the data returned by the get_package_properties and get_class_properties methods, and the data structures they require are not the same.

That being said: I have already started working on the PlantUML backend, and while doing so I extracted a common interface for the Printer classes. Using a NamedTuple should be possible then.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with your other comment, it your implementation of plantuml make you upgrade the code with NamedTuple and proper typing it will be nice to rebase on it once it's merged.

@@ -21,7 +21,7 @@

import astroid
import pytest
from unittest_pyreverse_writer import Config, get_project
from unittest_pyreverse_writer import _DEFAULTS, Config, get_project
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be defined in conftest.py and used as a pytest fixtures?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that would be a good idea to keep the dependencies of the test modules as minimal as possible. I will do that.

@DudeNr33
Copy link
Collaborator Author

No problem, I fully understand that pyreverse is not the most important part of pylint. ;)

As I noted above, I already started working on the PlantUML implementation, and introducing NamedTuple in favor of dicts should be way easier there. My branch for PlantUML also already includes the changes of this PR.
My suggestion would be:
We close this PR, and I will implement your review feedback in the PlantUML branch.
I can then submit a new PR with both the coloring and the PlantUML support.

@DudeNr33
Copy link
Collaborator Author

@Pierre-Sassoulas quick question - I extracted the tests into a separate subdirectory and added a new conftest.py there.
Now mypy reports an error:

tests/conftest.py: error: Duplicate module named 'conftest' (also at 'tests/pyreverse/conftest.py')

As conftest.py modules may very well exist at different levels, I would go ahead and apply the suggested fix with the --exclude option.
But before I change anything of pylints static analysis rules, I'd like to double check if that's OK.

@Pierre-Sassoulas
Copy link
Member

It's a known problem with mypy, I don't know if you can exclude the file, if you can that's great, and I did not think of that because last time I put all my fixtures tests/conftests.py, to make mypy happy 😄

@DudeNr33
Copy link
Collaborator Author

Yes, excluding works fine via the exclude option at the end of the mypy block in the pre-commit-config.yaml:

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.812
    hooks:
      - id: mypy
        name: mypy
        entry: mypy
        language: python
        types: [python]
        args: ["--ignore-missing-imports", "--scripts-are-modules"]
        require_serial: true
        additional_dependencies: []
        exclude: tests/functional/|tests/input|tests/extensions/data|tests/regrtest_data/|conftest.py|doc/|bin/

@Pierre-Sassoulas
Copy link
Member

Ha good to know, thanks ! Maybe we can ignore only some of them and keep analyzing test/conftests.py ? (Ie: Change |conftest.py| by |tests/pyreverse/conftest.py| ?)

@DudeNr33
Copy link
Collaborator Author

I see, being able to at least type check one conftest.py would still be better than ignoring all of them.
I would not like to add every new conftest.py by hand, but I guess using tests(/.*)+/conftest.py should work.
This would still check the tests/conftest.py, but ignore alle conftest.py which are in subdirectories of test.

@Pierre-Sassoulas
Copy link
Member

Hey, just a heads up, we just merged #4551 so you'll probably want to rebase :)

@Pierre-Sassoulas Pierre-Sassoulas added Enhancement ✨ Improvement to a component pyreverse Related to pyreverse component labels Jun 26, 2021
@DudeNr33
Copy link
Collaborator Author

Thanks Pierre, I just submitted the new PR which also includes this feature as well as the requested changes in the review.
This PR can be closed.

@DudeNr33 DudeNr33 closed this Jun 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component pyreverse Related to pyreverse component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: automatically color modules in package diagram
3 participants