Skip to content

Commit 6b059f5

Browse files
Fix underscore grouping regex to accept left-grouped fractional parts
The PEP 515 validation regex rejected properly left-grouped fractional underscores like 1_234.567_89 and 123_456.123_456, then suggested them back as their own fix. Fix the regex to accept the fractional pattern \d{3}(_\d{3})*(_\d{1,2})? and require at least one integer digit so .123_456 (missing leading zero) is still caught. Also improve the error message from "has underscores that are not delimiting packs of three digits" to "has non-standard underscore grouping" since the checker validates both integer and fractional parts with different grouping directions.
1 parent 75e2ef8 commit 6b059f5

4 files changed

Lines changed: 28 additions & 29 deletions

File tree

pylint/checkers/format.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -898,12 +898,11 @@ def add_bad_notation_message(reason: str) -> None:
898898
else "has underscores instead of engineering notation"
899899
)
900900
wrong_underscore_notation = not re.match(
901-
r"^\d{0,3}(_\d{3})*\.?\d*([eE]-?\d{0,3}(_\d{3})*)?$", string
901+
r"^\d{1,3}(_\d{3})*\.?(\d{3}(_\d{3})*(_\d{1,2})?|\d*)([eE]-?\d{0,3}(_\d{3})*)?$",
902+
string,
902903
)
903904
if pep515 and wrong_underscore_notation:
904-
return add_bad_notation_message(
905-
"has underscores that are not delimiting packs of three digits"
906-
)
905+
return add_bad_notation_message("has non-standard underscore grouping")
907906
return None
908907

909908
def _check_non_decimal_notation(

tests/functional/b/bad_number/bad_number_notation_default.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,12 @@ def function_with_sci(param=10.0e4, other_param=20.0e5):
116116
invalid_underscore_hexa = 0x12c_456 # [bad-number-notation]
117117

118118
invalid_underscore_float_no_int = .123_456 # [bad-number-notation]
119-
invalid_underscore_float_no_frac = 123_456.123_456 # [bad-number-notation]
119+
valid_underscore_float_both_parts = 123_456.123_456
120120
incorrect_sci_underscore = 1.234_567e6 # [bad-number-notation]
121121
incorrect_sci_uppercase = 1.234_567E6 # [bad-number-notation]
122122
incorrect_sci_underscore_exp = 1.2e1_0 # [bad-number-notation]
123-
invalid_underscore_float = 1_234.567_89 # [bad-number-notation]
123+
valid_underscore_float_frac = 1_234.567_89
124+
bad_frac_grouping = 1_000.12_3456 # [bad-number-notation]
124125
wrong_big_underscore = 45.3_45e6 # [bad-number-notation]
125126
wrong_small_underscore = 0.000_12e-26 # [bad-number-notation]
126127
scientific_double_digit_underscore = 1_2e8 # [bad-number-notation]
Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
bad-number-notation:4:19:4:25::'1504e5' has a base, '1504', that is not between 1 and 1000, and it should be written as '1.504e8' or '150.4e6' or '150_400_000.0' instead:HIGH
2-
bad-number-notation:10:23:10:38::'1_23_456_7_89.0' has underscores that are not delimiting packs of three digits, and it should be written as '1.23456789e8' or '123.456789e6' or '123_456_789.0' instead:HIGH
2+
bad-number-notation:10:23:10:38::'1_23_456_7_89.0' has non-standard underscore grouping, and it should be written as '1.23456789e8' or '123.456789e6' or '123_456_789.0' instead:HIGH
33
bad-number-notation:11:23:11:38::'1_23_4_5_67_8e9' has exponent and underscore at the same time, and it should be written as '1.2345678e16' or '12.345678e15' instead:HIGH
44
bad-number-notation:12:35:12:46::'123456789.0' is bigger than 1.0e6, and it should be written as '1.23456789e8' or '123.456789e6' or '123_456_789.0' instead:HIGH
55
bad-number-notation:18:33:18:38::'123e4' has an exponent '4' that is not a multiple of 3, and it should be written as '1.23e6' or '1_230_000.0' instead:HIGH
@@ -27,7 +27,7 @@ bad-number-notation:56:26:56:30::'0.00' is an unconventional zero literal, and i
2727
bad-number-notation:60:19:60:28::'0.0000001' is smaller than 1.0e-6, and it should be written as '0.000_000_1' or '1.0e-7' or '100.0e-9' instead:HIGH
2828
bad-number-notation:64:14:64:26::'1541455200.0' is bigger than 1.0e6, and it should be written as '1.5414552e9' or '1_541_455_200.0' instead:HIGH
2929
bad-number-notation:65:14:65:23::'1000000.0' is bigger than 1.0e6, and it should be written as '1.0e6' or '1_000_000.0' instead:HIGH
30-
bad-number-notation:67:16:67:34::'486787299458.15656' is bigger than 1.0e6, and have 17 significant digits, more than the 15 python guarantee, and it should be written as '4.86787299458157e11' or '486.787299458157e9' or '486_787_299_458.157' or 'decimal.Decimal("486787299458.15656")' instead:HIGH
30+
bad-number-notation:67:16:67:34::"'486787299458.15656' is bigger than 1.0e6, and have 17 significant digits, more than the 15 python guarantee, and it should be written as '4.86787299458157e11' or '486.787299458157e9' or '486_787_299_458.157' or 'decimal.Decimal(""486787299458.15656"")' instead":HIGH
3131
bad-number-notation:99:23:99:27::'15e4' has an exponent '4' that is not a multiple of 3, and it should be written as '1.5e5' or '150.0e3' or '150_000.0' instead:HIGH
3232
bad-number-notation:104:28:104:34::'10.0e4' has an exponent '4' that is not a multiple of 3, and it should be written as '1.0e5' or '100.0e3' or '100_000.0' instead:HIGH
3333
bad-number-notation:104:48:104:54::'20.0e5' has an exponent '5' that is not a multiple of 3, and it should be written as '2.0e6' or '2_000_000.0' instead:HIGH
@@ -36,26 +36,25 @@ bad-number-notation:108:27:108:33::'20.0e4' has an exponent '4' that is not a mu
3636
bad-number-notation:112:29:112:57::'123_000_000.12345e12_000_000' has exponent and underscore at the same time, and it should be written as '1.2300000012345e12000008' or '123.00000012345e12000006' or 'math.inf' instead:HIGH
3737
bad-number-notation:113:33:113:62::'123_000_000.12345E123_000_000' has exponent and underscore at the same time, and it should be written as '1.2300000012345e123000008' or '123.00000012345e123000006' or 'math.inf' instead:HIGH
3838
bad-number-notation:116:26:116:35::'0x12c_456' has underscores that are not grouping hex digits by 4, and it should be written as '0x12_c456' instead:HIGH
39-
bad-number-notation:118:34:118:42::'.123_456' has underscores that are not delimiting packs of three digits, and it should be written as '0.123_456' or '1.23456e-1' or '123.456e-3' instead:HIGH
40-
bad-number-notation:119:35:119:50::'123_456.123_456' has underscores that are not delimiting packs of three digits, and it should be written as '1.23456123456e5' or '123.456123456e3' or '123_456.123_456' instead:HIGH
39+
bad-number-notation:118:34:118:42::'.123_456' has non-standard underscore grouping, and it should be written as '0.123_456' or '1.23456e-1' or '123.456e-3' instead:HIGH
4140
bad-number-notation:120:27:120:38::'1.234_567e6' has exponent and underscore at the same time, and it should be written as '1.234567e6' or '1_234_567.0' instead:HIGH
4241
bad-number-notation:121:26:121:37::'1.234_567E6' has exponent and underscore at the same time, and it should be written as '1.234567e6' or '1_234_567.0' instead:HIGH
4342
bad-number-notation:122:31:122:38::'1.2e1_0' has exponent and underscore at the same time, and it should be written as '1.2e10' or '12.0e9' or '12_000_000_000.0' instead:HIGH
44-
bad-number-notation:123:27:123:39::'1_234.567_89' has underscores that are not delimiting packs of three digits, and it should be written as '1.23456789e3' or '1_234.567_89' instead:HIGH
45-
bad-number-notation:124:23:124:32::'45.3_45e6' has exponent and underscore at the same time, and it should be written as '4.5345e7' or '45.345e6' or '45_345_000.0' instead:HIGH
46-
bad-number-notation:125:25:125:37::'0.000_12e-26' has exponent and underscore at the same time, and it should be written as '1.2e-30' instead:HIGH
47-
bad-number-notation:126:37:126:42::'1_2e8' has exponent and underscore at the same time, and it should be written as '1.2e9' or '1_200_000_000.0' instead:HIGH
48-
bad-number-notation:127:37:127:43::'12_3e3' has exponent and underscore at the same time, and it should be written as '1.23e5' or '123.0e3' or '123_000.0' instead:HIGH
49-
bad-number-notation:128:25:128:40::'1_234.567_89e10' has exponent and underscore at the same time, and it should be written as '1.23456789e13' or '12.3456789e12' or '12_345_678_900_000.0' instead:HIGH
50-
bad-number-notation:129:29:129:36::'1.2e1_0' has exponent and underscore at the same time, and it should be written as '1.2e10' or '12.0e9' or '12_000_000_000.0' instead:HIGH
51-
bad-number-notation:130:34:130:45::'1_2.3_4e5_6' has exponent and underscore at the same time, and it should be written as '1.234e57' instead:HIGH
52-
bad-number-notation:131:24:131:39::'1_234.567_89E10' has exponent and underscore at the same time, and it should be written as '1.23456789e13' or '12.3456789e12' or '12_345_678_900_000.0' instead:HIGH
53-
bad-number-notation:132:20:132:25::'1_0e6' has exponent and underscore at the same time, and it should be written as '1.0e7' or '10.0e6' or '10_000_000.0' instead:HIGH
54-
bad-number-notation:133:21:133:35::'1_000_000.0e-3' has exponent and underscore at the same time, and it should be written as '1.0e3' or '1_000.0' instead:HIGH
55-
bad-number-notation:134:21:134:32::'0.000_001e3' has exponent and underscore at the same time, and it should be written as '0.001' or '1.0e-3' instead:HIGH
56-
bad-number-notation:135:21:135:28::'1_0.0e2' has exponent and underscore at the same time, and it should be written as '1.0e3' or '1_000.0' instead:HIGH
57-
bad-number-notation:138:21:138:28::'1.5_6e3' has exponent and underscore at the same time, and it should be written as '1.56e3' or '1_560.0' instead:HIGH
58-
bad-number-notation:139:27:139:33::'15_6e2' has exponent and underscore at the same time, and it should be written as '1.56e4' or '15.6e3' or '15_600.0' instead:HIGH
59-
bad-number-notation:142:35:142:43::'10.0_0e3' has exponent and underscore at the same time, and it should be written as '1.0e4' or '10.0e3' or '10_000.0' instead:HIGH
60-
bad-number-notation:142:57:142:65::'20.0_0e3' has exponent and underscore at the same time, and it should be written as '2.0e4' or '20.0e3' or '20_000.0' instead:HIGH
61-
bad-number-notation:153:19:153:27::'1_23_456' has underscores that are not grouping digits by 3, and it should be written as '123_456' instead:HIGH
43+
bad-number-notation:124:20:124:33::'1_000.12_3456' has non-standard underscore grouping, and it should be written as '1.000123456e3' or '1_000.123_456' instead:HIGH
44+
bad-number-notation:125:23:125:32::'45.3_45e6' has exponent and underscore at the same time, and it should be written as '4.5345e7' or '45.345e6' or '45_345_000.0' instead:HIGH
45+
bad-number-notation:126:25:126:37::'0.000_12e-26' has exponent and underscore at the same time, and it should be written as '1.2e-30' instead:HIGH
46+
bad-number-notation:127:37:127:42::'1_2e8' has exponent and underscore at the same time, and it should be written as '1.2e9' or '1_200_000_000.0' instead:HIGH
47+
bad-number-notation:128:37:128:43::'12_3e3' has exponent and underscore at the same time, and it should be written as '1.23e5' or '123.0e3' or '123_000.0' instead:HIGH
48+
bad-number-notation:129:25:129:40::'1_234.567_89e10' has exponent and underscore at the same time, and it should be written as '1.23456789e13' or '12.3456789e12' or '12_345_678_900_000.0' instead:HIGH
49+
bad-number-notation:130:29:130:36::'1.2e1_0' has exponent and underscore at the same time, and it should be written as '1.2e10' or '12.0e9' or '12_000_000_000.0' instead:HIGH
50+
bad-number-notation:131:34:131:45::'1_2.3_4e5_6' has exponent and underscore at the same time, and it should be written as '1.234e57' instead:HIGH
51+
bad-number-notation:132:24:132:39::'1_234.567_89E10' has exponent and underscore at the same time, and it should be written as '1.23456789e13' or '12.3456789e12' or '12_345_678_900_000.0' instead:HIGH
52+
bad-number-notation:133:20:133:25::'1_0e6' has exponent and underscore at the same time, and it should be written as '1.0e7' or '10.0e6' or '10_000_000.0' instead:HIGH
53+
bad-number-notation:134:21:134:35::'1_000_000.0e-3' has exponent and underscore at the same time, and it should be written as '1.0e3' or '1_000.0' instead:HIGH
54+
bad-number-notation:135:21:135:32::'0.000_001e3' has exponent and underscore at the same time, and it should be written as '0.001' or '1.0e-3' instead:HIGH
55+
bad-number-notation:136:21:136:28::'1_0.0e2' has exponent and underscore at the same time, and it should be written as '1.0e3' or '1_000.0' instead:HIGH
56+
bad-number-notation:139:21:139:28::'1.5_6e3' has exponent and underscore at the same time, and it should be written as '1.56e3' or '1_560.0' instead:HIGH
57+
bad-number-notation:140:27:140:33::'15_6e2' has exponent and underscore at the same time, and it should be written as '1.56e4' or '15.6e3' or '15_600.0' instead:HIGH
58+
bad-number-notation:143:35:143:43::'10.0_0e3' has exponent and underscore at the same time, and it should be written as '1.0e4' or '10.0e3' or '10_000.0' instead:HIGH
59+
bad-number-notation:143:57:143:65::'20.0_0e3' has exponent and underscore at the same time, and it should be written as '2.0e4' or '20.0e3' or '20_000.0' instead:HIGH
60+
bad-number-notation:154:19:154:27::'1_23_456' has underscores that are not grouping digits by 3, and it should be written as '123_456' instead:HIGH

tests/functional/b/bad_number/bad_number_pep515.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
bad-number-notation:3:23:3:38::'1_23_456_7_89.0' has underscores that are not delimiting packs of three digits, and it should be written as '123_456_789.0' instead:HIGH
1+
bad-number-notation:3:23:3:38::'1_23_456_7_89.0' has non-standard underscore grouping, and it should be written as '123_456_789.0' instead:HIGH
22
bad-number-notation:4:23:4:38::'1_23_4_5_67_8e9' has exponent and underscore at the same time, and it should be written as '1.2345678e16' instead:HIGH
33
bad-number-notation:5:35:5:46::'123456789.0' is bigger than 1.0e6, and it should be written as '123_456_789.0' instead:HIGH
44
bad-number-notation:6:22:6:34::'1.2345678e13' uses exponent notation instead of underscore grouping, and it should be written as '12_345_678_000_000.0' instead:HIGH

0 commit comments

Comments
 (0)