diff --git a/.github/workflows/unittest.yml b/.github/workflows/unittest.yml index 6b32831..dfdc7fd 100644 --- a/.github/workflows/unittest.yml +++ b/.github/workflows/unittest.yml @@ -50,7 +50,7 @@ jobs: path: ./dist - name: Install run: | - pip install `echo dist/*.whl`[dev] "bqplot<0.13" "pandas<3" "ruff==0.8.3" "ipyvue${{ matrix.ipyvue }}" "ipyvuetify${{ matrix.ipyvue }}" + pip install `echo dist/*.whl`[dev] "bqplot<0.13" six "pandas<3" "ruff==0.8.3" "ipyvue${{ matrix.ipyvue }}" "ipyvuetify${{ matrix.ipyvue }}" - name: test env: REACTON_FAST: ${{ matrix.reacton-fast }} diff --git a/reacton/core.py b/reacton/core.py index f2ef0ac..2246753 100644 --- a/reacton/core.py +++ b/reacton/core.py @@ -517,14 +517,10 @@ def _create_widget(self, kwargs): try: try: widget = self.component.widget(**kwargs) - hold_trait_notifications = widget.hold_trait_notifications - - @contextlib.contextmanager - def hold_trait_notifications_extra(*args, **kwargs): - with rc, hold_trait_notifications(*args, **kwargs): - yield - - widget.hold_trait_notifications = hold_trait_notifications_extra + # A partial of one module-level context manager, not a @contextlib.contextmanager defined here: + # that decorator runs functools.wraps for every widget, and on free-threaded Python setting + # attributes on a function object stops all threads. + widget.hold_trait_notifications = functools.partial(_hold_trait_notifications_extra, rc, widget.hold_trait_notifications) if self._meta: widget._react_meta = dict(self._meta) @@ -3423,3 +3419,9 @@ def Fragment(children: List[Element]): # not a public api yet, used in solara for now only. # lifecycle of context objects are linked to the lifecycle of the component _component_context_manager_classes: List[Any] = [] + + +@contextlib.contextmanager +def _hold_trait_notifications_extra(rc, hold_trait_notifications, *args, **kwargs): + with rc, hold_trait_notifications(*args, **kwargs): + yield diff --git a/reacton/core_test.py b/reacton/core_test.py index 579f37a..2a98167 100644 --- a/reacton/core_test.py +++ b/reacton/core_test.py @@ -4437,3 +4437,28 @@ def Level(): with pytest.raises(ValueError): rc.close() # close() raises the error of the failed render again assert time.monotonic() - start < 2 + + +def test_create_widget_does_not_wrap_a_function_per_widget(): + # on free-threaded Python, functools.wraps (setting attributes on a function) stops all threads, + # so creating a widget should not define and wrap a new function + import functools + + @react.component + def Test(): + with w.VBox() as main: + for i in range(10): + w.IntSlider(value=i) + return main + + with unittest.mock.patch("functools.update_wrapper", wraps=functools.update_wrapper) as update_wrapper: + box, rc = react.render(Test(), handle_error=False) + assert update_wrapper.call_count == 0 + slider = rc.find(widgets.IntSlider).widgets[0] + changes = [] + slider.observe(lambda change: changes.append(change["new"]), "value") + with slider.hold_trait_notifications(): + slider.value = 42 + assert changes == [] + assert changes == [42] + rc.close()