Skip to content

Commit

Permalink
Allow docstring to use single quotes and to not be displayed
Browse files Browse the repository at this point in the history
By specifying None for the docstring no docstring is displayed. By
specifying the quotation mark in the docstring the docstring is not
put into triple quotes allowing a docstring in signle quotes.

The implementation puts the responsibility to add the correct quotation
marks to the python side so we can be more flexible.
  • Loading branch information
agoscinski committed Dec 11, 2024
1 parent 3b0e400 commit ff4500a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion js/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function signatureValueChanged() {
// Set the value from python into the CodeMirror widget in the
// frontend.

const newDocstring = '"""' + model.get('docstring') + '"""';
const newDocstring = model.get('docstring');

if (newDocstring !== myDocstringCodeMirror.state.doc.toString()) {
myDocstringCodeMirror.dispatch({
Expand Down
12 changes: 10 additions & 2 deletions src/widget_code_input/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,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 +94,15 @@ def __init__( # pylint: disable=too-many-arguments

self.function_name = function_name
self.function_parameters = function_parameters
self.docstring = docstring
if docstring is not None and '"""' in docstring:
raise ValueError('Triple double quotes (""") not allowed in docstring')
if docstring is None:
self.docstring = ""
elif docstring.startswith("\"") and docstring.endswith("\""):
# assume the quotation marks have been added so we do not need to add them
self.docstring = docstring
else:
self.docstring = f"\"\"\"{docstring}\"\"\""
self.function_body = function_body
self.code_theme = code_theme
self.widget_instance_count_trait=f"{WidgetCodeInput.widget_instance_count}"
Expand Down
5 changes: 2 additions & 3 deletions src/widget_code_input/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ 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:
raise ValueError('Triple double quotes (""") not allowed in docstring')

return "{}\n{}".format(
signature,
prepend_indent('"""{}"""'.format(docstring), indent_level=indent_level),
prepend_indent('' if docstring is None else '"""{}"""'.format(docstring),
indent_level=indent_level),
)


Expand Down

0 comments on commit ff4500a

Please sign in to comment.