Skip to content

Commit

Permalink
Added Alex's fix for empty docstrings so sw tests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
Taylor-96 committed Feb 18, 2025
1 parent 3b0e400 commit 311f42c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
37 changes: 28 additions & 9 deletions src/widget_code_input/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import anywidget
import traitlets
import os
from traitlets import Unicode, validate, TraitError
from traitlets import Unicode, validate, TraitError, Any, observe
from .frontend import module_name, module_version

from .utils import (
Expand All @@ -28,7 +28,10 @@ class WidgetCodeInput(anywidget.AnyWidget):

function_name = Unicode('example').tag(sync=True)
function_parameters = Unicode('').tag(sync=True)
docstring = Unicode('\n').tag(sync=True)
docstring = Any(default_value=None, allow_none=True).tag(sync=True)


# docstring = Unicode('\n').tag(sync=True)
function_body = Unicode('').tag(sync=True)
code_theme = Unicode('').tag(sync=True)
widget_instance_count_trait = Unicode(f'').tag(sync=True)
Expand Down Expand Up @@ -62,8 +65,6 @@ def _valid_docstring(self, docstring):
"""
Validate that the docstring do not contain triple double quotes
"""
if '"""' in docstring['value']:
raise TraitError('The docstring cannot contain triple double quotes (""")')
return docstring['value']


Expand All @@ -73,7 +74,7 @@ def __init__( # pylint: disable=too-many-arguments
self,
function_name,
function_parameters="",
docstring="\n",
docstring=None,
function_body="",
code_theme="basicLight",
):
Expand All @@ -94,7 +95,18 @@ def __init__( # pylint: disable=too-many-arguments

self.function_name = function_name
self.function_parameters = function_parameters
self.docstring = docstring
if docstring is None:
# we cannot store docstring as None so we use
# a variable to signify that it was None
self.docstring = ""
self._display_docstring = False
elif docstring.startswith("\"\"\"") and docstring.endswith("\"\"\""):
# assume the quotation marks have been added so we do not need to add them
self.docstring = docstring.strip('\"\"\"')
self._display_docstring = True
else:
self.docstring = docstring
self._display_docstring = True
self.function_body = function_body
self.code_theme = code_theme
self.widget_instance_count_trait=f"{WidgetCodeInput.widget_instance_count}"
Expand All @@ -104,7 +116,15 @@ def __init__( # pylint: disable=too-many-arguments

name = traitlets.Unicode().tag(sync=True)



@observe("docstring")
def _on_docstring_changed(self, change):
if change["new"] is None:
# Use set_trait to avoid infinite recursion
self.set_trait("docstring", "")
self._display_docstring = False
else:
self._display_docstring = True


@property
Expand All @@ -114,7 +134,7 @@ def full_function_code(self):
including signature, docstring and body
"""
return build_function(
self.function_signature, self.docstring, self.function_body
self.function_signature, self.docstring if self._display_docstring else None, self.function_body
)

@property
Expand Down Expand Up @@ -174,4 +194,3 @@ def wrapper(*args, **kwargs):

return catch_exceptions(function_object)


2 changes: 1 addition & 1 deletion src/widget_code_input/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def build_pre_body(signature, docstring, indent_level=4):
:param docstring: the (unindented) docstring
:param indent_level: integer number of spaces to prepend to the docstring
"""
if '"""' in docstring:
if docstring is not None and '"""' in docstring:
raise ValueError('Triple double quotes (""") not allowed in docstring')

return "{}\n{}".format(
Expand Down

0 comments on commit 311f42c

Please sign in to comment.