Skip to content

Conversation

@avylove
Copy link
Contributor

@avylove avylove commented Feb 5, 2023

Type of Changes

Type
πŸ› Bug fix
βœ“ ✨ New feature
πŸ”¨ Refactoring
πŸ“œ Docs

Description

Adds an option (--show-stdlib, -L) to pyreverse. This is similar to the behavior of --show-builtin in that if this option is not included, standard library modules are not included by default.

To determine if a module is part of the standard library, sys.stdlib_module_names was used. Since this is only available in 3.10 and above, the generating code was backported to 3.7, 3.8, and 3.9. Then a list was generated. These were condensed for commonality to reduce code and create a shim.

Closes #8181

@avylove avylove requested a review from DudeNr33 as a code owner February 5, 2023 18:43
@codecov
Copy link

codecov bot commented Feb 5, 2023

Codecov Report

Merging #8190 (16c2852) into main (2b3d113) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #8190   +/-   ##
=======================================
  Coverage   95.71%   95.71%           
=======================================
  Files         175      175           
  Lines       18451    18455    +4     
=======================================
+ Hits        17660    17665    +5     
+ Misses        791      790    -1     
Impacted Files Coverage Ξ”
pylint/pyreverse/main.py 93.87% <ΓΈ> (ΓΈ)
pylint/pyreverse/diadefslib.py 100.00% <100.00%> (+0.81%) ⬆️
pylint/testutils/pyreverse.py 98.18% <100.00%> (+0.03%) ⬆️

@Pierre-Sassoulas Pierre-Sassoulas added the pyreverse Related to pyreverse component label Feb 5, 2023
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.

Looks pretty nice !

@Pierre-Sassoulas Pierre-Sassoulas added the Enhancement ✨ Improvement to a component label Feb 5, 2023
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.17.0 milestone Feb 5, 2023
@github-actions

This comment has been minimized.

@DudeNr33
Copy link
Collaborator

DudeNr33 commented Feb 6, 2023

Thank you for the PR!

I will do a complete review later, but already a quick remark:
The DiagramWriter class also uses logic to determine if a class or module is from the standard library (in the method get_shape_color).
It would be good to keep this consistent and use the same logic for both.

@avylove
Copy link
Contributor Author

avylove commented Feb 6, 2023

I will do a complete review later, but already a quick remark: The DiagramWriter class also uses logic to determine if a class or module is from the standard library (in the method get_shape_color). It would be good to keep this consistent and use the same logic for both.

I missed astroid.modutils.is_standard_module(), and probably would have just used it if I had seen it. But I feel like sys.stdlib_module_names is more accurate since it relies on a compiled list and is maintained upstream rather than the state of the current environment. Do you think it would be best to use astroid.modutils.is_standard_module() for both and then do a PR against Astroid to modify it to use sys.stdlib_module_names?

@DudeNr33
Copy link
Collaborator

DudeNr33 commented Feb 6, 2023

Do you think it would be best to use astroid.modutils.is_standard_module() for both and then do a PR against Astroid to modify it to use sys.stdlib_module_names?

That is the cleanest approach in my opinion.

Copy link
Collaborator

@DudeNr33 DudeNr33 left a comment

Choose a reason for hiding this comment

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

The logic looks fine to me. πŸ‘

Aside from the logic to determine which modules belong to the stdlib only some tests are missing.
You can either add a functional test (i.e. a Python module and the resulting class diagram) in tests/pyreverse/functional/class_diagrams with a suitable rcfile (example), or add a plain unittest.

For the latter you can take a look at test_inspector.test_concat_interfaces for an example how you can extract a astroid node from a code sample in a unit test.

@avylove
Copy link
Contributor Author

avylove commented Feb 10, 2023

Submitted pylint-dev/astroid#2015. If that gets merged, we can replace astroid.modutils.is_standard_module() everywhere.

Also added a couple tests. I went with a plain unit test because I thought it would be easier to confirm the behavior change between the options being set or not. Maybe there's an better way to implement them, not sure.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@DudeNr33 DudeNr33 left a comment

Choose a reason for hiding this comment

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

Thank you for putting in the effort in astroid and adding tests.
This made me realize we should flip the default value to preserve the existing behavior as default.

@@ -0,0 +1,5 @@
Add new option (``--show-stdlib``, ``-L``) to pyreverse.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Add new option (``--show-stdlib``, ``-L``) to pyreverse.
Add new option (``--show-stdlib``, ``-L``) to ``pyreverse``.

Just a nitpick and I know that the existing release notes are also not consistent with this, but the majority of the docs put pylint/pyreverse in backticks.

@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.17.0, 3.0.0 Feb 17, 2023
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.

We're in the alpha stage of 3.0 now, we could merge :)

@avylove
Copy link
Contributor Author

avylove commented Mar 8, 2023

We're in the alpha stage of 3.0 now, we could merge :)

3.0 will use Astroid >=2.15, right? So I can update this PR to use is_stdlib_module() instead of is_standard_module().

@Pierre-Sassoulas
Copy link
Member

Pierre-Sassoulas commented Mar 8, 2023

Definitely, I did not think of this but it would break main if you don't πŸ˜„ (You can merge upstream/main, the test will start to fail)

@avylove avylove force-pushed the pyreverse_stdlib branch from 6e657b3 to deb150e Compare March 8, 2023 16:44
@avylove
Copy link
Contributor Author

avylove commented Mar 8, 2023

Rebased to main, fixed quotes in whatnew fragment, updated to use is_stdlib_module()

@github-actions

This comment has been minimized.

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.

LGTM, but I'll let @DudeNr33 merge.

@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 3.0.0, 3.0.0b1 Mar 9, 2023
Copy link
Collaborator

@DudeNr33 DudeNr33 left a comment

Choose a reason for hiding this comment

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

Only one remark regarding the category of the news fragment now that we decided to change the default behavior, and then we are good to go!

@@ -0,0 +1,5 @@
Add new option (``--show-stdlib``, ``-L``) to ``pyreverse``.
This is similar to the behavior of ``--show-builtin`` in that standard library
modules are now not included by default, and this option will include them.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should probably put this under "breaking changes" (i. e. rename the file to 8181.breaking), as it is a change in the default behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

Copy link
Collaborator

@DudeNr33 DudeNr33 left a comment

Choose a reason for hiding this comment

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

Thank you for this addition and the patience waiting for 3.0!
This can really help to make diagrams more readable. πŸ‘

@github-actions
Copy link
Contributor

πŸ€– According to the primer, this change has no effect on the checked open source code. πŸ€–πŸŽ‰

This comment was generated for commit 16c2852

@DudeNr33 DudeNr33 modified the milestones: 3.0.0b1, 3.0.0a6 Mar 10, 2023
@DudeNr33 DudeNr33 merged commit 958ff85 into pylint-dev:main Mar 10, 2023
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.

pyreverse: Add option to exclude standard library

3 participants