Skip to content

Commit 37e6685

Browse files
committed
Add docstring hook tests
1 parent 589cf8f commit 37e6685

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

mypy/test/testcheck.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
'check-classvar.test',
7777
'check-enum.test',
7878
'check-incomplete-fixture.test',
79+
'check-docstring-hook.test',
7980
]
8081

8182

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
[case testFunctionDocstringHook]
2+
# flags: --config-file tmp/mypy.ini
3+
def f(x):
4+
"""
5+
x: int
6+
return: str
7+
"""
8+
return 1 # E: Incompatible return value type (got "int", expected "str")
9+
f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
10+
[file mypy.ini]
11+
[[mypy]
12+
hooks.docstring_parser = tmp/myhooks.py:parse_docstring
13+
[file myhooks.py]
14+
def parse_docstring(docstring, opts, errs):
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)}
17+
18+
[case testMethodDocstringHook]
19+
# flags: --config-file tmp/mypy.ini
20+
class A:
21+
def f(self, x):
22+
"""
23+
x: int
24+
return: str
25+
"""
26+
self.f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
27+
return 1 # E: Incompatible return value type (got "int", expected "str")
28+
A().f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
29+
[file mypy.ini]
30+
[[mypy]
31+
hooks.docstring_parser = tmp/myhooks.py:parse_docstring
32+
[file myhooks.py]
33+
def parse_docstring(docstring, opts, errs):
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)}
36+
37+
[case testSparseDocstringAnnotations]
38+
# flags: --config-file tmp/mypy.ini
39+
def f(x, y):
40+
"""
41+
x: int
42+
"""
43+
return 1
44+
f('', 1) # E: Argument 1 to "f" has incompatible type "str"; expected "int"
45+
[file mypy.ini]
46+
[[mypy]
47+
hooks.docstring_parser = tmp/myhooks.py:parse_docstring
48+
[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+
53+
54+
[case testInvalidDocstringAnnotation]
55+
# flags: --config-file tmp/mypy.ini
56+
def f(x):
57+
"""
58+
x: B/A/D
59+
return: None
60+
"""
61+
return None
62+
[file mypy.ini]
63+
[[mypy]
64+
hooks.docstring_parser = tmp/myhooks.py:parse_docstring
65+
[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)}
69+
[out]
70+
main:3: error: invalid type comment or annotation
71+
72+
-- Python 2.7
73+
-- -------------------------
74+
75+
[case testFunctionDocstringHook27]
76+
# flags: --config-file tmp/mypy.ini --python-version 2.7
77+
def f(x):
78+
"""
79+
x: int
80+
return: str
81+
"""
82+
return 1 # E: Incompatible return value type (got "int", expected "str")
83+
f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
84+
[file mypy.ini]
85+
[[mypy]
86+
hooks.docstring_parser = tmp/myhooks.py:parse_docstring
87+
[file myhooks.py]
88+
def parse_docstring(docstring, opts, errs):
89+
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)}
91+
92+
[case testMethodDocstringHook27]
93+
# flags: --config-file tmp/mypy.ini --python-version 2.7
94+
class A:
95+
def f(self, x):
96+
"""
97+
x: int
98+
return: str
99+
"""
100+
self.f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
101+
return 1 # E: Incompatible return value type (got "int", expected "str")
102+
A().f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
103+
[file mypy.ini]
104+
[[mypy]
105+
hooks.docstring_parser = tmp/myhooks.py:parse_docstring
106+
[file myhooks.py]
107+
def parse_docstring(docstring, opts, errs):
108+
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)}
110+

0 commit comments

Comments
 (0)