Skip to content

Commit 0ffce28

Browse files
committed
parse variable hints
1 parent 2a9f128 commit 0ffce28

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

parsers/parser.py

Lines changed: 6 additions & 3 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

@@ -562,6 +562,9 @@ def parse_arguments(self, line):
562562

563563
arguments = re.search(r'^\s*def \w*\((.*)\)', line)
564564

565+
# Parse type hints
566+
hints = dict(re.findall(r'(\w*)\s*:\s*(\w*\[[^:]*\]|\w*)\s*', arguments.group(1)))
567+
565568
# Remove type hints
566569
arguments = re.sub(r':\s*(\w*\[[^:]*\]|\w*)\s*', "", arguments.group(1))
567570

@@ -576,7 +579,7 @@ def parse_arguments(self, line):
576579
continue
577580

578581
argument_type = 'keyword_arguments' if '=' in argument else 'arguments'
579-
params = self.process_variable(argument)
582+
params = self.process_variable(argument, hints)
580583
parsed_arguments[argument_type].append(params)
581584

582585
return parsed_arguments

0 commit comments

Comments
 (0)