Commit 41cea70a authored by Matthias Bussonnier's avatar Matthias Bussonnier Committed by INADA Naoki

bpo-29637: clean docstring only if not None (GH-267)

parent 357bad71
...@@ -194,11 +194,14 @@ def get_docstring(node, clean=True): ...@@ -194,11 +194,14 @@ def get_docstring(node, clean=True):
Return the docstring for the given node or None if no docstring can Return the docstring for the given node or None if no docstring can
be found. If the node provided does not have docstrings a TypeError be found. If the node provided does not have docstrings a TypeError
will be raised. will be raised.
If *clean* is `True`, all tabs are expanded to spaces and any whitespace
that can be uniformly removed from the second line onwards is removed.
""" """
if not isinstance(node, (AsyncFunctionDef, FunctionDef, ClassDef, Module)): if not isinstance(node, (AsyncFunctionDef, FunctionDef, ClassDef, Module)):
raise TypeError("%r can't have docstrings" % node.__class__.__name__) raise TypeError("%r can't have docstrings" % node.__class__.__name__)
text = node.docstring text = node.docstring
if clean: if clean and text:
import inspect import inspect
text = inspect.cleandoc(text) text = inspect.cleandoc(text)
return text return text
......
...@@ -532,6 +532,7 @@ class ASTHelpers_Test(unittest.TestCase): ...@@ -532,6 +532,7 @@ class ASTHelpers_Test(unittest.TestCase):
node = ast.parse('async def foo():\n """spam\n ham"""') node = ast.parse('async def foo():\n """spam\n ham"""')
self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham') self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
self.assertIsNone(ast.get_docstring(ast.parse('')))
def test_literal_eval(self): def test_literal_eval(self):
self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3]) self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment