Skip to content

Support display no docstring #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading