@@ -104,6 +104,7 @@ def lru_cache(maxsize=128): # noqa as it's a fake implementation.
104104 'method' : 1 ,
105105}
106106MAX_DOC_LENGTH = 72
107+ INDENT_SIZE = 4
107108REPORT_FORMAT = {
108109 'default' : '%(path)s:%(row)d:%(col)d: %(code)s %(text)s' ,
109110 'pylint' : '%(path)s:%(row)d: [%(code)s] %(text)s' ,
@@ -543,8 +544,9 @@ def missing_whitespace(logical_line):
543544
544545@register_check
545546def 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.
548550
549551 For really old code that you don't want to mess up, you can continue
550552 to use 8-space tabs.
@@ -564,8 +566,11 @@ def indentation(logical_line, previous_logical, indent_char,
564566 """
565567 c = 0 if logical_line else 3
566568 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+ )
569574 indent_expect = previous_logical .endswith (':' )
570575 if indent_expect and indent_level <= previous_indent_level :
571576 yield 0 , tmpl % (2 + c , "expected an indented block" )
@@ -581,7 +586,8 @@ def indentation(logical_line, previous_logical, indent_char,
581586
582587@register_check
583588def 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 ):
585591 r"""Continuation lines indentation.
586592
587593 Continuation lines should align wrapped elements either vertically
@@ -620,7 +626,8 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
620626 indent_next = logical_line .endswith (':' )
621627
622628 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 )
624631 # remember how many brackets were opened on each line
625632 parens = [0 ] * nrows
626633 # relative indents of physical lines
@@ -685,7 +692,8 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
685692 # visual indent is broken
686693 yield (start , "E128 continuation line "
687694 "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 ):
689697 # hanging indent is verified
690698 if close_bracket and not hang_closing :
691699 yield (start , "E123 closing bracket does not match "
@@ -708,7 +716,7 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
708716 error = "E131" , "unaligned for hanging indent"
709717 else :
710718 hangs [depth ] = hang
711- if hang > 4 :
719+ if hang > indent_size :
712720 error = "E126" , "over-indented for hanging indent"
713721 else :
714722 error = "E121" , "under-indented for hanging indent"
@@ -775,8 +783,8 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
775783 if last_token_multiline :
776784 rel_indent [end [0 ] - first_row ] = rel_indent [row ]
777785
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 )
780788 if visual_indent :
781789 code = "E129 visually indented line"
782790 else :
@@ -1960,8 +1968,12 @@ def __init__(self, filename=None, lines=None,
19601968 self ._ast_checks = options .ast_checks
19611969 self .max_line_length = options .max_line_length
19621970 self .max_doc_length = options .max_doc_length
1971+ self .indent_size = options .indent_size
19631972 self .multiline = False # in a multiline string?
19641973 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 )))
19651977 self .verbose = options .verbose
19661978 self .filename = filename
19671979 # Dictionary where a checker can store its custom state.
@@ -2528,8 +2540,8 @@ def get_parser(prog='pycodestyle', version=__version__):
25282540 usage = "%prog [options] input ..." )
25292541 parser .config_options = [
25302542 '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' ]
25332545 parser .add_option ('-v' , '--verbose' , default = 0 , action = 'count' ,
25342546 help = "print status messages, or debug with -vv" )
25352547 parser .add_option ('-q' , '--quiet' , default = 0 , action = 'count' ,
@@ -2569,6 +2581,10 @@ def get_parser(prog='pycodestyle', version=__version__):
25692581 default = None ,
25702582 help = "set maximum allowed doc line length and perform "
25712583 "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)" )
25722588 parser .add_option ('--hang-closing' , action = 'store_true' ,
25732589 help = "hang closing bracket instead of matching "
25742590 "indentation of opening bracket's line" )
0 commit comments