Skip to content

Commit 434e8f1

Browse files
authored
Merge pull request #36 from EdsterG/bugfix/ignore_type_hints
type hints no longer break parser
2 parents 92c79b3 + bb9ff37 commit 434e8f1

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

parsers/parser.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ def parse(self, line, contents):
383383

384384
return {}
385385

386-
def process_variable(self, variable):
386+
def process_variable(self, variable, hints={}):
387387
"""Process an individual variable.
388388
389389
Determines programmatically what the assumed type of the variable is,
@@ -407,7 +407,7 @@ def process_variable(self, variable):
407407
params['default'] = pieces[1].strip()
408408

409409
params['name'] = variable
410-
params['type'] = guess_type_from_value(params.get('default')) or guess_type_from_name(variable)
410+
params['type'] = hints.get(variable, None) or guess_type_from_value(params.get('default')) or guess_type_from_name(variable)
411411

412412
return params
413413

@@ -560,20 +560,26 @@ def parse_arguments(self, line):
560560
'keyword_arguments': [],
561561
}
562562

563-
arguments = re.search(r'^\s*def \w*\((.*)\):\s*$', line)
563+
arguments = re.search(r'^\s*def\s+\w+\((.*)\)', line)
564+
565+
# Parse type hints
566+
hints = dict(re.findall(r'(\w+)\s*:\s*([\w\.]+\[[^:]*\]|[\w\.]+)\s*', arguments.group(1)))
567+
568+
# Remove type hints
569+
arguments = re.sub(r':\s*([\w\.]+\[[^:]*\]|[\w\.]+)\s*', "", arguments.group(1))
564570

565571
if not arguments:
566572
return None
567573

568574
excluded_parameters = ['self', 'cls']
569-
arguments = split_by_commas(arguments.group(1))
575+
arguments = split_by_commas(arguments)
570576

571577
for index, argument in enumerate(arguments):
572578
if index == 0 and argument in excluded_parameters:
573579
continue
574580

575581
argument_type = 'keyword_arguments' if '=' in argument else 'arguments'
576-
params = self.process_variable(argument)
582+
params = self.process_variable(argument, hints)
577583
parsed_arguments[argument_type].append(params)
578584

579585
return parsed_arguments
@@ -596,9 +602,13 @@ def parse_returns(self, contents):
596602
if len(match) == 0:
597603
return None
598604

605+
hint = re.search(r'^\s*def\s+\w+\(.*\)\s*->\s*([\w\.]+\[[^:]*\]|[\w\.]+)\s*:', contents)
606+
if hint:
607+
hint = hint.group(1)
608+
599609
match = match[0]
600610
return_type = match[0] + 's'
601-
return_value_type = guess_type_from_value(match[1])
611+
return_value_type = hint or guess_type_from_value(match[1])
602612

603613
return (return_type, {'type': return_value_type})
604614

0 commit comments

Comments
 (0)