/
percent_format_test.py
279 lines (260 loc) · 7.42 KB
/
percent_format_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
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
import ast
import pytest
from pyupgrade import _fix_percent_format
from pyupgrade import _percent_to_format
from pyupgrade import _simplify_conversion_flag
from pyupgrade import parse_percent_format
@pytest.mark.parametrize(
('s', 'expected'),
(
(
'""', (
('""', None),
),
),
(
'"%%"', (
('"', (None, None, None, None, '%')),
('"', None),
),
),
(
'"%s"', (
('"', (None, None, None, None, 's')),
('"', None),
),
),
(
'"%s two! %s"', (
('"', (None, None, None, None, 's')),
(' two! ', (None, None, None, None, 's')),
('"', None),
),
),
(
'"%(hi)s"', (
('"', ('hi', None, None, None, 's')),
('"', None),
),
),
(
'"%()s"', (
('"', ('', None, None, None, 's')),
('"', None),
),
),
(
'"%#o"', (
('"', (None, '#', None, None, 'o')),
('"', None),
),
),
(
'"% #0-+d"', (
('"', (None, ' #0-+', None, None, 'd')),
('"', None),
),
),
(
'"%5d"', (
('"', (None, None, '5', None, 'd')),
('"', None),
),
),
(
'"%*d"', (
('"', (None, None, '*', None, 'd')),
('"', None),
),
),
(
'"%.f"', (
('"', (None, None, None, '.', 'f')),
('"', None),
),
),
(
'"%.5f"', (
('"', (None, None, None, '.5', 'f')),
('"', None),
),
),
(
'"%.*f"', (
('"', (None, None, None, '.*', 'f')),
('"', None),
),
),
(
'"%ld"', (
('"', (None, None, None, None, 'd')),
('"', None),
),
),
(
'"%(complete)#4.4f"', (
('"', ('complete', '#', '4', '.4', 'f')),
('"', None),
),
),
),
)
def test_parse_percent_format(s, expected):
assert parse_percent_format(s) == expected
@pytest.mark.parametrize(
('s', 'expected'),
(
('%s', '{}'),
('%%%s', '%{}'),
('%(foo)s', '{foo}'),
('%2f', '{:2f}'),
('%r', '{!r}'),
('%a', '{!a}'),
),
)
def test_percent_to_format(s, expected):
assert _percent_to_format(s) == expected
@pytest.mark.parametrize(
('s', 'expected'),
(
('', ''),
(' ', ' '),
(' ', ' '),
('#0- +', '#<+'),
('-', '<'),
),
)
def test_simplify_conversion_flag(s, expected):
assert _simplify_conversion_flag(s) == expected
@pytest.mark.parametrize(
's',
(
# cannot determine whether `unknown_type` is tuple or not
'"%s" % unknown_type',
# format of bytestring cannot be changed to `.format(...)`
'b"%s" % (b"bytestring",)',
# out-of-order parameter consumption
'"%*s" % (5, "hi")', '"%.*s" % (5, "hi")',
# potential conversion to int required
'"%d" % (flt,)', '"%i" % (flt,)', '"%u" % (flt,)',
# potential conversion to character required
'"%c" % (some_string,)',
# different output vs .format() in python 2
'"%#o" % (123,)',
# no format equivalent
'"%()s" % {"": "empty"}',
# different output in python2 / python 3
'"%4%" % ()',
# no equivalent in format specifier
'"%.2r" % (1.25)', '"%.2a" % (1.25)',
# non-string mod
'i % 3',
# dict format but not keyed arguments
'"%s" % {"k": "v"}',
# dict format must have valid identifiers
'"%()s" % {"": "bar"}',
'"%(1)s" % {"1": "bar"}',
# don't trigger `SyntaxError: keyword argument repeated`
'"%(a)s" % {"a": 1, "a": 2}',
# don't rewrite string-joins in dict literal
'"%(ab)s" % {"a" "b": 1}',
# don't rewrite strangely styled things
'"%(a)s" % {"a" : 1}',
# don't rewrite non-str keys
'"%(1)s" % {1: 2, "1": 2}',
# don't rewrite keyword keys
'"%(and)s" % {"and": 2}',
# invalid string formats
'"%" % {}', '"%(hi)" % {}', '"%2" % {}',
),
)
def test_percent_format_noop(s):
assert _fix_percent_format(s) == s
def _has_16806_bug():
# See https://bugs.python.org/issue16806
body = ast.parse('"""\n"""').body[0]
assert isinstance(body, ast.Expr)
return body.value.col_offset == -1
@pytest.mark.xfail(not _has_16806_bug(), reason='multiline string parse bug')
def test_percent_format_noop_if_bug_16806():
s = '"""%s\n""" % ("issue16806",)'
assert _fix_percent_format(s) == s
@pytest.mark.parametrize(
('s', 'expected'),
(
# tuple
('"trivial" % ()', '"trivial".format()'),
('"%s" % ("simple",)', '"{}".format("simple")'),
('"%s" % ("%s" % ("nested",),)', '"{}".format("{}".format("nested"))'),
('"%s%% percent" % (15,)', '"{}% percent".format(15)'),
('"%3f" % (15,)', '"{:3f}".format(15)'),
('"%-5s" % ("hi",)', '"{:<5}".format("hi")'),
('"%9s" % (5,)', '"{:>9}".format(5)'),
('"brace {} %s" % (1,)', '"brace {{}} {}".format(1)'),
(
'"%s" % (\n'
' "trailing comma",\n'
')\n',
'"{}".format(\n'
' "trailing comma",\n'
')\n',
),
# dict
('"%(k)s" % {"k": "v"}', '"{k}".format(k="v")'),
('"%(to_list)s" % {"to_list": []}', '"{to_list}".format(to_list=[])'),
),
)
def test_percent_format(s, expected):
assert _fix_percent_format(s) == expected
@pytest.mark.xfail
@pytest.mark.parametrize(
('s', 'expected'),
(
# currently the approach does not attempt to consider joined strings
(
'paren_continue = (\n'
' "foo %s "\n'
' "bar %s" % (x, y)\n'
')\n',
'paren_continue = (\n'
' "foo {} "\n'
' "bar {}".format(x, y)\n'
')\n',
),
(
'paren_string = (\n'
' "foo %s "\n'
' "bar %s"\n'
') % (x, y)\n',
'paren_string = (\n'
' "foo {} "\n'
' "bar {}"\n'
').format(x, y)\n',
),
(
'paren_continue = (\n'
' "foo %(foo)s "\n'
' "bar %(bar)s" % {"foo": x, "bar": y}\n'
')\n',
'paren_continue = (\n'
' "foo {foo} "\n'
' "bar {bar}".format(foo=x, bar=y)\n'
')\n',
),
(
'paren_string = (\n'
' "foo %(foo)s "\n'
' "bar %(bar)s"\n'
') % {"foo": x, "bar": y}\n',
'paren_string = (\n'
' "foo {foo} "\n'
' "bar {bar}"\n'
').format(foo=x, bar=y)\n',
),
),
)
def test_percent_format_todo(s, expected):
assert _fix_percent_format(s) == expected