Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Commit e9aab69

Browse files
committed
#129 - Parse function parameters
1 parent 557e072 commit e9aab69

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/pydocstyle.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from re import compile as re
1919
import itertools
2020
from collections import defaultdict, namedtuple, Set
21+
from functools import partial
2122

2223
try: # Python 3.x
2324
from ConfigParser import RawConfigParser
@@ -119,6 +120,7 @@ class Definition(Value):
119120
all = property(lambda self: self.module.all)
120121
_slice = property(lambda self: slice(self.start - 1, self.end))
121122
is_class = False
123+
is_function = False
122124

123125
def __iter__(self):
124126
return chain([self], *self.children)
@@ -161,6 +163,9 @@ class Package(Module):
161163

162164
class Function(Definition):
163165

166+
is_function = True
167+
_fields = ('name', '_source', 'start', 'end', 'decorators', 'docstring',
168+
'children', 'parent', 'parameters')
164169
_nest = staticmethod(lambda s: {'def': NestedFunction,
165170
'class': NestedClass}[s])
166171

@@ -448,6 +453,8 @@ def parse_definition(self, class_):
448453
self.stream.move()
449454
if self.current.kind == tk.OP and self.current.value == '(':
450455
parenthesis_level = 0
456+
arguments = []
457+
is_default_assignment = False
451458
while True:
452459
if self.current.kind == tk.OP:
453460
if self.current.value == '(':
@@ -456,6 +463,14 @@ def parse_definition(self, class_):
456463
parenthesis_level -= 1
457464
if parenthesis_level == 0:
458465
break
466+
elif self.current.value == '=':
467+
is_default_assignment = True
468+
elif self.current.kind == tk.NAME:
469+
if is_default_assignment:
470+
is_default_assignment = False
471+
else:
472+
arguments.append(self.current.value)
473+
459474
self.stream.move()
460475
if self.current.kind != tk.OP or self.current.value != ':':
461476
self.leapfrog(tk.OP, value=":")
@@ -477,8 +492,11 @@ def parse_definition(self, class_):
477492
children = []
478493
end = self.line
479494
self.leapfrog(tk.NEWLINE)
480-
definition = class_(name, self.source, start, end,
481-
decorators, docstring, children, None)
495+
496+
creator = partial(class_, name, self.source, start, end,
497+
decorators, docstring, children, None)
498+
499+
definition = creator(arguments) if class_.is_function else creator()
482500
for child in definition.children:
483501
child.parent = definition
484502
log.debug("finished parsing %s '%s'. Next token is %r (%s)",

src/tests/test_definitions.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class class_(object):
2424
"""Class."""
2525
def method_1(self):
2626
"""Method."""
27-
def method_2(self):
27+
def method_2(self,
28+
param=None):
2829
def nested_3(self):
2930
"""Nested."""
3031
'''
@@ -142,25 +143,26 @@ def test_parser():
142143

143144
function, class_ = module.children
144145
assert Function('function', _, _, _, _, '"Function."', _,
145-
module) == function
146+
module, []) == function
146147
assert Class('class_', _, _, _, _, '"""Class."""', _, module) == class_
147148

148149
nested_1, nested_2 = function.children
149150
assert NestedFunction('nested_1', _, _, _, _,
150-
'"""Nested."""', _, function) == nested_1
151+
'"""Nested."""', _, function, []) == nested_1
151152
assert NestedFunction('nested_2', _, _, _, _, None, _,
152-
function) == nested_2
153+
function, []) == nested_2
153154
assert nested_1.is_public is False
154155

155156
method_1, method_2 = class_.children
156157
assert method_1.parent == method_2.parent == class_
157158
assert Method('method_1', _, _, _, _, '"""Method."""', _,
158-
class_) == method_1
159-
assert Method('method_2', _, _, _, _, None, _, class_) == method_2
159+
class_, ['self']) == method_1
160+
assert Method('method_2', _, _, _, _, None, _,
161+
class_, ['self', 'param']) == method_2
160162

161163
nested_3, = method_2.children
162164
assert NestedFunction('nested_3', _, _, _, _,
163-
'"""Nested."""', _, method_2) == nested_3
165+
'"""Nested."""', _, method_2, ['self']) == nested_3
164166
assert nested_3.module == module
165167
assert nested_3.all == dunder_all
166168

0 commit comments

Comments
 (0)