@@ -138,8 +138,8 @@ def tabs_or_spaces(physical_line, indent_char):
138
138
warnings about code that illegally mixes tabs and spaces. When using -tt
139
139
these warnings become errors. These options are highly recommended!
140
140
141
- Okay: if a == 0:\n a = 1\n b = 1
142
- E101: if a == 0:\n a = 1\n\tb = 1
141
+ Okay: if a == 0:\n a = 1
142
+ E101: if a == 0:\n \ta = 1
143
143
"""
144
144
indent = INDENT_REGEX .match (physical_line ).group (1 )
145
145
for offset , char in enumerate (indent ):
@@ -351,8 +351,8 @@ def missing_whitespace(logical_line):
351
351
yield index , "E231 missing whitespace after '%s'" % char
352
352
353
353
354
- def indentation (logical_line , previous_logical , indent_char ,
355
- indent_level , previous_indent_level ):
354
+ def indentation (logical_line , previous_logical , indent_char , initial_indent ,
355
+ indent_level , previous_indent_level , physical_line ):
356
356
r"""Use 4 spaces per indentation level.
357
357
358
358
For really old code that you don't want to mess up, you can continue to
@@ -370,14 +370,53 @@ def indentation(logical_line, previous_logical, indent_char,
370
370
Okay: a = 1\nb = 2
371
371
E113: a = 1\n b = 2
372
372
E116: a = 1\n # b = 2
373
+
374
+ Okay: if a == 0:\n a = 1\nif a == 1:\n a = 1
375
+ W191: if a == 0:\n\ta = 1\nif a == 1:\n\ta = 1
376
+
377
+ E117: if a == 0:\n\ta = 1\nif a == 1:\n a = 1
378
+
379
+ E118: # if a == 0:\n a = 1\nif a == 1:\n\ta = 1
380
+
373
381
"""
382
+
374
383
c = 0 if logical_line else 3
384
+ indent = INDENT_REGEX .match (physical_line ).group (1 )
385
+ tab = "\t "
375
386
tmpl = "E11%d %s" if logical_line else "E11%d %s (comment)"
376
- if indent_level % 4 :
377
- yield 0 , tmpl % (1 + c , "indentation is not a multiple of four" )
378
387
indent_expect = previous_logical .endswith (':' )
388
+
389
+ expectedTabs = previous_indent_level // 4 + 1
390
+ expectedSpaces = previous_indent_level + 4
391
+ numTabs = indent .count ("\t " )
392
+ numSpaces = sum (a .isspace () for a in indent ) - numTabs
393
+
394
+ if indent_level % 4 :
395
+ yield 0 , tmpl % (1 + c , "indentation is not a multiple of 4" )
396
+
379
397
if indent_expect and indent_level <= previous_indent_level :
380
398
yield 0 , tmpl % (2 + c , "expected an indented block" )
399
+ elif indent_expect and indent_level > previous_indent_level :
400
+ if initial_indent == tab :
401
+ if ' ' in indent :
402
+ yield 0 , tmpl % (7 , "TAB ERROR: %d tabs indentation "
403
+ "expected; indentation was %d tabs and "
404
+ "%d spaces" %
405
+ (expectedTabs , numTabs , numSpaces ))
406
+ elif indent_level != previous_indent_level + 4 :
407
+ yield 0 , tmpl % (7 , "TAB ERROR: %d tabs indentation "
408
+ "expected; indentation was %d tabs" %
409
+ (expectedTabs , numTabs ))
410
+ else :
411
+ if tab in indent :
412
+ yield 0 , tmpl % (8 , "SPACE ERROR: %d spaces indentation "
413
+ "expected; indentation was %d tabs and "
414
+ "%d spaces" %
415
+ (expectedSpaces , numTabs , numSpaces ))
416
+ elif indent_level != previous_indent_level + 4 :
417
+ yield 0 , tmpl % (8 , "SPACE ERROR: %d SPACES indentation "
418
+ "expected; indentation was %d spaces" %
419
+ (expectedSpaces , indent_level ))
381
420
elif not indent_expect and indent_level > previous_indent_level :
382
421
yield 0 , tmpl % (3 + c , "unexpected indentation" )
383
422
@@ -1207,18 +1246,18 @@ def expand_indent(line):
1207
1246
>>> expand_indent(' ')
1208
1247
4
1209
1248
>>> expand_indent('\t')
1210
- 8
1249
+ 4
1211
1250
>>> expand_indent(' \t')
1212
1251
8
1213
1252
>>> expand_indent(' \t')
1214
- 16
1253
+ 12
1215
1254
"""
1216
1255
if '\t ' not in line :
1217
1256
return len (line ) - len (line .lstrip ())
1218
1257
result = 0
1219
1258
for char in line :
1220
1259
if char == '\t ' :
1221
- result = result // 8 * 8 + 8
1260
+ result = result // 4 * 4 + 4
1222
1261
elif char == ' ' :
1223
1262
result += 1
1224
1263
else :
@@ -1316,7 +1355,10 @@ def _is_eol_token(token, _eol_token=_is_eol_token):
1316
1355
1317
1356
def _get_parameters (function ):
1318
1357
if sys .version_info >= (3 , 3 ):
1319
- return list (inspect .signature (function ).parameters )
1358
+ return [parameter .name
1359
+ for parameter
1360
+ in inspect .signature (function ).parameters .values ()
1361
+ if parameter .kind == parameter .POSITIONAL_OR_KEYWORD ]
1320
1362
else :
1321
1363
return inspect .getargspec (function )[0 ]
1322
1364
@@ -1416,6 +1458,7 @@ def readline(self):
1416
1458
self .line_number += 1
1417
1459
if self .indent_char is None and line [:1 ] in WHITESPACE :
1418
1460
self .indent_char = line [0 ]
1461
+ self .initial_indent = line [0 ]
1419
1462
return line
1420
1463
1421
1464
def run_check (self , check , argument_names ):
@@ -1571,6 +1614,7 @@ def check_all(self, expected=None, line_offset=0):
1571
1614
self .check_ast ()
1572
1615
self .line_number = 0
1573
1616
self .indent_char = None
1617
+ self .initial_indent = None
1574
1618
self .indent_level = self .previous_indent_level = 0
1575
1619
self .previous_logical = ''
1576
1620
self .tokens = []
0 commit comments