From 973e6103e15b7e839166d811102030a4b9d1bbf0 Mon Sep 17 00:00:00 2001 From: Maarten Breddels Date: Tue, 10 Nov 2026 15:23:28 +0100 Subject: [PATCH] fix: show component docstrings in Sphinx autodoc Sphinx users could not document their components. Sphinx autodoc skips a module-level component without a warning, and it shows a component on a class as "Method = react.component(mylib.Method)". The docstring was on the object, but Sphinx did not treat the object as a function. ComponentFunction now has a __get__ method that returns the component itself. Python already gave that result, so attribute access does not change. The method makes inspect.isroutine true, and Sphinx uses that check to choose its function documenter. A Sphinx build with automodule showed a module-level component and a component on a class with their signatures and docstrings. A new test failed before this change and passes now. Co-Authored-By: Claude Opus 5.5 --- reacton/core.py | 5 +++++ reacton/core_test.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/reacton/core.py b/reacton/core.py index 75804de..294b986 100644 --- a/reacton/core.py +++ b/reacton/core.py @@ -738,6 +738,11 @@ def __init__(self, f: Callable[[], Element], mime_bundle=mime_bundle_default, va self.render_count = 0 # used for debugging, not thread safe, so count can be off functools.update_wrapper(self, f) + def __get__(self, instance, owner=None): + # Python does the same without this method. Having it makes inspect.isroutine true, + # so Sphinx autodoc documents a component as a function or method, with its docstring. + return self + def widget(self, **kwargs): _ensure_colab_fixes() return self.widget_class()(**kwargs) diff --git a/reacton/core_test.py b/reacton/core_test.py index 25c27b6..c4ca366 100644 --- a/reacton/core_test.py +++ b/reacton/core_test.py @@ -1,4 +1,5 @@ import gc +import inspect import sys import threading import time @@ -346,6 +347,23 @@ def test_monkey_patch(): rc.close() +def test_component_function_is_routine(): + # Sphinx autodoc only documents a member as a function or method when inspect.isroutine is true + class Holder: + @react.component + def method(self): + """Method docstring.""" + return w.Button() + + component = Holder.__dict__["method"] + assert isinstance(component, reacton.core.ComponentFunction) + assert inspect.isroutine(component) + assert component.__doc__ == "Method docstring." + # reading it from the class or an instance gives the component itself, there is no binding + assert Holder.method is component + assert Holder().method is component + + def test_component_function(): clear() assert count() == 0