def test_parse_two_space_indent(self): docstring = '\n'.join([ 'Short.', '', 'Args:', ' x: Something something something.', ' Something something.', ' y: Something Else.', '', 'Returns:', ' A value.', ]) config = Configuration([], None, DocstringStyle.GOOGLE, Strictness.FULL_DESCRIPTION, indentation=2) tokens = condense(lex(docstring, config=config)) node = parse(tokens) annotation_lookup = self.get_annotation_lookup(node) values = { ArgumentIdentifier.extract(x) for x in annotation_lookup[ArgumentIdentifier] } self.assertEqual( values, {'x', 'y'}, )
def setUp(self): self.config = Configuration( ignore=[], message_template=None, style=DocstringStyle.NUMPY, strictness=Strictness.FULL_DESCRIPTION, )
def test_missing_parameter_types(self): program = '\n'.join([ 'def function_with_excess_parameter(extra):', ' """We have an extra parameter below, extra.', '', ' Args:', ' extra: This shouldn\'t be here.', '', ' """', ' print(\'Hey!\')', ]) tree = ast.parse(program) functions = get_function_descriptions(tree) checker = IntegrityChecker(config=Configuration( ignore=[], message_template=None, style=DocstringStyle.GOOGLE, strictness=Strictness.FULL_DESCRIPTION, enable=['DAR104'] )) checker.run_checks(functions[0]) errors = checker.errors self.assertEqual(len(errors), 1) self.assertTrue(isinstance(errors[0], ParameterTypeMissingError))
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) checker = IntegrityChecker( config=Configuration( ignore=[], message_template=None, style=DocstringStyle.GOOGLE, strictness=Strictness.FULL_DESCRIPTION, ignore_regex=r'^_(.*)' ) ) checker.run_checks(functions[0]) checker.run_checks(functions[1]) errors = checker.errors self.assertEqual(len(errors), 1)
def test_default_disabled_error(self): not_enabled_config = Configuration( ignore=[], message_template=None, style=DocstringStyle.GOOGLE, strictness=Strictness.FULL_DESCRIPTION, enable=[], ) enabled_config = Configuration( ignore=[], message_template=None, style=DocstringStyle.GOOGLE, strictness=Strictness.FULL_DESCRIPTION, enable=['DAR104'], ) src = '\n'.join([ 'def double(x):', ' """Double the value.', ' ', ' Args:', ' x: The value to double.', '', ' Returns:', ' The value, doubled.', '', ' """', ' return 2 * x', ]) errors = self.get_n_errors(0, src, not_enabled_config) self.assertEqual( len(errors), 0, ) errors = self.get_n_errors(1, src, enabled_config) self.assertEqual( len(errors), 1, )
def setUp(self): self.short_google_config = Configuration( ignore=[], message_template=None, style=DocstringStyle.GOOGLE, strictness=Strictness.SHORT_DESCRIPTION, ) self.long_google_config = Configuration( ignore=[], message_template=None, style=DocstringStyle.GOOGLE, strictness=Strictness.LONG_DESCRIPTION, ) self.full_google_config = Configuration( ignore=[], message_template=None, style=DocstringStyle.GOOGLE, strictness=Strictness.FULL_DESCRIPTION, ) self.short_sphinx_config = Configuration( ignore=[], message_template=None, style=DocstringStyle.SPHINX, strictness=Strictness.SHORT_DESCRIPTION, ) self.long_sphinx_config = Configuration( ignore=[], message_template=None, style=DocstringStyle.SPHINX, strictness=Strictness.LONG_DESCRIPTION, ) self.full_sphinx_config = Configuration( ignore=[], message_template=None, style=DocstringStyle.SPHINX, strictness=Strictness.FULL_DESCRIPTION, ) self.short_docstring = 'Adds an item to the head of the list.' self.long_docstring = '\n'.join([ 'Adds an item to the head of the list', '', 'Not very pythonic.', ]) self.full_docstring = '\n'.join([ 'Adds an item to the head of the list', '', 'Not very pythonic, but oh well.', '', 'Args:', ' x: Definitely only the head is required.', ]) self.full_docstring_sphinx = '\n'.join([ 'Adds an item to the head of the list', '', 'Not very pythonic, but oh well.', '', ':param x: Definitely only the head is required.', ])
def __enter__(self): # Save the state of the original item. for keyword, value in vars(self.config).items(): self.original[keyword] = getattr(self.config, keyword) # Override with the default configuration. default_config = Configuration.get_default_instance() for keyword in self.original: setattr(self.config, keyword, getattr(default_config, keyword)) # Apply test-specific values. for keyword, value in self.kwargs.items(): setattr(self.config, keyword, value) return self.config
def setUp(self): self.config = Configuration( ignore=[], message_template=None, style=DocstringStyle.SPHINX )