Skip to content

Commit 7e96cb2

Browse files
author
marym
committed
Raise error when __copy__ method is used on base widget class;
Base widget class does not have a copy implementation and raises a NotImplementedError. Test case added in test_widget.py Fixes #2352
1 parent 449f895 commit 7e96cb2

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

python/ipywidgets/ipywidgets/widgets/tests/test_widget.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .. import widget
1414
from ..widget import Widget
1515
from ..widget_button import Button
16+
import copy
1617

1718

1819
def test_no_widget_view():
@@ -80,3 +81,11 @@ def test_compatibility():
8081
caller_path = inspect.stack(context=0)[1].filename
8182
assert all(x.filename == caller_path for x in record)
8283
assert len(record) == 6
84+
85+
86+
def test_widget_copy():
87+
button = Button()
88+
with pytest.raises(NotImplementedError):
89+
copy.copy(button)
90+
with pytest.raises(NotImplementedError):
91+
copy.deepcopy(button)

python/ipywidgets/ipywidgets/widgets/widget.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,12 @@ def __init__(self, **kwargs):
502502

503503
Widget._call_widget_constructed(self)
504504
self.open()
505+
506+
def __copy__(self):
507+
raise NotImplementedError("Widgets cannot be copied; custom implementation required")
508+
509+
def __deepcopy__(self, memo):
510+
raise NotImplementedError("Widgets cannot be copied; custom implementation required")
505511

506512
def __del__(self):
507513
"""Object disposal"""

0 commit comments

Comments
 (0)