File tree Expand file tree Collapse file tree 2 files changed +16
-29
lines changed
Expand file tree Collapse file tree 2 files changed +16
-29
lines changed Original file line number Diff line number Diff line change @@ -33,24 +33,12 @@ def _autofix(filename, new_contents):
3333 f .write (new_contents )
3434
3535
36- def parse_indent (s ):
37- # type: (str) -> str
36+ def parse_num_to_int (s ):
37+ """Convert string numbers to int, leaving strings as is."""
3838 try :
39- int_indentation_spec = int (s )
39+ return int (s )
4040 except ValueError :
41- if not s .strip ():
42- return s
43- else :
44- raise ValueError (
45- 'Non-whitespace JSON indentation delimiter supplied. ' ,
46- )
47- else :
48- if int_indentation_spec >= 0 :
49- return int_indentation_spec * ' '
50- else :
51- raise ValueError (
52- 'Negative integer supplied to construct JSON indentation delimiter. ' ,
53- )
41+ return s
5442
5543
5644def parse_topkeys (s ):
@@ -68,9 +56,12 @@ def pretty_format_json(argv=None):
6856 )
6957 parser .add_argument (
7058 '--indent' ,
71- type = parse_indent ,
72- default = ' ' ,
73- help = 'String used as delimiter for one indentation level' ,
59+ type = parse_num_to_int ,
60+ default = '2' ,
61+ help = (
62+ 'The number of indent spaces or a string to be used as delimiter'
63+ ' for indentation level e.g. 4 or "\t " (Default: 2)'
64+ ),
7465 )
7566 parser .add_argument (
7667 '--no-ensure-ascii' ,
Original file line number Diff line number Diff line change 33import pytest
44from six import PY2
55
6- from pre_commit_hooks .pretty_format_json import parse_indent
6+ from pre_commit_hooks .pretty_format_json import parse_num_to_int
77from pre_commit_hooks .pretty_format_json import pretty_format_json
88from testing .util import get_resource_path
99
1010
11- def test_parse_indent ():
12- assert parse_indent ('0' ) == ''
13- assert parse_indent ('2' ) == ' '
14- assert parse_indent ('\t ' ) == '\t '
15- with pytest .raises (ValueError ):
16- parse_indent ('a' )
17- with pytest .raises (ValueError ):
18- parse_indent ('-2' )
19-
11+ def test_parse_num_to_int ():
12+ assert parse_num_to_int ('0' ) == 0
13+ assert parse_num_to_int ('2' ) == 2
14+ assert parse_num_to_int ('\t ' ) == '\t '
15+ assert parse_num_to_int (' ' ) == ' '
2016
2117
2218@pytest .mark .parametrize (
You can’t perform that action at this time.
0 commit comments