Skip to content

Commit da31d85

Browse files
committed
Expect docstring hook to provide Type instances rather than tuples of str, int.
1 parent d405634 commit da31d85

File tree

3 files changed

+6
-16
lines changed

3 files changed

+6
-16
lines changed

mypy/fastparse.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,10 @@ def parse_docstring(hook: DocstringParserHook, docstring: str, arg_names: List[s
122122
123123
Returns a 2-tuple: (list of arguments Types, and return Type).
124124
"""
125-
126-
def pop_and_convert(name: str) -> Optional[Type]:
127-
t = type_map.pop(name, None)
128-
if t is None:
129-
return AnyType()
130-
else:
131-
return parse_type_comment(t[0], t[1], errors)
132-
133125
type_map = hook(docstring, line, errors)
134126
if type_map:
135-
arg_types = [pop_and_convert(name) for name in arg_names]
136-
return_type = pop_and_convert('return')
127+
arg_types = [type_map.pop(name, AnyType()) for name in arg_names]
128+
return_type = type_map.pop('return', AnyType())
137129
if type_map:
138130
errors.report(line, 0,
139131
TYPE_COMMENT_DOCSTRING_ERROR.format(list(type_map), arg_names))

mypy/plugin.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@
7676
],
7777
Dict[
7878
str, # Argument name, or 'return' for return type.
79-
Tuple[
80-
str, # PEP484-compatible string to use as the argument's type
81-
int # Line number identifying the location of the type annotation
82-
]
79+
Type # Corresponding type extracted from docstring
8380
]
8481
]
8582

test-data/unit/plugins/docstring.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from mypy.plugin import Plugin
2+
from mypy.fastparse import parse_type_comment
23

34
class MyPlugin(Plugin):
45
def get_docstring_parser_hook(self):
56
return my_hook
67

78
def my_hook(docstring, line, errs):
89
params = [l.split(':', 1) for l in docstring.strip().split('\n')]
9-
return {k.strip(): (v.strip(), line + i + 1) for i, (k, v) in enumerate(params)}
10+
return {k.strip(): parse_type_comment(v.strip(), line + i + 1, errs)
11+
for i, (k, v) in enumerate(params)}
1012

1113
def plugin(version):
1214
return MyPlugin
13-

0 commit comments

Comments
 (0)