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