@@ -11,9 +11,9 @@ f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
11
11
[[mypy]
12
12
hooks.docstring_parser = tmp/myhooks.py:parse_docstring
13
13
[file myhooks.py]
14
- def parse_docstring(docstring, opts, errs):
14
+ def parse_docstring(docstring, line, opts, errs):
15
15
params = [l.split(':', 1) for l in docstring.strip().split('\n')]
16
- return {k.strip(): (v.strip(), i + 1) for i, (k, v) in enumerate(params)}
16
+ return {k.strip(): (v.strip(), line + i + 1) for i, (k, v) in enumerate(params)}
17
17
18
18
[case testMethodDocstringHook]
19
19
# flags: --config-file tmp/mypy.ini
@@ -30,44 +30,65 @@ A().f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "
30
30
[[mypy]
31
31
hooks.docstring_parser = tmp/myhooks.py:parse_docstring
32
32
[file myhooks.py]
33
- def parse_docstring(docstring, opts, errs):
33
+ def parse_docstring(docstring, line, opts, errs):
34
34
params = [l.split(':', 1) for l in docstring.strip().split('\n')]
35
- return {k.strip(): (v.strip(), i + 1) for i, (k, v) in enumerate(params)}
35
+ return {k.strip(): (v.strip(), line + i + 1) for i, (k, v) in enumerate(params)}
36
36
37
37
[case testSparseDocstringAnnotations]
38
38
# flags: --config-file tmp/mypy.ini
39
39
def f(x, y):
40
- """
41
- x: int
42
- """
40
+ """trigger parsing"""
43
41
return 1
44
42
f('', 1) # E: Argument 1 to "f" has incompatible type "str"; expected "int"
45
43
[file mypy.ini]
46
44
[[mypy]
47
45
hooks.docstring_parser = tmp/myhooks.py:parse_docstring
48
46
[file myhooks.py]
49
- def parse_docstring(docstring, opts, errs):
50
- params = [l.split(':', 1) for l in docstring.strip().split('\n')]
51
- return {k.strip(): (v.strip(), i + 1) for i, (k, v) in enumerate(params)}
52
-
47
+ def parse_docstring(docstring, line, opts, errs):
48
+ return {'x': ('int', line)}
53
49
54
50
[case testInvalidDocstringAnnotation]
55
51
# flags: --config-file tmp/mypy.ini
56
52
def f(x):
57
- """
58
- x: B/A/D
59
- return: None
60
- """
53
+ """trigger parsing"""
61
54
return None
62
55
[file mypy.ini]
63
56
[[mypy]
64
57
hooks.docstring_parser = tmp/myhooks.py:parse_docstring
65
58
[file myhooks.py]
66
- def parse_docstring(docstring, opts, errs):
67
- params = [l.split(':', 1) for l in docstring.strip().split('\n')]
68
- return {k.strip(): (v.strip(), i + 1) for i, (k, v) in enumerate(params)}
59
+ def parse_docstring(docstring, line, opts, errs):
60
+ return {'x': ('B/A/D', line), 'return': ('None', line)}
61
+ [out]
62
+ main:2: error: invalid type comment or annotation
63
+
64
+ [case testDocstringHookErrors]
65
+ # flags: --config-file tmp/mypy.ini
66
+ def f(x):
67
+ """trigger parsing"""
68
+ return ''
69
+ [file mypy.ini]
70
+ [[mypy]
71
+ hooks.docstring_parser = tmp/myhooks.py:parse_docstring
72
+ [file myhooks.py]
73
+ def parse_docstring(docstring, line, opts, errs):
74
+ errs.report(line, 0, 'custom error')
75
+ return {}
76
+ [out]
77
+ main:2: error: custom error
78
+
79
+ [case testDocstringMismatchErrors]
80
+ # flags: --config-file tmp/mypy.ini
81
+ def f(x):
82
+ """trigger parsing"""
83
+ return ''
84
+ [file mypy.ini]
85
+ [[mypy]
86
+ hooks.docstring_parser = tmp/myhooks.py:parse_docstring
87
+ [file myhooks.py]
88
+ def parse_docstring(docstring, line, opts, errs):
89
+ return {'x': ('int', line), 'y': ('int', line)}
69
90
[out]
70
- main:3 : error: invalid type comment or annotation
91
+ main:2 : error: Arguments parsed from docstring are not present in function signature: ['y'] not in ['x']
71
92
72
93
-- Python 2.7
73
94
-- -------------------------
@@ -85,9 +106,9 @@ f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
85
106
[[mypy]
86
107
hooks.docstring_parser = tmp/myhooks.py:parse_docstring
87
108
[file myhooks.py]
88
- def parse_docstring(docstring, opts, errs):
109
+ def parse_docstring(docstring, line, opts, errs):
89
110
params = [l.split(':', 1) for l in docstring.strip().split('\n')]
90
- return {k.strip(): (v.strip(), i + 1) for i, (k, v) in enumerate(params)}
111
+ return {k.strip(): (v.strip(), line + i + 1) for i, (k, v) in enumerate(params)}
91
112
92
113
[case testMethodDocstringHook27]
93
114
# flags: --config-file tmp/mypy.ini --python-version 2.7
@@ -104,7 +125,7 @@ A().f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "
104
125
[[mypy]
105
126
hooks.docstring_parser = tmp/myhooks.py:parse_docstring
106
127
[file myhooks.py]
107
- def parse_docstring(docstring, opts, errs):
128
+ def parse_docstring(docstring, line, opts, errs):
108
129
params = [l.split(':', 1) for l in docstring.strip().split('\n')]
109
- return {k.strip(): (v.strip(), i + 1) for i, (k, v) in enumerate(params)}
130
+ return {k.strip(): (v.strip(), line + i + 1) for i, (k, v) in enumerate(params)}
110
131
0 commit comments