Skip to content

Commit 6e52126

Browse files
committed
bpo-29637: clean docstring only if not None
Likely introduced by the combinaison of bpo-29622 and bpo-29463 http://bugs.python.org/issue29463 http://bugs.python.org/issue29622
1 parent 1aceb02 commit 6e52126

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

Lib/ast.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,14 @@ def get_docstring(node, clean=True):
194194
Return the docstring for the given node or None if no docstring can
195195
be found. If the node provided does not have docstrings a TypeError
196196
will be raised.
197+
198+
If *clean* is `True`, all tabs are expanded to spaces and any whitespace
199+
that can be uniformly removed from the second line onwards is removed.
197200
"""
198201
if not isinstance(node, (AsyncFunctionDef, FunctionDef, ClassDef, Module)):
199202
raise TypeError("%r can't have docstrings" % node.__class__.__name__)
200203
text = node.docstring
201-
if clean:
204+
if clean and text:
202205
import inspect
203206
text = inspect.cleandoc(text)
204207
return text

Lib/test/test_ast.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ def test_get_docstring(self):
532532

533533
node = ast.parse('async def foo():\n """spam\n ham"""')
534534
self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
535+
self.assertIsNone(ast.get_docstring(ast.parse('')))
535536

536537
def test_literal_eval(self):
537538
self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])

0 commit comments

Comments
 (0)