Skip to content

BUG: overriden methods of subclasses of Styler are not called during rendering #52728 #52919

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

Merged
merged 5 commits into from
Apr 27, 2023
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ ExtensionArray

Styler
^^^^^^
-
- Bug in :meth:`Styler._copy` calling overridden methods in subclasses of :class:`Styler` (:issue:`52728`)
-

Other
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1578,8 +1578,8 @@ def _copy(self, deepcopy: bool = False) -> Styler:
- applied styles (_todo)

"""
# GH 40675
styler = Styler(
# GH 40675, 52728
styler = type(self)(
self.data, # populates attributes 'data', 'columns', 'index' as shallow
)
shallow = [ # simple string or boolean immutables
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,20 @@ def test_copy(comprehensive, render, deepcopy, mi_styler, mi_styler_comp):
assert id(getattr(s2, attr)) != id(getattr(styler, attr))


@pytest.mark.parametrize("deepcopy", [True, False])
def test_inherited_copy(mi_styler, deepcopy):
# Ensure that the inherited class is preserved when a Styler object is copied.
# GH 52728
class CustomStyler(Styler):
pass

custom_styler = CustomStyler(mi_styler.data)
custom_styler_copy = (
copy.deepcopy(custom_styler) if deepcopy else copy.copy(custom_styler)
)
assert isinstance(custom_styler_copy, CustomStyler)


def test_clear(mi_styler_comp):
# NOTE: if this test fails for new features then 'mi_styler_comp' should be updated
# to ensure proper testing of the 'copy', 'clear', 'export' methods with new feature
Expand Down