141
141
COMMENT_WITH_NL = tokenize .generate_tokens (['#\n ' ].pop ).send (None )[1 ] == '#\n '
142
142
143
143
144
+ _checks = {'physical_line' : {}, 'logical_line' : {}, 'tree' : {}}
145
+
146
+
147
+ def _get_parameters (function ):
148
+ if sys .version_info >= (3 , 3 ):
149
+ return [parameter .name
150
+ for parameter
151
+ in inspect .signature (function ).parameters .values ()
152
+ if parameter .kind == parameter .POSITIONAL_OR_KEYWORD ]
153
+ else :
154
+ return inspect .getargspec (function )[0 ]
155
+
156
+
157
+ def register_check (check , codes = None ):
158
+ """Register a new check object."""
159
+ def _add_check (check , kind , codes , args ):
160
+ if check in _checks [kind ]:
161
+ _checks [kind ][check ][0 ].extend (codes or [])
162
+ else :
163
+ _checks [kind ][check ] = (codes or ['' ], args )
164
+ if inspect .isfunction (check ):
165
+ args = _get_parameters (check )
166
+ if args and args [0 ] in ('physical_line' , 'logical_line' ):
167
+ if codes is None :
168
+ codes = ERRORCODE_REGEX .findall (check .__doc__ or '' )
169
+ _add_check (check , args [0 ], codes , args )
170
+ elif inspect .isclass (check ):
171
+ if _get_parameters (check .__init__ )[:2 ] == ['self' , 'tree' ]:
172
+ _add_check (check , 'tree' , codes , None )
173
+ return check
174
+
175
+
144
176
##############################################################################
145
177
# Plugins (check functions) for physical lines
146
178
##############################################################################
147
179
148
-
180
+ @ register_check
149
181
def tabs_or_spaces (physical_line , indent_char ):
150
182
r"""Never mix tabs and spaces.
151
183
@@ -165,6 +197,7 @@ def tabs_or_spaces(physical_line, indent_char):
165
197
return offset , "E101 indentation contains mixed spaces and tabs"
166
198
167
199
200
+ @register_check
168
201
def tabs_obsolete (physical_line ):
169
202
r"""For new projects, spaces-only are strongly recommended over tabs.
170
203
@@ -176,6 +209,7 @@ def tabs_obsolete(physical_line):
176
209
return indent .index ('\t ' ), "W191 indentation contains tabs"
177
210
178
211
212
+ @register_check
179
213
def trailing_whitespace (physical_line ):
180
214
r"""Trailing whitespace is superfluous.
181
215
@@ -197,6 +231,7 @@ def trailing_whitespace(physical_line):
197
231
return 0 , "W293 blank line contains whitespace"
198
232
199
233
234
+ @register_check
200
235
def trailing_blank_lines (physical_line , lines , line_number , total_lines ):
201
236
r"""Trailing blank lines are superfluous.
202
237
@@ -213,6 +248,7 @@ def trailing_blank_lines(physical_line, lines, line_number, total_lines):
213
248
return len (physical_line ), "W292 no newline at end of file"
214
249
215
250
251
+ @register_check
216
252
def maximum_line_length (physical_line , max_line_length , multiline , noqa ):
217
253
r"""Limit all lines to a maximum of 79 characters.
218
254
@@ -251,6 +287,7 @@ def maximum_line_length(physical_line, max_line_length, multiline, noqa):
251
287
##############################################################################
252
288
253
289
290
+ @register_check
254
291
def blank_lines (logical_line , blank_lines , indent_level , line_number ,
255
292
blank_before , previous_logical ,
256
293
previous_unindented_logical_line , previous_indent_level ,
@@ -313,6 +350,7 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
313
350
"class or function definition, found %d" % blank_before
314
351
315
352
353
+ @register_check
316
354
def extraneous_whitespace (logical_line ):
317
355
r"""Avoid extraneous whitespace.
318
356
@@ -345,6 +383,7 @@ def extraneous_whitespace(logical_line):
345
383
yield found , "%s whitespace before '%s'" % (code , char )
346
384
347
385
386
+ @register_check
348
387
def whitespace_around_keywords (logical_line ):
349
388
r"""Avoid extraneous whitespace around keywords.
350
389
@@ -368,6 +407,7 @@ def whitespace_around_keywords(logical_line):
368
407
yield match .start (2 ), "E271 multiple spaces after keyword"
369
408
370
409
410
+ @register_check
371
411
def missing_whitespace_after_import_keyword (logical_line ):
372
412
r"""Multiple imports in form from x import (a, b, c) should have space
373
413
between import statement and parenthesised name list.
@@ -385,6 +425,7 @@ def missing_whitespace_after_import_keyword(logical_line):
385
425
yield pos , "E275 missing whitespace after keyword"
386
426
387
427
428
+ @register_check
388
429
def missing_whitespace (logical_line ):
389
430
r"""Each comma, semicolon or colon should be followed by whitespace.
390
431
@@ -411,6 +452,7 @@ def missing_whitespace(logical_line):
411
452
yield index , "E231 missing whitespace after '%s'" % char
412
453
413
454
455
+ @register_check
414
456
def indentation (logical_line , previous_logical , indent_char ,
415
457
indent_level , previous_indent_level ):
416
458
r"""Use 4 spaces per indentation level.
@@ -442,6 +484,7 @@ def indentation(logical_line, previous_logical, indent_char,
442
484
yield 0 , tmpl % (3 + c , "unexpected indentation" )
443
485
444
486
487
+ @register_check
445
488
def continued_indentation (logical_line , tokens , indent_level , hang_closing ,
446
489
indent_char , noqa , verbose ):
447
490
r"""Continuation lines indentation.
@@ -641,6 +684,7 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
641
684
yield pos , "%s with same indent as next logical line" % code
642
685
643
686
687
+ @register_check
644
688
def whitespace_before_parameters (logical_line , tokens ):
645
689
r"""Avoid extraneous whitespace.
646
690
@@ -673,6 +717,7 @@ def whitespace_before_parameters(logical_line, tokens):
673
717
prev_end = end
674
718
675
719
720
+ @register_check
676
721
def whitespace_around_operator (logical_line ):
677
722
r"""Avoid extraneous whitespace around an operator.
678
723
@@ -696,6 +741,7 @@ def whitespace_around_operator(logical_line):
696
741
yield match .start (2 ), "E222 multiple spaces after operator"
697
742
698
743
744
+ @register_check
699
745
def missing_whitespace_around_operator (logical_line , tokens ):
700
746
r"""Surround operators with a single space on either side.
701
747
@@ -788,6 +834,7 @@ def missing_whitespace_around_operator(logical_line, tokens):
788
834
prev_end = end
789
835
790
836
837
+ @register_check
791
838
def whitespace_around_comma (logical_line ):
792
839
r"""Avoid extraneous whitespace after a comma or a colon.
793
840
@@ -806,6 +853,7 @@ def whitespace_around_comma(logical_line):
806
853
yield found , "E241 multiple spaces after '%s'" % m .group ()[0 ]
807
854
808
855
856
+ @register_check
809
857
def whitespace_around_named_parameter_equals (logical_line , tokens ):
810
858
r"""Don't use spaces around the '=' sign in function arguments.
811
859
@@ -856,6 +904,7 @@ def whitespace_around_named_parameter_equals(logical_line, tokens):
856
904
prev_end = end
857
905
858
906
907
+ @register_check
859
908
def whitespace_before_comment (logical_line , tokens ):
860
909
r"""Separate inline comments by at least two spaces.
861
910
@@ -897,6 +946,7 @@ def whitespace_before_comment(logical_line, tokens):
897
946
prev_end = end
898
947
899
948
949
+ @register_check
900
950
def imports_on_separate_lines (logical_line ):
901
951
r"""Place imports on separate lines.
902
952
@@ -916,6 +966,7 @@ def imports_on_separate_lines(logical_line):
916
966
yield found , "E401 multiple imports on one line"
917
967
918
968
969
+ @register_check
919
970
def module_imports_on_top_of_file (
920
971
logical_line , indent_level , checker_state , noqa ):
921
972
r"""Place imports at the top of the file.
@@ -972,6 +1023,7 @@ def is_string_literal(line):
972
1023
checker_state ['seen_non_imports' ] = True
973
1024
974
1025
1026
+ @register_check
975
1027
def compound_statements (logical_line ):
976
1028
r"""Compound statements (on the same line) are generally discouraged.
977
1029
@@ -1032,6 +1084,7 @@ def compound_statements(logical_line):
1032
1084
found = line .find (';' , found + 1 )
1033
1085
1034
1086
1087
+ @register_check
1035
1088
def explicit_line_join (logical_line , tokens ):
1036
1089
r"""Avoid explicit line join between brackets.
1037
1090
@@ -1071,6 +1124,7 @@ def explicit_line_join(logical_line, tokens):
1071
1124
parens -= 1
1072
1125
1073
1126
1127
+ @register_check
1074
1128
def break_around_binary_operator (logical_line , tokens ):
1075
1129
r"""
1076
1130
Avoid breaks before binary operators.
@@ -1120,6 +1174,7 @@ def is_binary_operator(token_type, text):
1120
1174
previous_text = text
1121
1175
1122
1176
1177
+ @register_check
1123
1178
def comparison_to_singleton (logical_line , noqa ):
1124
1179
r"""Comparison to singletons should use "is" or "is not".
1125
1180
@@ -1154,6 +1209,7 @@ def comparison_to_singleton(logical_line, noqa):
1154
1209
(code , singleton , msg ))
1155
1210
1156
1211
1212
+ @register_check
1157
1213
def comparison_negative (logical_line ):
1158
1214
r"""Negative comparison should be done using "not in" and "is not".
1159
1215
@@ -1175,6 +1231,7 @@ def comparison_negative(logical_line):
1175
1231
yield pos , "E714 test for object identity should be 'is not'"
1176
1232
1177
1233
1234
+ @register_check
1178
1235
def comparison_type (logical_line , noqa ):
1179
1236
r"""Object type comparisons should always use isinstance().
1180
1237
@@ -1198,6 +1255,7 @@ def comparison_type(logical_line, noqa):
1198
1255
yield match .start (), "E721 do not compare types, use 'isinstance()'"
1199
1256
1200
1257
1258
+ @register_check
1201
1259
def bare_except (logical_line , noqa ):
1202
1260
r"""When catching exceptions, mention specific exceptions when possible.
1203
1261
@@ -1214,6 +1272,7 @@ def bare_except(logical_line, noqa):
1214
1272
yield match .start (), "E722 do not use bare except'"
1215
1273
1216
1274
1275
+ @register_check
1217
1276
def ambiguous_identifier (logical_line , tokens ):
1218
1277
r"""Never use the characters 'l', 'O', or 'I' as variable names.
1219
1278
@@ -1266,6 +1325,7 @@ def ambiguous_identifier(logical_line, tokens):
1266
1325
prev_start = start
1267
1326
1268
1327
1328
+ @register_check
1269
1329
def python_3000_has_key (logical_line , noqa ):
1270
1330
r"""The {}.has_key() method is removed in Python 3: use the 'in' operator.
1271
1331
@@ -1277,6 +1337,7 @@ def python_3000_has_key(logical_line, noqa):
1277
1337
yield pos , "W601 .has_key() is deprecated, use 'in'"
1278
1338
1279
1339
1340
+ @register_check
1280
1341
def python_3000_raise_comma (logical_line ):
1281
1342
r"""When raising an exception, use "raise ValueError('message')".
1282
1343
@@ -1290,6 +1351,7 @@ def python_3000_raise_comma(logical_line):
1290
1351
yield match .end () - 1 , "W602 deprecated form of raising exception"
1291
1352
1292
1353
1354
+ @register_check
1293
1355
def python_3000_not_equal (logical_line ):
1294
1356
r"""New code should always use != instead of <>.
1295
1357
@@ -1303,6 +1365,7 @@ def python_3000_not_equal(logical_line):
1303
1365
yield pos , "W603 '<>' is deprecated, use '!='"
1304
1366
1305
1367
1368
+ @register_check
1306
1369
def python_3000_backticks (logical_line ):
1307
1370
r"""Use repr() instead of backticks in Python 3.
1308
1371
@@ -1471,50 +1534,6 @@ def _is_eol_token(token, _eol_token=_is_eol_token):
1471
1534
##############################################################################
1472
1535
1473
1536
1474
- _checks = {'physical_line' : {}, 'logical_line' : {}, 'tree' : {}}
1475
-
1476
-
1477
- def _get_parameters (function ):
1478
- if sys .version_info >= (3 , 3 ):
1479
- return [parameter .name
1480
- for parameter
1481
- in inspect .signature (function ).parameters .values ()
1482
- if parameter .kind == parameter .POSITIONAL_OR_KEYWORD ]
1483
- else :
1484
- return inspect .getargspec (function )[0 ]
1485
-
1486
-
1487
- def register_check (check , codes = None ):
1488
- """Register a new check object."""
1489
- def _add_check (check , kind , codes , args ):
1490
- if check in _checks [kind ]:
1491
- _checks [kind ][check ][0 ].extend (codes or [])
1492
- else :
1493
- _checks [kind ][check ] = (codes or ['' ], args )
1494
- if inspect .isfunction (check ):
1495
- args = _get_parameters (check )
1496
- if args and args [0 ] in ('physical_line' , 'logical_line' ):
1497
- if codes is None :
1498
- codes = ERRORCODE_REGEX .findall (check .__doc__ or '' )
1499
- _add_check (check , args [0 ], codes , args )
1500
- elif inspect .isclass (check ):
1501
- if _get_parameters (check .__init__ )[:2 ] == ['self' , 'tree' ]:
1502
- _add_check (check , 'tree' , codes , None )
1503
-
1504
-
1505
- def init_checks_registry ():
1506
- """Register all globally visible functions.
1507
-
1508
- The first argument name is either 'physical_line' or 'logical_line'.
1509
- """
1510
- mod = inspect .getmodule (register_check )
1511
- for (name , function ) in inspect .getmembers (mod , inspect .isfunction ):
1512
- register_check (function )
1513
-
1514
-
1515
- init_checks_registry ()
1516
-
1517
-
1518
1537
class Checker (object ):
1519
1538
"""Load a Python source file, tokenize it, check coding style."""
1520
1539
0 commit comments