Beispiel #1
0
    def test_bare_raise(self):
        line = '\n'.join([
            'def foo():',
            '   try:',
            '      12/0',
            '   except:',
            '      raise',
        ])

        actual = parse(line)[0]
        self.assertDictEqual(
            {
                'name': 'foo',
                'params': [],
                'return_type': None,
                'start_lineno': 1,
                'start_col': 0,
                'end_lineno': 5,
                'end_col': 11,
                'is_doc_exists': False,
                'exceptions': [''],
                'yields': [],
            },
            actual,
        )
Beispiel #2
0
 def test_with_args_and_kwargs(self):
     line = """def foo(*args, **kwargs): pass"""
     actual = parse(line)[0]
     self.assertDictEqual(
         {
             'name': 'foo',
             'params': [
                 {
                     'argument': 'args',
                     'annotation': None,
                     'default': None,
                 },
                 {
                     'argument': 'kwargs',
                     'annotation': None,
                     'default': None,
                 },
             ],
             'return_type': None,
             'start_lineno': 1,
             'start_col': 0,
             'end_lineno': 1,
             'end_col': len(line),
             'is_doc_exists': False,
             'exceptions': [],
             'yields': [],
         },
         actual,
     )
Beispiel #3
0
 def test_async_keyword(self):
     line = '\n'.join([
         'async def foo(arg1):',
         '   pass',
     ])
     actual = parse(line)[0]
     self.assertDictEqual(
         {
             'name': 'foo',
             'params': [{
                 'argument': 'arg1',
                 'annotation': None,
                 'default': None,
             }],
             'return_type': None,
             'start_lineno': 1,
             'start_col': 0,
             'end_lineno': 2,
             'end_col': 7,
             'is_doc_exists': False,
             'exceptions': [],
             'yields': [],
         },
         actual,
     )
Beispiel #4
0
 def test_with_return_multi_line_type(self):
     line = '\n'.join([
         'def foo(arg1) -> Tuple[',
         '    int,',
         '    int,',
         ']:',
         '    pass',
     ])
     actual = parse(line)[0]
     self.assertDictEqual(
         {
             'name': 'foo',
             'params': [
                 {
                     'argument': 'arg1',
                     'annotation': None,
                     'default': None,
                 },
             ],
             'return_type': 'Tuple[\n    int,\n    int,\n]',
             'start_lineno': 1,
             'start_col': 0,
             'end_lineno': 5,
             'end_col': 8,
             'is_doc_exists': False,
             'exceptions': [],
             'yields': [],
         },
         actual,
     )
Beispiel #5
0
    def test_doc_exists(self):
        line = '\n'.join([
            'def foo(arg1):',
            '   """foo.'
            '',
            '   :param arg1',
            '   """',
            '   pass',
        ])

        actual = parse(line)[0]
        self.assertDictEqual(
            {
                'name': 'foo',
                'params': [{
                    'argument': 'arg1',
                    'annotation': None,
                    'default': None,
                }],
                'return_type': None,
                'start_lineno': 1,
                'start_col': 0,
                'end_lineno': 5,
                'end_col': 7,
                'is_doc_exists': True,
                'exceptions': [],
            },
            actual,
        )
Beispiel #6
0
    def test_with_defs(self):
        line = '\n'.join([
            'def bar(arg1) -> List[str]:',
            '    pass',
            '',
            '',
            'def foo(arg1, arg2):',
            '    pass',
        ])

        actual = parse(line)
        self.assertDictEqual(
            {
                'name': 'bar',
                'params': [
                    {
                        'argument': 'arg1',
                        'annotation': None,
                        'default': None,
                    },
                ],
                'return_type': 'List[str]',
                'start_lineno': 1,
                'start_col': 0,
                'end_lineno': 3,
                'end_col': 0,
                'is_doc_exists': False,
                'exceptions': [],
                'yields': [],
            },
            actual[0],
        )

        self.assertDictEqual(
            {
                'name': 'foo',
                'params': [
                    {
                        'argument': 'arg1',
                        'annotation': None,
                        'default': None,
                    },
                    {
                        'argument': 'arg2',
                        'annotation': None,
                        'default': None,
                    },
                ],
                'return_type': None,
                'start_lineno': 5,
                'start_col': 0,
                'end_lineno': 6,
                'end_col': 8,
                'is_doc_exists': False,
                'exceptions': [],
                'yields': [],
            },
            actual[1],
        )
Beispiel #7
0
 def test_with_two_arguments_with_default(self):
     fixtures = [
         {
             'given': """def foo(arg1, arg2=None): pass""",
             'expected': {
                 'name': 'foo',
                 'params': [
                     {
                         'argument': 'arg1',
                         'annotation': None,
                         'default': None,
                     }, {
                         'argument': 'arg2',
                         'annotation': None,
                         'default': 'None',
                     },
                 ],
                 'return_type': None,
                 'start_lineno': 1,
                 'start_col': 0,
                 'end_lineno': 1,
                 'end_col': 30,
                 'is_doc_exists': False,
                 'exceptions': [],
                 'yields': [],
             },
         },
         {
             'given': """def foo(arg1='foo', arg2=None): pass""",
             'expected': {
                 'name': 'foo',
                 'params': [
                     {
                         'argument': 'arg1',
                         'annotation': None,
                         'default': '\'foo\'',
                     }, {
                         'argument': 'arg2',
                         'annotation': None,
                         'default': 'None',
                     },
                 ],
                 'return_type': None,
                 'return_type': None,
                 'start_lineno': 1,
                 'start_col': 0,
                 'end_lineno': 1,
                 'end_col': 36,
                 'is_doc_exists': False,
                 'exceptions': [],
                 'yields': [],
             },
         },
     ]
     for fixture in fixtures:
         actual = parse(fixture['given'])[0]
         self.assertDictEqual(fixture['expected'], actual)
Beispiel #8
0
 def test_with_two_arguments_with_annotation(self):
     fixtures = [
         {
             'given': """def foo(arg1, arg2: str): pass""",
             'expected': {
                 'name': 'foo',
                 'params': [
                     {
                         'argument': 'arg1',
                         'annotation': None,
                         'default': None,
                     }, {
                         'argument': 'arg2',
                         'annotation': 'str',
                         'default': None,
                     },
                 ],
                 'return_type': None,
                 'start_lineno': 1,
                 'start_col': 0,
                 'end_lineno': 1,
                 'end_col': 30,
                 'is_doc_exists': False,
                 'exceptions': [],
                 'yields': [],
             },
         },
         {
             'given': """def foo(arg1: str, arg2: Callable[List[init]]): pass""",
             'expected': {
                 'name': 'foo',
                 'params': [
                     {
                         'argument': 'arg1',
                         'annotation': 'str',
                         'default': None,
                     }, {
                         'argument': 'arg2',
                         'annotation': 'Callable[List[init]]',
                         'default': None,
                     },
                 ],
                 'return_type': None,
                 'start_lineno': 1,
                 'start_col': 0,
                 'end_lineno': 1,
                 'end_col': 52,
                 'is_doc_exists': False,
                 'exceptions': [],
                 'yields': [],
             },
         },
     ]
     for fixture in fixtures:
         actual = parse(fixture['given'])[0]
         self.assertDictEqual(fixture['expected'], actual)
Beispiel #9
0
def generate_docstrings(code, path, omissions=None, ignore_exception=False):
    template = Template(paths=[path])
    signatures = parse('\n'.join(code), omissions=omissions)
    is_exception = False if ignore_exception else is_exception_enabled(
        os.path.join(path, 'def.txt'))

    docstrings = []
    for signature in signatures:
        if 'defs' in signature:
            # Class docstring
            if signature['is_doc_exists'] is False:
                docstring = template.load(params=signature,
                                          filename='class.txt')
                docstrings.append({
                    'docstring': docstring,
                    'start_lineno': signature['start_lineno'],
                    'start_col': signature['start_col'],
                    'end_lineno': signature['end_lineno'],
                    'end_col': signature['end_col'],
                })

            # Method docstring
            docstrings += generate_def_docstrings(signature, template,
                                                  is_exception)
        else:
            if signature['is_doc_exists'] is False:
                filename = 'noarg.txt'
                if len(signature['params']) \
                        or signature['return_type'] \
                        or is_exception and signature['exceptions']:
                    filename = 'def.txt'

                # Function docstring
                docstring = template.load(params=signature, filename=filename)
                docstrings.append({
                    'docstring': docstring,
                    'start_lineno': signature['start_lineno'],
                    'start_col': signature['start_col'],
                    'end_lineno': signature['end_lineno'],
                    'end_col': signature['start_col'],
                })

    docstrings.sort(key=_sort)

    return docstrings
Beispiel #10
0
    def test_without_argument(self):
        line = """def foo(): pass"""

        actual = parse(line)[0]
        self.assertDictEqual(
            {
                'name': 'foo',
                'params': [],
                'return_type': None,
                'start_lineno': 1,
                'start_col': 0,
                'end_lineno': 1,
                'end_col': len(line),
                'is_doc_exists': False,
                'exceptions': [],
            },
            actual,
        )
Beispiel #11
0
 def test_with_one_argument_annotaion_and_default(self):
     line = """def foo(arg1: str='bar'): pass"""
     actual = parse(line)[0]
     self.assertDictEqual(
         {
             'name': 'foo',
             'params': [{
                 'argument': 'arg1',
                 'annotation': 'str',
                 'default': '\'bar\'',
             }],
             'return_type': None,
             'start_lineno': 1,
             'start_col': 0,
             'end_lineno': 1,
             'end_col': len(line),
             'is_doc_exists': False,
             'exceptions': [],
         },
         actual,
     )
Beispiel #12
0
 def test_with_return_type(self):
     line = """def foo(arg1) -> List[str]: pass"""
     actual = parse(line)[0]
     self.assertDictEqual(
         {
             'name': 'foo',
             'params': [
                 {
                     'argument': 'arg1',
                     'annotation': None,
                     'default': None,
                 },
             ],
             'return_type': 'List[str]',
             'start_lineno': 1,
             'start_col': 0,
             'end_lineno': 1,
             'end_col': 32,
             'is_doc_exists': False,
             'exceptions': [],
         },
         actual,
     )