Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
' {}',
' {}',
' {}',
'',
' """',
' scream()',
])
for section, item in [('Parameters', 'x')]:
program = program_template.format(
section,
'-' * len(section),
item,
)
tree = ast.parse(program)
function = get_function_descriptions(tree)[0]
checker = IntegrityChecker(self.config)
checker.run_checks(function)
errors = checker.errors
self.assertTrue(
len(errors) > 0,
'EmptyDescriptionError not defined for {}'.format(section),
)
self.assertTrue(
any([
isinstance(error, EmptyDescriptionError)
for error in errors
]),
'EmptyDescriptionError not defined for {}: {}'.format(
section,
errors,
),
def test_skips_functions_without_docstrings(self):
program = '\n'.join([
'def function_without_docstring(arg1, arg2):',
' return 3',
])
tree = ast.parse(program)
functions = get_function_descriptions(tree)
checker = IntegrityChecker()
checker.run_checks(functions[0])
self.assertEqual(len(checker.errors), 0)
def test_ignore_private_methods(self):
program = '\n'.join([
'def function_with_missing_parameter(x):',
' """We\'re missing a description of x."""',
' print(x / 2)',
''
'def _same_error_but_private_method(x):',
' """We\'re missing a description of x."""',
' print(x / 2)',
])
tree = ast.parse(program)
functions = get_function_descriptions(tree)
with ConfigurationContext(
ignore=[],
message_template=None,
style=DocstringStyle.GOOGLE,
strictness=Strictness.FULL_DESCRIPTION,
ignore_regex=r'^_(.*)'
):
checker = IntegrityChecker()
checker.run_checks(functions[0])
checker.run_checks(functions[1])
errors = checker.errors
self.assertEqual(len(errors), 1)
def test_missing_yield_added_to_errors(self):
program = '\n'.join([
'def funtion_with_yield():',
' """This should have a yields section."""',
' yield 3',
])
tree = ast.parse(program)
functions = get_function_descriptions(tree)
checker = IntegrityChecker()
checker.run_checks(functions[0])
self.assertEqual(len(checker.errors), 1)
self.assertTrue(isinstance(checker.errors[0], MissingYieldError))
def get_n_errors(self, amount, src, config=dict()):
tree = ast.parse(src)
functions = get_function_descriptions(tree)
with ConfigurationContext(**config):
checker = IntegrityChecker()
checker.run_checks(functions[0])
errors = checker.errors
self.assertEqual(
len(errors),
amount,
(
'There should only be {} errors, '
'but there were {}: {}.'
).format(
amount,
len(errors),
' '.join([x.__class__.__name__ for x in errors])
)
)
def test_gets_functions(self):
program = '\n'.join([
'def top_level_function(arg):',
' """My docstring"""',
' return 1',
])
tree = ast.parse(program)
function = get_function_descriptions(tree)[0]
self.assertEqual(function.name, 'top_level_function')
self.assertEqual(function.argument_names, ['arg'])
self.assertEqual(function.has_return, True)
self.assertEqual(function.docstring, 'My docstring')
def test_setters_and_getters_treated_like_normal_methods(self):
program = '\n'.join([
'class SomeClass(object):',
'',
' @name.setter',
' def name(self, value):',
' pass',
])
tree = ast.parse(program)
function = get_function_descriptions(tree)[0]
self.assertEqual(function.argument_names, ['value'])
def test_catch_and_raise(self):
program = '\n'.join([
'def false_positive() -> None:',
' """summary',
'',
' Raises:',
' ValueError: description',
'',
' """',
' try:',
' raise ValueError("233")',
' except ValueError as e:',
' raise',
])
tree = ast.parse(program)
function = get_function_descriptions(tree)[0]
checker = IntegrityChecker()
checker.run_checks(function)
self.assertEqual(
len(checker.errors),
0,
checker.errors,
)
' """Reduce the list with the given function.',
'',
' :param fn: A function which takes two items and produces',
' one as a result.',
' :param l: The list to reduce.',
' :return: The final, reduced result of the list.',
'',
' """',
' if not l:',
' return _curr',
' if not _curr:',
' return reduce(fn, l[1:], l[0])',
' return reduce(fn, l[1:], fn(l[0], _curr))',
])
tree = ast.parse(program)
functions = get_function_descriptions(tree)
checker = IntegrityChecker(self.config)
checker.run_checks(functions[0])
errors = checker.errors
self.assertEqual(
len(errors), 0,
[(x.message()) for x in errors],
)
'',
' Args:',
' x: The divisor.',
' y: The dividend.',
'',
' Raises:',
' ZeroDivisionError: If y is zero.',
'',
' Returns:',
' The quotient.',
'',
' """',
' return x / y',
])
tree = ast.parse(program)
functions = get_function_descriptions(tree)
checker = IntegrityChecker()
checker.run_checks(functions[0])
self.assertEqual(
len(checker.errors), 1,
checker.errors
)
error = checker.errors[0]
self.assertTrue(isinstance(error, ExcessRaiseError))
self.assertEqual(error.name, 'ZeroDivisionError')