|
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