Skip to content

Commit ff4500a

Browse files
committed
Allow docstring to use single quotes and to not be displayed
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.
1 parent 3b0e400 commit ff4500a

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

js/widget.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ function signatureValueChanged() {
225225
// Set the value from python into the CodeMirror widget in the
226226
// frontend.
227227

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

230230
if (newDocstring !== myDocstringCodeMirror.state.doc.toString()) {
231231
myDocstringCodeMirror.dispatch({

src/widget_code_input/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def __init__( # pylint: disable=too-many-arguments
7373
self,
7474
function_name,
7575
function_parameters="",
76-
docstring="\n",
76+
docstring=None,
7777
function_body="",
7878
code_theme="basicLight",
7979
):
@@ -94,7 +94,15 @@ def __init__( # pylint: disable=too-many-arguments
9494

9595
self.function_name = function_name
9696
self.function_parameters = function_parameters
97-
self.docstring = docstring
97+
if docstring is not None and '"""' in docstring:
98+
raise ValueError('Triple double quotes (""") not allowed in docstring')
99+
if docstring is None:
100+
self.docstring = ""
101+
elif docstring.startswith("\"") and docstring.endswith("\""):
102+
# assume the quotation marks have been added so we do not need to add them
103+
self.docstring = docstring
104+
else:
105+
self.docstring = f"\"\"\"{docstring}\"\"\""
98106
self.function_body = function_body
99107
self.code_theme = code_theme
100108
self.widget_instance_count_trait=f"{WidgetCodeInput.widget_instance_count}"

src/widget_code_input/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,11 @@ def build_pre_body(signature, docstring, indent_level=4):
4747
:param docstring: the (unindented) docstring
4848
:param indent_level: integer number of spaces to prepend to the docstring
4949
"""
50-
if '"""' in docstring:
51-
raise ValueError('Triple double quotes (""") not allowed in docstring')
5250

5351
return "{}\n{}".format(
5452
signature,
55-
prepend_indent('"""{}"""'.format(docstring), indent_level=indent_level),
53+
prepend_indent('' if docstring is None else '"""{}"""'.format(docstring),
54+
indent_level=indent_level),
5655
)
5756

5857

0 commit comments

Comments
 (0)