-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·204 lines (184 loc) · 9.85 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
"""Unit tests."""
import os
import subprocess
import tempfile
import unittest
from typing import List
def flake8(test: str, options: List[str] = None) -> List[str]:
"""Run flake8 on test input and return output."""
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(test.encode('utf-8'))
# print(test)
# print(' '.join(['flake8', '--isolated', '--select=MDA', temp_file.name] + [f'--modern-annotations-{option}' for option in (options or [])]))
process = subprocess.Popen(['flake8', '--isolated', '--select=MDA', temp_file.name] + [f'--modern-annotations-{option}' for option in (options or [])],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
os.remove(temp_file.name)
if (stderr):
print(stderr.decode('utf-8'))
return [f'0:0:{line}' for line in stderr.decode('utf-8').splitlines()]
# print(repr([line.split(':', 1)[1] for line in stdout.decode('utf-8').splitlines()]))
return [line.split(':', 1)[1] for line in stdout.decode('utf-8').splitlines()]
class TestAnnotations(unittest.TestCase):
"""Test annotation handling."""
def test_valid(self) -> None:
options = ['postponed=always']
self.assertEqual(flake8('x: int', options), [])
self.assertEqual(flake8('x: int = 2', options), [])
self.assertEqual(flake8('x: Dict[str, int]', options), [])
self.assertEqual(flake8('x: Dict[str, int] = {}', options), [])
self.assertEqual(flake8('x: Dict[str, List[Optional[Union[str, int]]]]', options), [])
self.assertEqual(flake8('x: Dict[str, List[Optional[Union[str, int]]]] = {}', options), [])
self.assertEqual(flake8('def func(x: int) -> None:\n pass', options), [])
self.assertEqual(flake8('def func(x: int = None) -> str:\n pass', options), [])
def test_quoted(self) -> None:
options = ['postponed=always']
self.assertEqual(flake8("x: 'int'", options), [
"1:4: MDA001 Remove quotes from variable type annotation 'int'",
])
self.assertEqual(flake8("x: 'int' = 2", options), [
"1:4: MDA001 Remove quotes from variable type annotation 'int'",
])
self.assertEqual(flake8("x: Dict[str, 'int']", options), [
"1:14: MDA001 Remove quotes from variable type annotation 'int'",
])
self.assertEqual(flake8("x: Dict[str, 'int'] = {}", options), [
"1:14: MDA001 Remove quotes from variable type annotation 'int'",
])
self.assertEqual(flake8("x: Dict[str, List[Optional[Union[str, 'int']]]]", options), [
"1:39: MDA001 Remove quotes from variable type annotation 'int'",
])
self.assertEqual(flake8("x: Dict[str, List[Optional[Union[str, 'int']]]] = {}", options), [
"1:39: MDA001 Remove quotes from variable type annotation 'int'",
])
self.assertEqual(flake8("def func(x: 'int') -> None:\n pass", options), [
"1:13: MDA002 Remove quotes from argument type annotation 'int'",
])
self.assertEqual(flake8("def func(x: int = None) -> 'None':\n pass", options), [
"1:28: MDA003 Remove quotes from return type annotation 'None'",
])
self.assertEqual(flake8("def func(x: 'int' = None) -> 'None':\n pass", options), [
"1:13: MDA002 Remove quotes from argument type annotation 'int'",
"1:30: MDA003 Remove quotes from return type annotation 'None'",
])
def test_typing_literal(self) -> None:
options = ['postponed=always']
self.assertEqual(flake8("import typing\ndef func() -> typing.Literal[False]:\n pass", options), [
])
self.assertEqual(flake8("import typing as typ\ndef func() -> typ.Literal[False]:\n pass", options), [
])
self.assertEqual(flake8("from typing import Literal\ndef func() -> Literal['False']:\n pass", options), [
])
self.assertEqual(flake8("from typing import Literal as Lit\ndef func() -> Lit[False]:\n pass", options), [
])
def test_typing_extensions_literal(self) -> None:
options = ['postponed=always']
self.assertEqual(flake8("import typing_extensions\ndef func() -> typing_extensions.Literal[False]:\n pass", options), [
])
self.assertEqual(flake8("import typing_extensions as typ\ndef func() -> typ.Literal[False]:\n pass", options), [
])
self.assertEqual(flake8("from typing_extensions import Literal\ndef func() -> Literal['False']:\n pass", options), [
])
self.assertEqual(flake8("from typing_extensions import Literal as Lit\ndef func() -> Lit[False]:\n pass", options), [
])
def test_callable(self) -> None:
options = ['postponed=always', 'deprecated=never']
self.assertEqual(flake8("from typing import Callable\ndef func(x: Callable[..., None]) -> None:\n pass", options), [
])
def test_deprecated(self) -> None:
options = ['deprecated=always']
self.assertEqual(flake8("import typing\ndef func(x: typing.Dict[str, str]) -> None:\n pass", options), [
"2:13: MDA202 Replace 'typing.Dict' with 'dict'",
])
self.assertEqual(flake8("import typing as typ\ndef func(x: typ.Dict[str, str]) -> None:\n pass", options), [
"2:13: MDA202 Replace 'typ.Dict' with 'dict'",
])
self.assertEqual(flake8("from typing import Dict\ndef func(x: Dict[str, str]) -> None:\n pass", options), [
"1:1: MDA102 'typing.Dict' is deprecated, remove from import",
"2:13: MDA202 Replace 'Dict' with 'dict'",
])
self.assertEqual(flake8("from typing import Dict as TDict\ndef func(x: TDict[str, str]) -> None:\n pass", options), [
"1:1: MDA102 'typing.Dict' is deprecated, remove from import",
"2:13: MDA202 Replace 'TDict' with 'dict'",
])
self.assertEqual(flake8("import typing\ndef func(x: typing.Mapping[str, str]) -> typing.Mapping:\n y: typing.Dict[str, str] = dict(x)\n return y", options), [
"2:13: MDA234 Replace 'typing.Mapping' with 'collections.abc.Mapping'",
"2:42: MDA234 Replace 'typing.Mapping' with 'collections.abc.Mapping'",
"3:8: MDA202 Replace 'typing.Dict' with 'dict'",
])
self.assertEqual(flake8("from typing import Dict, Mapping\ndef func(x: Mapping[str, str]) -> Mapping:\n y: Dict[str, str] = dict(x)\n return y", options), [
"1:1: MDA102 'typing.Dict' is deprecated, remove from import",
"1:1: MDA134 'typing.Mapping' is deprecated, replace with 'collections.abc.Mapping'",
"2:13: MDA234 Replace 'Mapping' with 'collections.abc.Mapping'",
"2:35: MDA234 Replace 'Mapping' with 'collections.abc.Mapping'",
"3:8: MDA202 Replace 'Dict' with 'dict'",
])
self.assertEqual(flake8("from typing import Dict as TDict\nx: TDict[str, str] = {}", options), [
"1:1: MDA102 'typing.Dict' is deprecated, remove from import",
"2:4: MDA202 Replace 'TDict' with 'dict'",
])
def test_allowed_type_alias(self) -> None:
options = ['deprecated=always', 'type-alias=always']
self.assertEqual(flake8("import typing\nMyDict = typing.Dict[str, typing.List[str]]", options), [
])
self.assertEqual(flake8("import typing as typ\nMyDict = typ.Dict[str, typ.List[str]]", options), [
])
self.assertEqual(flake8("from typing import Dict, List\nMyDict = Dict[str, List[str]]", options), [
])
self.assertEqual(flake8("from typing import Dict as TDict, List as TList\nMyDict = TDict[str, TList[str]]", options), [
])
def test_required_type_alias(self) -> None:
options = ['deprecated=always', 'type-alias=always']
self.assertEqual(flake8("MyDict = dict[str, list[str]]", options), [
"1:10: MDA302 Replace 'dict' with 'typing.Dict' for type alias",
"1:20: MDA301 Replace 'list' with 'typing.List' for type alias",
])
self.assertEqual(flake8("from collections.abc import Mapping, Sequence\nMyDict = Mapping[str, Sequence[str]]", options), [
"2:10: MDA334 Replace 'Mapping' with 'typing.Mapping' for type alias",
"2:23: MDA336 Replace 'Sequence' with 'typing.Sequence' for type alias",
])
self.assertEqual(flake8("from re import Match as ReMatch, Pattern as RePattern\nMyMatch = ReMatch[RePattern[str]]", options), [
"2:11: MDA361 Replace 'ReMatch' with 'typing.Match' for type alias",
"2:19: MDA360 Replace 'RePattern' with 'typing.Pattern' for type alias",
])
def test_no_type_alias(self) -> None:
options = ['deprecated=always', 'type-alias=never']
self.assertEqual(flake8("import typing\nMyDict = typing.Dict[str, typing.List]", options), [
"2:10: MDA202 Replace 'typing.Dict' with 'dict'",
"2:27: MDA201 Replace 'typing.List' with 'list'",
])
self.assertEqual(flake8("import typing as typ\nMyDict = typ.Dict[str, typ.List]", options), [
"2:10: MDA202 Replace 'typ.Dict' with 'dict'",
"2:24: MDA201 Replace 'typ.List' with 'list'",
])
self.assertEqual(flake8("from typing import Dict, List\nMyDict = Dict[str, List]", options), [
"1:1: MDA102 'typing.Dict' is deprecated, remove from import",
"1:1: MDA101 'typing.List' is deprecated, remove from import",
"2:10: MDA202 Replace 'Dict' with 'dict'",
"2:20: MDA201 Replace 'List' with 'list'",
])
self.assertEqual(flake8("from typing import Dict as TDict, List as TList\nMyDict = TDict[str, TList]", options), [
"1:1: MDA102 'typing.Dict' is deprecated, remove from import",
"1:1: MDA101 'typing.List' is deprecated, remove from import",
"2:10: MDA202 Replace 'TDict' with 'dict'",
"2:21: MDA201 Replace 'TList' with 'list'",
])
class TestOptions(unittest.TestCase):
"""Test options."""
def test_postponed(self) -> None:
self.assertEqual(flake8("x: 'int'", ['postponed=auto']), [])
self.assertEqual(flake8("from __future__ import annotations\nx: 'int'", ['postponed=auto']), [
"2:4: MDA001 Remove quotes from variable type annotation 'int'",
])
self.assertEqual(flake8("x: 'int'", ['postponed=always']), [
"1:4: MDA001 Remove quotes from variable type annotation 'int'",
])
self.assertEqual(flake8("x: 'int'", ['postponed=never']), [])
self.assertEqual(flake8("from __future__ import annotations\nx: 'int'", ['postponed=never']), [])
def test_include_name(self) -> None:
self.assertEqual(flake8("from __future__ import annotations\nx: 'int'", ['include-name']), [
"2:4: MDA001 (flake8-modern-annotations) Remove quotes from variable type annotation 'int'",
])
if __name__ == '__main__':
unittest.main()