Exemple #1
0
    def test_parse_argument_with_two_lines(self):
        program = ('''
class _BaseError(object):
    def message(self, verbosity=1, raises=True):  # type: (int, bool) -> str
        """Get the message for this error, according to the verbosity.

        Args:
            verbosity: An integer in the set {1,2}, where 1 is a more
                terse message, and 2 includes a general description.
            raises: True if it should raise an exception.

        Raises:
            Exception: If the verbosity level is not recognized.

        Returns:
            An error message.

        """
        pass
''')
        docstring = ast.get_docstring(ast.parse(program).body[0].body[0])
        tokens = condense(lex(docstring))
        node = parse(tokens)
        self.assertTrue(CykNodeUtils.contains(node, 'arguments-section'))
        annotation_lookup = self.get_annotation_lookup(node)
        self.assertEqual(len(annotation_lookup[ArgumentIdentifier]), 2)
        values = {
            ArgumentIdentifier.extract(x)
            for x in annotation_lookup[ArgumentIdentifier]
        }
        self.assertEqual(
            values,
            {'verbosity', 'raises'},
        )
Exemple #2
0
 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 test_parse_args_section_with_quotation(self):
     tokens = condense(lex('\n'.join([
         'Args:',
         '    x: Some description with "quotes"',
         '    y: The next item, which should appear.',
     ])))
     node = parse(tokens)
     self.assertEqual(
         node.symbol,
         'arguments-section',
         str(node),
     )
     args = CykNodeUtils.get_annotated(node, ArgumentIdentifier)
     self.assertEqual(
         {ArgumentIdentifier.extract(x) for x in args},
         {'x', 'y'},
     )
Exemple #4
0
 def test_parse_sole_argument_with_two_lines(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'))
     annotation_lookup = self.get_annotation_lookup(node)
     values = {
         ArgumentIdentifier.extract(x)
         for x in annotation_lookup[ArgumentIdentifier]
     }
     self.assertEqual(
         values,
         {'x'},
     )