26 | 26 |
import functools
|
27 | 27 |
import re
|
28 | 28 |
import tokenize
|
|
29 |
|
|
30 |
from hacking import core
|
29 | 31 |
|
30 | 32 |
re_assert_true_instance = re.compile(
|
31 | 33 |
r"(.)*assertTrue\(isinstance\((\w|\.|\'|\"|\[|\])+, "
|
|
62 | 64 |
r"\s:raise[^s] *.*$|\s:raises *:.*$|\s:raises *[^:]+$")
|
63 | 65 |
|
64 | 66 |
|
|
67 |
@core.flake8ext
|
65 | 68 |
def skip_ignored_lines(func):
|
66 | 69 |
|
67 | 70 |
@functools.wraps(func)
|
|
85 | 88 |
|
86 | 89 |
|
87 | 90 |
@skip_ignored_lines
|
|
91 |
@core.flake8ext
|
88 | 92 |
def check_assert_methods_from_mock(logical_line, filename):
|
89 | 93 |
"""Ensure that ``assert_*`` methods from ``mock`` library is used correctly
|
90 | 94 |
|
|
131 | 135 |
|
132 | 136 |
|
133 | 137 |
@skip_ignored_lines
|
|
138 |
@core.flake8ext
|
134 | 139 |
def assert_true_instance(logical_line, filename):
|
135 | 140 |
"""Check for assertTrue(isinstance(a, b)) sentences
|
136 | 141 |
|
|
142 | 147 |
|
143 | 148 |
|
144 | 149 |
@skip_ignored_lines
|
|
150 |
@core.flake8ext
|
145 | 151 |
def assert_equal_type(logical_line, filename):
|
146 | 152 |
"""Check for assertEqual(type(A), B) sentences
|
147 | 153 |
|
|
153 | 159 |
|
154 | 160 |
|
155 | 161 |
@skip_ignored_lines
|
|
162 |
@core.flake8ext
|
156 | 163 |
def assert_equal_none(logical_line, filename):
|
157 | 164 |
"""Check for assertEqual(A, None) or assertEqual(None, A) sentences
|
158 | 165 |
|
|
167 | 174 |
|
168 | 175 |
|
169 | 176 |
@skip_ignored_lines
|
|
177 |
@core.flake8ext
|
170 | 178 |
def assert_true_or_false_with_in(logical_line, filename):
|
171 | 179 |
"""Check assertTrue/False(A in/not in B) with collection contents
|
172 | 180 |
|
|
185 | 193 |
|
186 | 194 |
|
187 | 195 |
@skip_ignored_lines
|
|
196 |
@core.flake8ext
|
188 | 197 |
def assert_equal_in(logical_line, filename):
|
189 | 198 |
"""Check assertEqual(A in/not in B, True/False) with collection contents
|
190 | 199 |
|
|
203 | 212 |
|
204 | 213 |
|
205 | 214 |
@skip_ignored_lines
|
|
215 |
@core.flake8ext
|
206 | 216 |
def check_quotes(logical_line, filename):
|
207 | 217 |
"""Check that single quotation marks are not used
|
208 | 218 |
|
|
256 | 266 |
|
257 | 267 |
|
258 | 268 |
@skip_ignored_lines
|
|
269 |
@core.flake8ext
|
259 | 270 |
def check_no_constructor_data_struct(logical_line, filename):
|
260 | 271 |
"""Check that data structs (lists, dicts) are declared using literals
|
261 | 272 |
|
|
270 | 281 |
yield (0, "N351 Remove list() construct and use literal []")
|
271 | 282 |
|
272 | 283 |
|
|
284 |
@core.flake8ext
|
273 | 285 |
def check_dict_formatting_in_string(logical_line, tokens):
|
274 | 286 |
"""Check that strings do not use dict-formatting with a single replacement
|
275 | 287 |
|
|
321 | 333 |
format_keys.add(match.group(1))
|
322 | 334 |
if len(format_keys) == 1:
|
323 | 335 |
yield (0,
|
324 | |
"N353 Do not use mapping key string formatting "
|
|
336 |
"N352 Do not use mapping key string formatting "
|
325 | 337 |
"with a single key")
|
326 | 338 |
if text != ")":
|
327 | 339 |
# NOTE(stpierre): You can have a parenthesized string
|
|
338 | 350 |
|
339 | 351 |
|
340 | 352 |
@skip_ignored_lines
|
|
353 |
@core.flake8ext
|
341 | 354 |
def check_using_unicode(logical_line, filename):
|
342 | 355 |
"""Check crosspython unicode usage
|
343 | 356 |
|
|
349 | 362 |
"use 'six.text_type' instead.")
|
350 | 363 |
|
351 | 364 |
|
|
365 |
@core.flake8ext
|
352 | 366 |
def check_raises(physical_line, filename):
|
353 | 367 |
"""Check raises usage
|
354 | 368 |
|
|
361 | 375 |
if re_raises.search(physical_line):
|
362 | 376 |
return (0, "N354 ':Please use ':raises Exception: conditions' "
|
363 | 377 |
"in docstrings.")
|
364 | |
|
365 | |
|
366 | |
def factory(register):
|
367 | |
register(check_assert_methods_from_mock)
|
368 | |
register(assert_true_instance)
|
369 | |
register(assert_equal_type)
|
370 | |
register(assert_equal_none)
|
371 | |
register(assert_true_or_false_with_in)
|
372 | |
register(assert_equal_in)
|
373 | |
register(check_quotes)
|
374 | |
register(check_no_constructor_data_struct)
|
375 | |
register(check_dict_formatting_in_string)
|
376 | |
register(check_using_unicode)
|
377 | |
register(check_raises)
|