Skip to content

Commit 8d4f6aa

Browse files
authored
Support display no docstring (#119)
The `WidgetCodeInput` now supports to display no docstring docstring. We now support in scwidgets to pass `None` value when no docstring should be displayed. Fixed some formatting error in docs.
1 parent 44495c4 commit 8d4f6aa

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

docs/src/getting_started.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
" update=update_func,\n",
9090
" update_mode=\"continuous\",\n",
9191
" title=\"Sinus function\",\n",
92-
" description=\"Implements $\\sin(x\\omega)$\",\n",
92+
" description=\"Implements $\\\\sin(x\\\\omega)$\",\n",
9393
")\n",
9494
"\n",
9595
"code_ex.run_update() # For the demonstration we run the widget one time\n",

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ classifiers = [
3030
dependencies = [
3131
"ipywidgets>=8.0.0",
3232
"numpy<2.0.0",
33-
"widget_code_input>=4.0.13",
33+
"widget_code_input>=4.0.17",
3434
"matplotlib",
3535
"termcolor"
3636
]

src/scwidgets/code/_widget_code_input.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import types
99
import warnings
1010
from functools import wraps
11-
from typing import Any, List, Optional, Tuple
11+
from typing import Any, List, Optional, Tuple, Union
1212

1313
from widget_code_input import WidgetCodeInput
1414
from widget_code_input.utils import (
@@ -71,7 +71,6 @@ def __init__(
7171
if function_name is None:
7272
raise ValueError("function_name must be given if no function is given.")
7373
function_parameters = "" if function_parameters is None else function_parameters
74-
docstring = "\n" if docstring is None else docstring
7574
function_body = "" if function_body is None else function_body
7675
self._builtins = {} if builtins is None else builtins
7776
super().__init__(
@@ -152,9 +151,13 @@ def function_parameters_name(self) -> List[str]:
152151
return self.function_parameters.replace(",", "").split(" ")
153152

154153
@staticmethod
155-
def get_docstring(function: types.FunctionType) -> str:
154+
def get_docstring(function: types.FunctionType) -> Union[str, None]:
156155
docstring = function.__doc__
157-
return "" if docstring is None else textwrap.dedent(docstring)
156+
return (
157+
None
158+
if docstring is None
159+
else textwrap.dedent(docstring).strip('"""') # noqa: B005
160+
)
158161

159162
@staticmethod
160163
def _get_function_source_and_def(

tests/test_code.py

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def test_get_function_paramaters(self):
7979
assert CodeInput.get_function_parameters(self.mock_function_7) == "x, **kwargs"
8080

8181
def test_get_docstring(self):
82+
assert CodeInput.get_docstring(self.mock_function_0) is None
8283
assert (
8384
CodeInput.get_docstring(self.mock_function_1)
8485
== "\nThis is an example function.\nIt adds two numbers.\n"

0 commit comments

Comments
 (0)