Skip to content

Commit d5c2517

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. The line numbers are now counted from the function body if the docstring is empty.
1 parent 3b0e400 commit d5c2517

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

js/widget.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,19 @@ function signatureValueChanged() {
205205

206206
function updateLineNumbers() {
207207
const linesSignature = mySignatureCodeMirror.state.doc.toString().split('\n').length;
208-
const linesDocstring = myDocstringCodeMirror.state.doc.toString().split('\n').length;
209208

209+
const docstring = myDocstringCodeMirror.state.doc.toString();
210+
const linesDocstring = docstring == "" ? 0 : docstring.split('\n').length;
210211

211212
// increment line numbers in docstring text area by the number of lines in the signature
212213
myDocstringCodeMirror.dispatch({
213-
effects: guttercomp.reconfigure(
214-
lineNumbers({ formatNumber: n=>linesSignature+n }))
214+
effects: guttercomp.reconfigure(
215+
lineNumbers({ formatNumber: n=> linesDocstring == 0 ? "" : linesSignature+n }))
215216
});
216217

217218
myBodyCodeMirror.dispatch({
218-
effects: guttercomp.reconfigure(
219-
lineNumbers({ formatNumber: n=>linesSignature+linesDocstring+n}))
219+
effects: guttercomp.reconfigure(
220+
lineNumbers({ formatNumber: n=>linesSignature+linesDocstring+n}))
220221
});
221222

222223
}
@@ -225,7 +226,7 @@ function signatureValueChanged() {
225226
// Set the value from python into the CodeMirror widget in the
226227
// frontend.
227228

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

230231
if (newDocstring !== myDocstringCodeMirror.state.doc.toString()) {
231232
myDocstringCodeMirror.dispatch({

src/widget_code_input/__init__.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ def _valid_docstring(self, docstring):
6262
"""
6363
Validate that the docstring do not contain triple double quotes
6464
"""
65-
if '"""' in docstring['value']:
66-
raise TraitError('The docstring cannot contain triple double quotes (""")')
6765
return docstring['value']
6866

6967

@@ -73,7 +71,7 @@ def __init__( # pylint: disable=too-many-arguments
7371
self,
7472
function_name,
7573
function_parameters="",
76-
docstring="\n",
74+
docstring=None,
7775
function_body="",
7876
code_theme="basicLight",
7977
):
@@ -94,7 +92,18 @@ def __init__( # pylint: disable=too-many-arguments
9492

9593
self.function_name = function_name
9694
self.function_parameters = function_parameters
97-
self.docstring = docstring
95+
if docstring is None:
96+
# we cannot store docstring as None so we use
97+
# a variable to signify that it was None
98+
self.docstring = ""
99+
self._display_docstring = False
100+
elif docstring.startswith("\"") and docstring.endswith("\""):
101+
# assume the quotation marks have been added so we do not need to add them
102+
self.docstring = docstring
103+
self._display_docstring = True
104+
else:
105+
self.docstring = f"\"\"\"{docstring}\"\"\""
106+
self._display_docstring = True
98107
self.function_body = function_body
99108
self.code_theme = code_theme
100109
self.widget_instance_count_trait=f"{WidgetCodeInput.widget_instance_count}"
@@ -114,7 +123,7 @@ def full_function_code(self):
114123
including signature, docstring and body
115124
"""
116125
return build_function(
117-
self.function_signature, self.docstring, self.function_body
126+
self.function_signature, self.docstring if self._display_docstring else None, self.function_body
118127
)
119128

120129
@property

src/widget_code_input/utils.py

+2-3
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)