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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# NOTE(morganfainberg) This file shouldn't have flake8 run on it as it has
# code examples that will fail normal CI pep8/flake8 tests. This is expected.
# The code has been moved here to ensure that proper tests occur on the
# test_hacking_checks test cases.
# flake8: noqa
import fixtures
class HackingCode(fixtures.Fixture):
"""A fixture to house the various code examples for the keystone hacking
style checks.
"""
mutable_default_args = {
'code': """
def f():
pass
def f(a, b='', c=None):
pass
def f(bad=[]):
pass
def f(foo, bad=[], more_bad=[x for x in range(3)]):
pass
def f(foo, bad={}):
pass
def f(foo, bad={}, another_bad=[], fine=None):
pass
def f(bad=[]): # noqa
pass
def funcs(bad=dict(), more_bad=list(), even_more_bad=set()):
"creating mutables through builtins"
def funcs(bad=something(), more_bad=some_object.something()):
"defaults from any functions"
def f(bad=set(), more_bad={x for x in range(3)},
even_more_bad={1, 2, 3}):
"set and set comprehession"
def f(bad={x: x for x in range(3)}):
"dict comprehension"
""",
'expected_errors': [
(7, 10, 'K001'),
(10, 15, 'K001'),
(10, 29, 'K001'),
(13, 15, 'K001'),
(16, 15, 'K001'),
(16, 31, 'K001'),
(22, 14, 'K001'),
(22, 31, 'K001'),
(22, 53, 'K001'),
(25, 14, 'K001'),
(25, 36, 'K001'),
(28, 10, 'K001'),
(28, 27, 'K001'),
(29, 21, 'K001'),
(32, 11, 'K001'),
]}
comments_begin_with_space = {
'code': """
# This is a good comment
#This is a bad one
# This is alright and can
# be continued with extra indentation
# if that's what the developer wants.
""",
'expected_errors': [
(3, 0, 'K002'),
]}
asserting_none_equality = {
'code': """
class Test(object):
def test(self):
self.assertEqual('', '')
self.assertEqual('', None)
self.assertEqual(None, '')
self.assertNotEqual('', None)
self.assertNotEqual(None, '')
self.assertNotEqual('', None) # noqa
self.assertNotEqual(None, '') # noqa
""",
'expected_errors': [
(5, 8, 'K003'),
(6, 8, 'K003'),
(7, 8, 'K004'),
(8, 8, 'K004'),
]}
assert_no_translations_for_debug_logging = {
'code': """
import logging
import logging as stlib_logging
from keystone.i18n import _
from keystone.i18n import _ as oslo_i18n
from oslo_log import log
from oslo_log import log as oslo_logging
# stdlib logging
L0 = logging.getLogger()
L0.debug(_('text'))
class C:
def __init__(self):
L0.debug(oslo_i18n('text', {}))
# stdlib logging w/ alias and specifying a logger
class C:
def __init__(self):
self.L1 = logging.getLogger(__name__)
def m(self):
self.L1.debug(
_('text'), {}
)
# oslo logging and specifying a logger
L2 = logging.getLogger(__name__)
L2.debug(oslo_i18n('text'))
# oslo logging w/ alias
class C:
def __init__(self):
self.L3 = oslo_logging.getLogger()
self.L3.debug(_('text'))
# translation on a separate line
msg = _('text')
L2.debug(msg)
# this should not fail
if True:
msg = _('message %s') % X
L2.error(msg)
raise TypeError(msg)
if True:
msg = 'message'
L2.debug(msg)
# this should not fail
if True:
if True:
msg = _('message')
else:
msg = _('message')
L2.debug(msg)
raise Exception(msg)
""",
'expected_errors': [
(10, 9, 'K005'),
(13, 17, 'K005'),
(21, 12, 'K005'),
(26, 9, 'K005'),
(32, 22, 'K005'),
(36, 9, 'K005'),
]
}
dict_constructor = {
'code': """
lower_res = {k.lower(): v for k, v in six.iteritems(res[1])}
fool = dict(a='a', b='b')
lower_res = dict((k.lower(), v) for k, v in six.iteritems(res[1]))
attrs = dict([(k, _from_json(v))])
dict([[i,i] for i in range(3)])
dict(({1:2}))
""",
'expected_errors': [
(3, 0, 'K008'),
(4, 0, 'K008'),
(5, 0, 'K008'),
]}
class HackingLogging(fixtures.Fixture):
shared_imports = """
import logging
import logging as stlib_logging
from keystone.i18n import _
from keystone.i18n import _ as oslo_i18n
from keystone.i18n import _LC
from keystone.i18n import _LE
from keystone.i18n import _LE as error_hint
from keystone.i18n import _LI
from keystone.i18n import _LW
from oslo_log import log
from oslo_log import log as oslo_logging
"""
examples = [
{
'code': """
# stdlib logging
LOG = logging.getLogger()
LOG.info(_('text'))
class C:
def __init__(self):
LOG.warn(oslo_i18n('text', {}))
LOG.warn(_LW('text', {}))
""",
'expected_errors': [
(3, 9, 'K006'),
(6, 17, 'K006'),
],
},
{
'code': """
# stdlib logging w/ alias and specifying a logger
class C:
def __init__(self):
self.L = logging.getLogger(__name__)
def m(self):
self.L.warning(
_('text'), {}
)
self.L.warning(
_LW('text'), {}
)
""",
'expected_errors': [
(7, 12, 'K006'),
],
},
{
'code': """
# oslo logging and specifying a logger
L = log.getLogger(__name__)
L.error(oslo_i18n('text'))
L.error(error_hint('text'))
""",
'expected_errors': [
(3, 8, 'K006'),
],
},
{
'code': """
# oslo logging w/ alias
class C:
def __init__(self):
self.LOG = oslo_logging.getLogger()
self.LOG.critical(_('text'))
self.LOG.critical(_LC('text'))
""",
'expected_errors': [
(5, 26, 'K006'),
],
},
{
'code': """
LOG = log.getLogger(__name__)
# translation on a separate line
msg = _('text')
LOG.exception(msg)
msg = _LE('text')
LOG.exception(msg)
""",
'expected_errors': [
(4, 14, 'K006'),
],
},
{
'code': """
LOG = logging.getLogger()
# ensure the correct helper is being used
LOG.warn(_LI('this should cause an error'))
# debug should not allow any helpers either
LOG.debug(_LI('this should cause an error'))
""",
'expected_errors': [
(4, 9, 'K006'),
(7, 10, 'K005'),
],
},
{
'code': """
# this should not be an error
L = log.getLogger(__name__)
msg = _('text')
L.warn(msg)
raise Exception(msg)
""",
'expected_errors': [],
},
{
'code': """
L = log.getLogger(__name__)
def f():
msg = _('text')
L2.warn(msg)
something = True # add an extra statement here
raise Exception(msg)
""",
'expected_errors': [],
},
{
'code': """
LOG = log.getLogger(__name__)
def func():
msg = _('text')
LOG.warn(msg)
raise Exception('some other message')
""",
'expected_errors': [
(4, 13, 'K006'),
],
},
{
'code': """
LOG = log.getLogger(__name__)
if True:
msg = _('text')
else:
msg = _('text')
LOG.warn(msg)
raise Exception(msg)
""",
'expected_errors': [
],
},
{
'code': """
LOG = log.getLogger(__name__)
if True:
msg = _('text')
else:
msg = _('text')
LOG.warn(msg)
""",
'expected_errors': [
(6, 9, 'K006'),
],
},
{
'code': """
LOG = log.getLogger(__name__)
msg = _LW('text')
LOG.warn(msg)
raise Exception(msg)
""",
'expected_errors': [
(3, 9, 'K007'),
],
},
{
'code': """
LOG = log.getLogger(__name__)
msg = _LW('text')
LOG.warn(msg)
msg = _('something else')
raise Exception(msg)
""",
'expected_errors': [],
},
{
'code': """
LOG = log.getLogger(__name__)
msg = _LW('hello %s') % 'world'
LOG.warn(msg)
raise Exception(msg)
""",
'expected_errors': [
(3, 9, 'K007'),
],
},
{
'code': """
LOG = log.getLogger(__name__)
msg = _LW('hello %s') % 'world'
LOG.warn(msg)
""",
'expected_errors': [],
},
{
'code': """
# this should not be an error
LOG = log.getLogger(__name__)
try:
something = True
except AssertionError as e:
LOG.warning(six.text_type(e))
raise exception.Unauthorized(e)
""",
'expected_errors': [],
},
]
|