Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
' """foobar',
'',
' Args:',
' foo: foobar',
'',
' Returns:',
' bar',
'',
' """',
' return "bar"',
])
function = ast.parse(contents).body[0]
docstring = ast.get_docstring(function)
tokens = condense(lex(docstring))
node = parse(tokens)
self.assertTrue(CykNodeUtils.contains(node, 'short-description'))
self.assertTrue(CykNodeUtils.contains(node, 'returns-section'))
self.assertTrue(CykNodeUtils.contains(node, 'arguments-section'))
def test_parse_noqa_with_target_and_argument(self):
"""Make sure we can target specific args in a noqa."""
node = parse(condense(lex('# noqa: I101 arg1, arg2\n')))
self.assertTrue(CykNodeUtils.contains(node, 'noqa'), str(node))
def test_parse_sole_argument_with_two_lines_indent_error(self):
docstring = '\n'.join([
'Short.',
'',
'Args:',
' x: Something something something.',
' Something something.',
' Something, something, something.',
'',
'Returns:',
' A value.',
])
tokens = condense(lex(docstring))
node = parse(tokens)
self.assertTrue(CykNodeUtils.contains(node, 'arguments-section'))
' SomethingException: Randomly.',
'',
'Non-Standard:'
' Everything about this.',
'',
'Yields:',
' Values to analyze.',
]))))
for symbol in [
'short-description',
'long-description',
'arguments-section',
'raises-section',
'yields-section',
]:
self.assertTrue(CykNodeUtils.contains(node, symbol))
def test_type_is_type(self):
docstring = '\n'.join([
'Takes a class and returns an instance.',
'',
'Args:',
' klass (type): A class to instantiate.',
' args (List[int]): The initial arguments to pass to it.',
'',
])
node = parse(condense(lex(docstring)))
self.assertTrue(CykNodeUtils.contains(node, 'type-section-parens'))
def test_parse_whole_description(self):
"""Make sure we can handle descriptions of multiple lines."""
node = parse(condense(lex(
'Short description\n'
'\n'
'Long : (description)\n'
'\n'
' <code></code>\n'
'\n'
)))
self.assertTrue(node)
self.assertTrue(CykNodeUtils.contains(node, 'short-description'))
self.assertTrue(CykNodeUtils.contains(node, 'long-description'))
def test_parse_vartype_item_cyk(self):
"""Ensure we can parse a variable type description."""
node = parse(condense(lex(
'short\n\n:vartype foo: Dict[str][str]'
)))
self.assertTrue(
CykNodeUtils.contains(node, 'variable-type-section'),
node,
)
],
'argument-type-section': ['type'],
'variable-type-section': ['vartype'],
'raises-section': ['raises'],
# 'returns-section': ['returns'],
# 'return-type-section': ['rtype'],
'yields-section': ['yield', 'yields'],
}
for keyword_section in keywords:
for keyword in keywords[keyword_section]:
docstring = 'Short description.\n\n:{} a: something'.format(
keyword,
)
node = parse(condense(lex(docstring)))
self.assertTrue(
CykNodeUtils.contains(node, keyword_section),
'{}: {}'.format(keyword_section, node)
)
def test_noqa_always_on_left(self):
raw = '\n'.join([
'# noqa',
'b',
])
node = self.parse_string(raw)
for noqa in self.get_identifiers(node, NoqaIdentifier):
self.assertFalse(
CykNodeUtils.contains(noqa, 'long-description'),
'The noqa should be on its own, but was not:\n{}'.format(
noqa
)
def test_no_blank_line_swallows_sections(self):
"""Make sure there must be a blank line after the short description."""
node = parse(condense(lex('\n'.join([
'Should not have a raises section.',
'Args:',
' x: The divisor.',
' y: The dividend.',
'',
]))))
self.assertFalse(
CykNodeUtils.contains(node, 'arguments-section'),
'The arguments section should have been eaten -- that way the '
'user gets a signal that they should separate the sections.'