/
import_replaces_test.py
306 lines (298 loc) · 9.75 KB
/
import_replaces_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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
from __future__ import annotations
import pytest
from pyupgrade._data import Settings
from pyupgrade._main import _fix_plugins
@pytest.mark.parametrize(
('s', 'min_version'),
(
pytest.param('from a import b', (3,), id='unrelated import'),
pytest.param(
'from collections import Mapping\n',
(2, 7),
id='too old min version',
),
pytest.param(
'from .xml.etree.cElementTree import XML\n',
(3,),
id='leave relative imports alone',
),
pytest.param(
'if True: from six.moves import getcwd, StringIO\n',
(3,),
id='inline from-import with space',
),
pytest.param(
'if True:from six.moves import getcwd, StringIO\n',
(3,),
id='inline from-import without space',
),
pytest.param(
'if True:import mock, sys\n',
(3,),
id='inline import-import',
),
pytest.param(
'import xml.etree.cElementTree',
(3,),
id='import without alias',
),
pytest.param(
'from xml.etree import cElementTree',
(3,),
id='from import of module without alias',
),
),
)
def test_import_replaces_noop(s, min_version):
assert _fix_plugins(s, settings=Settings(min_version=min_version)) == s
def test_mock_noop_keep_mock():
"""This would've been rewritten if keep_mock were False"""
s = (
'from mock import patch\n'
'\n'
'patch("func")'
)
settings = Settings(min_version=(3,), keep_mock=True)
assert _fix_plugins(s, settings=settings) == s
@pytest.mark.parametrize(
('s', 'min_version', 'expected'),
(
pytest.param(
'from collections import Mapping\n',
(3,),
'from collections.abc import Mapping\n',
id='one-name replacement',
),
pytest.param(
'from collections import Mapping as MAP\n',
(3,),
'from collections.abc import Mapping as MAP\n',
id='one-name replacement with alias',
),
pytest.param(
'from collections import Mapping, Sequence\n',
(3,),
'from collections.abc import Mapping, Sequence\n',
id='multi-name replacement',
),
pytest.param(
'from collections import Counter, Mapping\n',
(3,),
'from collections import Counter\n'
'from collections.abc import Mapping\n',
id='one name rewritten to new module',
),
pytest.param(
'from collections import Counter, Mapping',
(3,),
'from collections import Counter\n'
'from collections.abc import Mapping\n',
id='one name rewritten to new module, no eol',
),
pytest.param(
'from collections import (Counter, \n'
' Mapping)\n',
(3,),
'from collections import (Counter)\n'
'from collections.abc import Mapping\n',
id='one name rewritten with parens',
),
pytest.param(
'from collections import Counter, \\\n'
' Mapping\n',
(3,),
'from collections import Counter\n'
'from collections.abc import Mapping\n',
id='one name rewritten with backslash',
),
pytest.param(
'from collections import Counter, Mapping, Sequence\n',
(3,),
'from collections import Counter\n'
'from collections.abc import Mapping, Sequence\n',
id='multiple names rewritten to new module',
),
pytest.param(
'from six.moves import getcwd, StringIO\n',
(3,),
'from io import StringIO\n'
'from os import getcwd\n',
id='all imports rewritten but to multiple modules',
),
pytest.param(
'from collections import Mapping as mapping, Counter\n',
(3,),
'from collections import Counter\n'
'from collections.abc import Mapping as mapping\n',
id='new import with aliased name',
),
pytest.param(
'if True:\n'
' from xml.etree import cElementTree as ET\n',
(3,),
'if True:\n'
' from xml.etree import ElementTree as ET\n',
id='indented and full import replaced',
),
pytest.param(
'if True:\n'
' from collections import Mapping, Counter\n',
(3,),
'if True:\n'
' from collections import Counter\n'
' from collections.abc import Mapping\n',
id='indented from-import being added',
),
pytest.param(
'if True:\n'
' from six.moves import queue, urllib_request\n',
(3,),
'if True:\n'
' from six.moves import urllib_request\n'
' import queue\n',
id='indented import-import being added',
),
pytest.param(
'if True:\n'
' import mock\n',
(3,),
'if True:\n'
' from unittest import mock\n',
id='indented import-import rewritten',
),
pytest.param(
'if True:\n'
' if True:\n'
' pass\n'
' from collections import Mapping, Counter\n',
(3,),
'if True:\n'
' if True:\n'
' pass\n'
' from collections import Counter\n'
' from collections.abc import Mapping\n',
id='indented import after dedent',
),
pytest.param(
'if True: from collections import Mapping\n',
(3,),
'if True: from collections.abc import Mapping\n',
id='inline import, only one replacement',
),
pytest.param(
'import os\n'
'from collections import Counter, Mapping\n'
'import sys\n',
(3,),
'import os\n'
'from collections import Counter\n'
'from collections.abc import Mapping\n'
'import sys\n',
id='other imports left alone',
),
pytest.param(
'from six.moves import urllib_request, filter, getcwd\n',
(3,),
'from six.moves import urllib_request\n'
'from os import getcwd\n',
id='replaces and removals and one remaining',
),
pytest.param(
'from six.moves import filter, getcwd\n',
(3,),
'from os import getcwd\n',
id='replaces and removals and no remaining',
),
pytest.param(
'from six.moves.queue import Queue\n',
(3,),
'from queue import Queue\n',
id='module replacement',
),
pytest.param(
'from xml.etree.cElementTree import XML\n',
(3,),
'from xml.etree.ElementTree import XML\n',
id='relative import func',
),
pytest.param(
'from xml.etree.cElementTree import XML, Element\n',
(3,),
'from xml.etree.ElementTree import XML, Element\n',
id='import multiple objects',
),
pytest.param(
'from six.moves import queue\n',
(3,),
'import queue\n',
id='from import a module to an import-import',
),
pytest.param(
'from six.moves import queue, map, getcwd\n',
(3,),
'from os import getcwd\n'
'import queue\n',
id='removal, rename, module rename',
),
pytest.param(
'from xml.etree import cElementTree as ET\n',
(3,),
'from xml.etree import ElementTree as ET\n',
id='from import a module but aliased',
),
pytest.param(
'import xml.etree.cElementTree as ET',
(3,),
'import xml.etree.ElementTree as ET',
id='import with alias',
),
pytest.param(
'import contextlib, xml.etree.cElementTree as ET\n',
(3,),
'import contextlib, xml.etree.ElementTree as ET\n',
id='can rewrite multiple import imports',
),
pytest.param(
'import mock\n',
(3,),
'from unittest import mock\n',
id='rewrites mock import',
),
pytest.param(
'import mock.mock\n',
(3,),
'from unittest import mock\n',
id='rewrites mock.mock import',
),
pytest.param(
'import contextlib, mock, sys\n',
(3,),
'import contextlib, sys\n'
'from unittest import mock\n',
id='mock rewriting multiple imports in middle',
),
pytest.param(
'import mock, sys\n',
(3,),
'import sys\n'
'from unittest import mock\n',
id='mock rewriting multiple imports at beginning',
),
pytest.param(
'import mock, sys',
(3,),
'import sys\n'
'from unittest import mock\n',
id='adds import-import no eol',
),
pytest.param(
'from mock import mock\n',
(3,),
'from unittest import mock\n',
id='mock import mock import',
),
),
)
def test_import_replaces(s, min_version, expected):
ret = _fix_plugins(s, settings=Settings(min_version=min_version))
assert ret == expected