Skip to content

Commit

Permalink
Support display no docstring (#119)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
agoscinski authored Mar 5, 2025
1 parent 44495c4 commit 8d4f6aa
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/src/getting_started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
" update=update_func,\n",
" update_mode=\"continuous\",\n",
" title=\"Sinus function\",\n",
" description=\"Implements $\\sin(x\\omega)$\",\n",
" description=\"Implements $\\\\sin(x\\\\omega)$\",\n",
")\n",
"\n",
"code_ex.run_update() # For the demonstration we run the widget one time\n",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ classifiers = [
dependencies = [
"ipywidgets>=8.0.0",
"numpy<2.0.0",
"widget_code_input>=4.0.13",
"widget_code_input>=4.0.17",
"matplotlib",
"termcolor"
]
Expand Down
11 changes: 7 additions & 4 deletions src/scwidgets/code/_widget_code_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import types
import warnings
from functools import wraps
from typing import Any, List, Optional, Tuple
from typing import Any, List, Optional, Tuple, Union

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

@staticmethod
def get_docstring(function: types.FunctionType) -> str:
def get_docstring(function: types.FunctionType) -> Union[str, None]:
docstring = function.__doc__
return "" if docstring is None else textwrap.dedent(docstring)
return (
None
if docstring is None
else textwrap.dedent(docstring).strip('"""') # noqa: B005
)

@staticmethod
def _get_function_source_and_def(
Expand Down
1 change: 1 addition & 0 deletions tests/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def test_get_function_paramaters(self):
assert CodeInput.get_function_parameters(self.mock_function_7) == "x, **kwargs"

def test_get_docstring(self):
assert CodeInput.get_docstring(self.mock_function_0) is None
assert (
CodeInput.get_docstring(self.mock_function_1)
== "\nThis is an example function.\nIt adds two numbers.\n"
Expand Down

0 comments on commit 8d4f6aa

Please sign in to comment.