Skip to content

Commit 0980a07

Browse files
committed
add include_overrides_only kwarg to display_code_comparison
1 parent ecde164 commit 0980a07

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

HISTORY.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ History
44

55
v0.2.0dev
66
---------
7+
* New keyword argument for ``display_code_comparison``, ``include_overrides_only`` which when True (the default), only includes the classes that override the function of interest.
78
* Improved typing (`PR 42 <https://github.com/data-exp-lab/inheritance_explorer/pull/42>`_)
89

910
v0.2.0

docs/examples/ex_004_code_comparison.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ The following screenshot shows the code comparison widget in a Jupyter notebook:
2323
.. image:: /resources/inherit_code_widget.gif
2424
:width: 800
2525

26+
By default, the code comparison widget will only display child classes that override
27+
the function being compared. To include all classes, set ``include_overrides_only==False``
28+
when calling ``display_code_comparison``.
29+

inheritance_explorer/_widget_support.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,32 @@ def find_closest_source(cgt: ClassGraphTree, node_id: int):
2424
# it does not, get base class source
2525

2626

27-
def display_code_compare(cgt: ClassGraphTree):
28-
_display_code_compare(cgt)
27+
def display_code_compare(cgt: ClassGraphTree, include_overrides_only: bool = True):
28+
_display_code_compare(cgt, include_overrides_only=include_overrides_only)
29+
30+
31+
def _get_class_names(
32+
cgt: ClassGraphTree, include_overrides_only: bool = True
33+
) -> list[str]:
34+
if include_overrides_only is False:
35+
names_classes = [nm for nm in cgt._node_map_r.keys()]
36+
else:
37+
names_classes = []
38+
for node_id in cgt._override_src.keys():
39+
names_classes.append(cgt._node_map[node_id])
40+
41+
names_classes.sort()
42+
return names_classes
2943

3044

3145
def _display_code_compare(
3246
cgt: ClassGraphTree,
3347
class_1_name: Optional[str] = None,
3448
class_2_name: Optional[str] = None,
49+
include_overrides_only: bool = True,
3550
):
36-
names_classes = [i.child_name for i in cgt._node_list]
37-
names_classes.sort()
51+
52+
names_classes = _get_class_names(cgt, include_overrides_only=include_overrides_only)
3853

3954
class_dropdown_1 = ipywidgets.Dropdown(options=names_classes.copy())
4055
class_dropdown_2 = ipywidgets.Dropdown(options=names_classes.copy())

inheritance_explorer/inheritance_explorer.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,16 +532,22 @@ def get_multiple_source_code(
532532
src_dict[src_key] = self.get_source_code(src_key)
533533
return src_dict
534534

535-
def display_code_comparison(self):
535+
def display_code_comparison(self, include_overrides_only: bool = True):
536536
"""
537537
show the code comparison widget
538+
539+
Parameters
540+
----------
541+
include_overrides_only: bool
542+
if True (default), only displays the classes that override the function
543+
being compared.
538544
"""
539545

540546
# add a check that we are running from a notebook?
541547
if self.funcname is not None:
542548
from inheritance_explorer._widget_support import display_code_compare
543549

544-
display_code_compare(self)
550+
display_code_compare(self, include_overrides_only=include_overrides_only)
545551

546552

547553
def _validate_color(clr, default_rgb_tuple: tuple[float, float, float]) -> str:

inheritance_explorer/tests/test_widgets.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from inheritance_explorer._testing import ClassForTesting
4-
from inheritance_explorer._widget_support import _display_code_compare
4+
from inheritance_explorer._widget_support import _display_code_compare, _get_class_names
55
from inheritance_explorer.inheritance_explorer import ClassGraphTree
66

77

@@ -16,4 +16,16 @@ def test_code_comparison_widget_from_cgt(cgt):
1616

1717
def test_secret_code_comparison_widget(cgt):
1818
_display_code_compare(cgt, class_1_name="ClassForTesting4")
19-
_display_code_compare(cgt, class_2_name="ClassForTesting3")
19+
_display_code_compare(
20+
cgt, class_2_name="ClassForTesting3", include_overrides_only=False
21+
)
22+
23+
24+
def test_get_class_names(cgt):
25+
26+
cnames_all = _get_class_names(cgt, include_overrides_only=False)
27+
assert len(cnames_all) == len(cgt._node_list)
28+
29+
cnames_override = _get_class_names(cgt, include_overrides_only=True)
30+
assert len(cnames_override) < len(cgt._node_list)
31+
assert len(cnames_override) == len(cgt._override_src)

0 commit comments

Comments
 (0)