@@ -104,6 +104,7 @@ def lru_cache(maxsize=128): # noqa as it's a fake implementation.
104
104
'method' : 1 ,
105
105
}
106
106
MAX_DOC_LENGTH = 72
107
+ INDENT_SIZE = 4
107
108
REPORT_FORMAT = {
108
109
'default' : '%(path)s:%(row)d:%(col)d: %(code)s %(text)s' ,
109
110
'pylint' : '%(path)s:%(row)d: [%(code)s] %(text)s' ,
@@ -543,8 +544,9 @@ def missing_whitespace(logical_line):
543
544
544
545
@register_check
545
546
def indentation (logical_line , previous_logical , indent_char ,
546
- indent_level , previous_indent_level ):
547
- r"""Use 4 spaces per indentation level.
547
+ indent_level , previous_indent_level ,
548
+ indent_size , indent_size_str ):
549
+ r"""Use indent_size (PEP8 says 4) spaces per indentation level.
548
550
549
551
For really old code that you don't want to mess up, you can continue
550
552
to use 8-space tabs.
@@ -564,8 +566,11 @@ def indentation(logical_line, previous_logical, indent_char,
564
566
"""
565
567
c = 0 if logical_line else 3
566
568
tmpl = "E11%d %s" if logical_line else "E11%d %s (comment)"
567
- if indent_level % 4 :
568
- yield 0 , tmpl % (1 + c , "indentation is not a multiple of four" )
569
+ if indent_level % indent_size :
570
+ yield 0 , tmpl % (
571
+ 1 + c ,
572
+ "indentation is not a multiple of " + indent_size_str ,
573
+ )
569
574
indent_expect = previous_logical .endswith (':' )
570
575
if indent_expect and indent_level <= previous_indent_level :
571
576
yield 0 , tmpl % (2 + c , "expected an indented block" )
@@ -581,7 +586,8 @@ def indentation(logical_line, previous_logical, indent_char,
581
586
582
587
@register_check
583
588
def continued_indentation (logical_line , tokens , indent_level , hang_closing ,
584
- indent_char , noqa , verbose ):
589
+ indent_char , indent_size , indent_size_str , noqa ,
590
+ verbose ):
585
591
r"""Continuation lines indentation.
586
592
587
593
Continuation lines should align wrapped elements either vertically
@@ -620,7 +626,8 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
620
626
indent_next = logical_line .endswith (':' )
621
627
622
628
row = depth = 0
623
- valid_hangs = (4 ,) if indent_char != '\t ' else (4 , 8 )
629
+ valid_hangs = (indent_size ,) if indent_char != '\t ' \
630
+ else (indent_size , indent_size * 2 )
624
631
# remember how many brackets were opened on each line
625
632
parens = [0 ] * nrows
626
633
# relative indents of physical lines
@@ -685,7 +692,8 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
685
692
# visual indent is broken
686
693
yield (start , "E128 continuation line "
687
694
"under-indented for visual indent" )
688
- elif hanging_indent or (indent_next and rel_indent [row ] == 8 ):
695
+ elif hanging_indent or (indent_next and
696
+ rel_indent [row ] == 2 * indent_size ):
689
697
# hanging indent is verified
690
698
if close_bracket and not hang_closing :
691
699
yield (start , "E123 closing bracket does not match "
@@ -708,7 +716,7 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
708
716
error = "E131" , "unaligned for hanging indent"
709
717
else :
710
718
hangs [depth ] = hang
711
- if hang > 4 :
719
+ if hang > indent_size :
712
720
error = "E126" , "over-indented for hanging indent"
713
721
else :
714
722
error = "E121" , "under-indented for hanging indent"
@@ -775,8 +783,8 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
775
783
if last_token_multiline :
776
784
rel_indent [end [0 ] - first_row ] = rel_indent [row ]
777
785
778
- if indent_next and expand_indent (line ) == indent_level + 4 :
779
- pos = (start [0 ], indent [0 ] + 4 )
786
+ if indent_next and expand_indent (line ) == indent_level + indent_size :
787
+ pos = (start [0 ], indent [0 ] + indent_size )
780
788
if visual_indent :
781
789
code = "E129 visually indented line"
782
790
else :
@@ -1960,8 +1968,12 @@ def __init__(self, filename=None, lines=None,
1960
1968
self ._ast_checks = options .ast_checks
1961
1969
self .max_line_length = options .max_line_length
1962
1970
self .max_doc_length = options .max_doc_length
1971
+ self .indent_size = options .indent_size
1963
1972
self .multiline = False # in a multiline string?
1964
1973
self .hang_closing = options .hang_closing
1974
+ self .indent_size = options .indent_size
1975
+ self .indent_size_str = ({2 : 'two' , 4 : 'four' , 8 : 'eight' }
1976
+ .get (self .indent_size , str (self .indent_size )))
1965
1977
self .verbose = options .verbose
1966
1978
self .filename = filename
1967
1979
# Dictionary where a checker can store its custom state.
@@ -2528,8 +2540,8 @@ def get_parser(prog='pycodestyle', version=__version__):
2528
2540
usage = "%prog [options] input ..." )
2529
2541
parser .config_options = [
2530
2542
'exclude' , 'filename' , 'select' , 'ignore' , 'max-line-length' ,
2531
- 'max-doc-length' , 'hang-closing ' , 'count ' , 'format ' , 'quiet ' ,
2532
- 'show-pep8' , 'show-source' , 'statistics' , 'verbose' ]
2543
+ 'max-doc-length' , 'indent-size ' , 'hang-closing ' , 'count ' , 'format ' ,
2544
+ 'quiet' , ' show-pep8' , 'show-source' , 'statistics' , 'verbose' ]
2533
2545
parser .add_option ('-v' , '--verbose' , default = 0 , action = 'count' ,
2534
2546
help = "print status messages, or debug with -vv" )
2535
2547
parser .add_option ('-q' , '--quiet' , default = 0 , action = 'count' ,
@@ -2569,6 +2581,10 @@ def get_parser(prog='pycodestyle', version=__version__):
2569
2581
default = None ,
2570
2582
help = "set maximum allowed doc line length and perform "
2571
2583
"these checks (unchecked if not set)" )
2584
+ parser .add_option ('--indent-size' , type = 'int' , metavar = 'n' ,
2585
+ default = INDENT_SIZE ,
2586
+ help = "set how many spaces make up an indent "
2587
+ "(default: %default)" )
2572
2588
parser .add_option ('--hang-closing' , action = 'store_true' ,
2573
2589
help = "hang closing bracket instead of matching "
2574
2590
"indentation of opening bracket's line" )
0 commit comments