Skip to content

Commit

Permalink
Merge pull request #3780 from mmc1718/duplicating-widget
Browse files Browse the repository at this point in the history
Raise error when __copy__ method is used on base widget class
  • Loading branch information
ibdafna committed Aug 15, 2023
2 parents e1718c2 + acd2576 commit 689bc46
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python/ipywidgets/ipywidgets/widgets/tests/test_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .. import widget
from ..widget import Widget
from ..widget_button import Button
import copy


def test_no_widget_view():
Expand Down Expand Up @@ -80,3 +81,11 @@ def test_compatibility():
caller_path = inspect.stack(context=0)[1].filename
assert all(x.filename == caller_path for x in record)
assert len(record) == 6


def test_widget_copy():
button = Button()
with pytest.raises(NotImplementedError):
copy.copy(button)
with pytest.raises(NotImplementedError):
copy.deepcopy(button)
6 changes: 6 additions & 0 deletions python/ipywidgets/ipywidgets/widgets/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,12 @@ def __init__(self, **kwargs):

Widget._call_widget_constructed(self)
self.open()

def __copy__(self):
raise NotImplementedError("Widgets cannot be copied; custom implementation required")

def __deepcopy__(self, memo):
raise NotImplementedError("Widgets cannot be copied; custom implementation required")

def __del__(self):
"""Object disposal"""
Expand Down

0 comments on commit 689bc46

Please sign in to comment.