Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def assertHasNoErrors(self, config, docstring):
with ConfigurationContext(**config):
checker = IntegrityChecker()
checker.run_checks(self.get_function_with(docstring))
errors = checker.errors
self.assertEqual(
len(errors),
0,
[(x.message()) for x in errors]
)
def test_missing_parameter_added(self):
program = '\n'.join([
'def function_with_missing_parameter(x):',
' """We\'re missing a description of x."""',
' print(x / 2)',
])
tree = ast.parse(program)
functions = get_function_descriptions(tree)
checker = IntegrityChecker()
checker.run_checks(functions[0])
errors = checker.errors
self.assertEqual(len(errors), 1)
self.assertTrue(isinstance(errors[0], MissingParameterError))
' name (str): module name',
' ',
' Returns:',
' bool: Whether the module can be imported',
' ',
' """',
' try:',
' __import__(name)',
' except ImportError:',
' return False',
' else:',
' return True',
])
tree = ast.parse(program)
functions = get_function_descriptions(tree)
checker = IntegrityChecker()
checker.run_checks(functions[0])
errors = checker.errors
self.assertEqual(len(errors), 0)
' """Tell the person hello.',
'',
' {}:',
' {}:',
'',
' """',
' person.hello()',
])
for section, item in [
('Args', 'name'),
('Raises', 'Exception'),
]:
program = program_template.format(section, item)
tree = ast.parse(program)
functions = get_function_descriptions(tree)
checker = IntegrityChecker()
checker.run_checks(functions[0])
errors = checker.errors
self.assertTrue(
len(errors) > 0,
'Failed to raise any errors for {}'.format(section),
)
self.assertTrue(
any([
isinstance(error, EmptyDescriptionError)
for error in errors
]),
'Failed to raise EmptyDescriptionError for {}'.format(section),
)
def has_no_errors(self, program):
tree = ast.parse(program)
functions = get_function_descriptions(tree)
checker = IntegrityChecker()
checker.run_checks(functions[0])
self.assertEqual(
len(checker.errors),
0,
'Expected there to be no errors, but there were {}:\n\t{}'.format(
len(checker.errors),
'\n\t'.join([x.general_message for x in checker.errors])
)
' The list to reduce.',
'',
' Returns',
' -------',
' 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],
)
def test_yields_from_added_to_error(self):
program = '\n'.join([
'def function_with_yield():',
' """This should have a yields section."""',
' yield from (x for x in range(10))',
])
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 test_return_type_unchecked_if_not_defined_in_function(self):
program = '\n'.join([
'def foo():',
' """Just a foobar.',
'',
' Returns:',
' str: bar',
'',
' """',
' return "bar"',
])
tree = ast.parse(program)
functions = get_function_descriptions(tree)
checker = IntegrityChecker()
checker.run_checks(functions[0])
self.assertEqual(len(checker.errors), 0)
raise_errors_for_syntax: True if we want parser errors
to propagate up (crashing darglint.) This is useful
if we are developing on darglint -- we can get the stack
trace and know exactly where darglint failed.
message_template: A python format string for specifying
how the message should appear to the user.
Returns:
An error report for the file.
"""
program = read_program(filename)
try:
tree = ast.parse(program)
functions = get_function_descriptions(tree)
checker = IntegrityChecker(
raise_errors=raise_errors_for_syntax,
)
for function in functions:
checker.schedule(function)
return checker.get_error_report_string(
verbosity,
filename,
message_template=message_template,
)
except SyntaxError as e:
error = darglint.errors.PythonSyntaxError(e)
report = ErrorReport([error], filename, verbosity, message_template)
return str(report)