Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_parser_sections_correctly(self):
program = '\n'.join([
'def func(x, l):',
' """Add an item to the head of the list.',
' ',
' :param x: The item to add to the list.',
' :return: The list with the item attached.',
' ',
' """',
' return l.appendleft(x)',
])
doc = ast.get_docstring(ast.parse(program).body[0])
tokens = condense(lex(doc))
node = parse(tokens)
self.assertTrue(
CykNodeUtils.contains(node, 'returns-section'),
)
self.assertTrue(
CykNodeUtils.contains(node, 'arguments-section'),
)
def test_parse_star_arguments(self):
docstring = '\n'.join([
'Negate a function which returns a boolean.',
'',
'Args:',
' *fns (int): Functions which returns a boolean.',
'',
'Returns:',
' int: A function which returns fallse when any of the'
' callables return true, and true will all of the ',
' callables return false.',
])
tokens = condense(lex(docstring))
tree = parse(tokens)
self.assertTrue(tree is not None)
self.assertContains(tree, 'arguments')
def test_other_parameters_section(self):
raw_docstring = '\n'.join([
'Translate the string to the target language.',
'',
'Parameters',
'----------',
'x : str',
' The string to translate.',
'',
'Other Parameters',
'----------------',
'target : str',
' The target language.',
'',
])
tokens = condense(lex(raw_docstring))
docstring = parse(tokens)
self.assertContains(docstring, 'other-arguments-section')
self.assertIdentified(
docstring,
ArgumentItemIdentifier,
{'x', 'target'},
)
def test_parse_line_without_type_but_with_parentheses(self):
"""Make sure we can have parentheses otherwise."""
node = parse(condense(lex(
' A list of items (of type T), which pass the given test'
)))
self.assertTrue(node)
def test_can_parse_return_type(self):
docstring = '\n'.join([
'Return an approximation of pi.',
'',
'Returns:',
' Decimal: An approximation of pi.',
])
node = parse(condense(lex(docstring)))
self.assertTrue(CykNodeUtils.contains(node, 'returns-section'))
self.assertTrue(CykNodeUtils.contains(node, 'returns-type'))
def test_parse_short_description_multiple_words_cyk(self):
"""Make sure we can have a normal short description."""
content = 'Flips the pancake.'
node = parse(condense(lex(content)))
self.assertTrue(CykNodeUtils.contains(node, 'short-description'))
def test_parse_bare_noqa_statement(self):
"""Make sure we can parse noqa statements."""
node = parse(condense(lex('# noqa\n')))
self.assertTrue(CykNodeUtils.contains(node, 'noqa'))
# It breaks this into two sections.
#
# # Separation without indent
# ':raises RuntimeError:\n'
# '\n'
# ' Long description of why this happens.',
# Separation with indent
':raises RuntimeError:\n'
' \n'
' Long description of why this happens.',
]
doc_template = 'Short description.\n\n{}'
for raises_variant in raises_variants:
raw_docstring = doc_template.format(raises_variant)
tokens = condense(lex(raw_docstring))
node = parse(tokens)
self.assertTrue(
CykNodeUtils.contains(node, 'raises-section'),
'Variant failed: {}'.format(repr(raises_variant)),
)
def test_warns_section(self):
raw_docstring = '\n'.join([
'Always warn.',
'',
'Warns',
'-----',
'Warning',
' Under all conditions.',
'',
])
tokens = condense(lex(raw_docstring))
docstring = parse(tokens)
self.assertContains(docstring, 'warns-section')
def __init__(self, root, style=DocstringStyle.GOOGLE):
# type: (Union[CykNode, str], DocstringStyle) -> None
"""Create a new docstring from the AST.
Args:
root: The root of the AST, or the docstring
(as a string.) If it is a string, the
string will be parsed.
style: The style of the docstring. Discarded,
since this Docstring is always the Google style.
"""
if isinstance(root, CykNode):
self.root = root
else:
self.root = parse(condense(lex(root)))
self._lookup = self._discover()