Skip to content

Commit 4dc8225

Browse files
committed
Address docparser review notes.
- simplify and improve registration of mypy.hooks - do not call cleandoc (that's up to the hook user) - doc parser hooks must return strings and not Type
1 parent 7b9c28a commit 4dc8225

File tree

3 files changed

+18
-38
lines changed

3 files changed

+18
-38
lines changed

mypy/fastparse.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from functools import wraps
22
import sys
3-
import inspect
43

54
from typing import Tuple, Union, TypeVar, Callable, Sequence, Optional, Any, cast, List
65
from mypy.nodes import (
@@ -100,14 +99,11 @@ def pop_and_convert(name):
10099
t = type_map.pop(name, None)
101100
if t is None:
102101
return AnyType()
103-
elif isinstance(t, Type):
104-
return t
105102
else:
106103
return parse_str_as_type(t, line)
107104

108-
docstring_parser = hooks.get_docstring_parser()
109-
if docstring_parser is not None:
110-
type_map = docstring_parser(inspect.cleandoc(docstring), line)
105+
if hooks.docstring_parser is not None:
106+
type_map = hooks.docstring_parser(docstring)
111107
if type_map:
112108
arg_types = [pop_and_convert(name) for name in arg_names]
113109
return_type = pop_and_convert('return')
@@ -323,10 +319,9 @@ def do_func_def(self, n: Union[ast35.FunctionDef, ast35.AsyncFunctionDef],
323319
return_type = TypeConverter(line=n.lineno).visit(n.returns)
324320
# hooks
325321
if (not any(arg_types) and return_type is None and
326-
hooks.get_docstring_parser()):
322+
hooks.docstring_parser):
327323
doc = ast35.get_docstring(n, clean=False)
328324
if doc:
329-
doc = doc.decode('unicode_escape')
330325
types = parse_docstring(doc, arg_names, n.lineno)
331326
if types is not None:
332327
arg_types, return_type = types

mypy/fastparse2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ def visit_FunctionDef(self, n: ast27.FunctionDef) -> Statement:
298298
return_type = converter.visit(None)
299299
# hooks
300300
if (not any(arg_types) and return_type is None and
301-
hooks.get_docstring_parser()):
302-
doc = ast27.get_docstring(n, clean=False)
301+
hooks.docstring_parser):
302+
doc = cast(bytes, ast27.get_docstring(n, clean=False))
303303
if doc:
304304
doc = doc.decode('unicode_escape')
305305
types = parse_docstring(doc, arg_names, n.lineno)

mypy/hooks.py

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
1-
from typing import Dict, Optional, Callable, Union
2-
from mypy.types import Type
3-
4-
hooks = {} # type: Dict[str, Callable]
5-
6-
docstring_parser_type = Callable[[str, int], Optional[Dict[str, Union[str, Type]]]]
7-
8-
9-
def set_docstring_parser(func: docstring_parser_type) -> None:
10-
"""Enable the docstring parsing hook.
11-
12-
The callable must take a docstring for a function along with its line number
13-
(typically passed to mypy.parsetype.parse_str_as_type), and should return
14-
a mapping of argument name to type. The function's return type, if
15-
specified, is stored in the mapping with the special key 'return'.
16-
17-
The keys of the mapping must be a subset of the arguments of the function
18-
to which the docstring belongs (other than the special 'return'
19-
key); an error will be raised if the mapping contains stray arguments.
20-
21-
The values of the mapping must be either mypy.types.Type or a valid
22-
PEP484-compatible string which can be converted to a Type.
23-
"""
24-
hooks['docstring_parser'] = func
25-
26-
27-
def get_docstring_parser() -> Optional[docstring_parser_type]:
28-
return hooks.get('docstring_parser')
1+
from typing import Dict, Optional, Callable
2+
3+
# The docstring_parser hook is called for each function that has a docstring
4+
# and no other type annotations applied, and the callable should accept the
5+
# docstring as an argument and return a mapping of argument name to type.
6+
#
7+
# The function's return type, if specified, is stored in the mapping with the
8+
# special key 'return'. Other than 'return', the keys of the mapping must be
9+
# a subset of the arguments of the function to which the docstring belongs; an
10+
# error will be raised if the mapping contains stray arguments.
11+
#
12+
# The values of the mapping must be valid PEP484-compatible strings.
13+
docstring_parser = None # type: Callable[[str], Optional[Dict[str, str]]]

0 commit comments

Comments
 (0)