Skip to content
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
5 changes: 5 additions & 0 deletions doc/whatsnew/fragments/8181.breaking
Original file line number Diff line number Diff line change
@@ -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.

Closes #8181
13 changes: 9 additions & 4 deletions pylint/pyreverse/diadefslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import astroid
from astroid import nodes
from astroid.modutils import is_stdlib_module

from pylint.pyreverse.diagrams import ClassDiagram, PackageDiagram
from pylint.pyreverse.inspector import Linker, Project
Expand Down Expand Up @@ -67,10 +68,14 @@ def _get_levels(self) -> tuple[int, int]:
return self.anc_level, self.association_level

def show_node(self, node: nodes.ClassDef) -> bool:
"""True if builtins and not show_builtins."""
if self.config.show_builtin:
return True
return node.root().name != "builtins" # type: ignore[no-any-return]
"""Determine if node should be shown based on config."""
if node.root().name == "builtins":
return self.config.show_builtin # type: ignore[no-any-return]

if is_stdlib_module(node.root().name):
return self.config.show_stdlib # type: ignore[no-any-return]

return True

def add_class(self, node: nodes.ClassDef) -> None:
"""Visit one class and add it to diagram."""
Expand Down
9 changes: 9 additions & 0 deletions pylint/pyreverse/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@
"help": "include builtin objects in representation of classes",
},
),
(
"show-stdlib",
{
"short": "L",
"action": "store_true",
"default": False,
"help": "include standard library objects in representation of classes",
},
),
(
"module-names",
{
Expand Down
2 changes: 2 additions & 0 deletions pylint/testutils/pyreverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(
show_associated: int | None = None,
all_associated: bool | None = None,
show_builtin: bool = False,
show_stdlib: bool = False,
module_names: bool | None = None,
only_classnames: bool = False,
output_format: str = "dot",
Expand All @@ -59,6 +60,7 @@ def __init__(
self.show_associated = show_associated
self.all_associated = all_associated
self.show_builtin = show_builtin
self.show_stdlib = show_stdlib
self.module_names = module_names
self.only_classnames = only_classnames
self.output_format = output_format
Expand Down
46 changes: 45 additions & 1 deletion tests/pyreverse/test_diadefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pathlib import Path

import pytest
from astroid import nodes
from astroid import extract_node, nodes

from pylint.pyreverse.diadefslib import (
ClassDiadefGenerator,
Expand Down Expand Up @@ -97,6 +97,50 @@ def test_default_values() -> None:
# TODO : should test difference between default values for package or class diagrams


class TestShowOptions:
def test_show_stdlib(self) -> None:
example = extract_node(
'''
import collections

class CustomDict(collections.OrderedDict):
"""docstring"""
'''
)

config = PyreverseConfig()
dd_gen = DiaDefGenerator(Linker(PROJECT), DiadefsHandler(config))

# Default behavior
assert not list(dd_gen.get_ancestors(example, 1))

# Show standard library enabled
config.show_stdlib = True
ancestors = list(dd_gen.get_ancestors(example, 1))
assert len(ancestors) == 1
assert ancestors[0].name == "OrderedDict"

def test_show_builtin(self) -> None:
example = extract_node(
'''
class CustomError(Exception):
"""docstring"""
'''
)

config = PyreverseConfig()
dd_gen = DiaDefGenerator(Linker(PROJECT), DiadefsHandler(config))

# Default behavior
assert not list(dd_gen.get_ancestors(example, 1))

# Show builtin enabled
config.show_builtin = True
ancestors = list(dd_gen.get_ancestors(example, 1))
assert len(ancestors) == 1
assert ancestors[0].name == "Exception"


class TestDefaultDiadefGenerator:
_should_rels = [
("aggregation", "DoNothing2", "Specialization"),
Expand Down
1 change: 1 addition & 0 deletions tests/pyreverse/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def test_graphviz_unsupported_image_format(capsys: CaptureFixture) -> None:
("show_associated", None),
("all_associated", None),
("show_builtin", 0),
("show_stdlib", 0),
("module_names", None),
("output_format", "dot"),
("colorized", 0),
Expand Down
1 change: 1 addition & 0 deletions tests/pyreverse/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"all_associated": None,
"mode": "PUB_ONLY",
"show_builtin": False,
"show_stdlib": False,
"only_classnames": False,
"output_directory": "",
}
Expand Down