Skip to content

Commit 2886dc7

Browse files
Fix the 0 case and mixed underscore and exponent case
1 parent 9a577d2 commit 2886dc7

File tree

5 files changed

+83
-95
lines changed

5 files changed

+83
-95
lines changed

pylint/checkers/format.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,6 @@ def to_standard_or_engineering_base(cls, number: float) -> tuple[str, str]:
566566
return "0", "0"
567567
if number == math.inf:
568568
return "math.inf", "0"
569-
print(f"Converting {number} to standard or engineering base")
570569
exponent = math.floor(math.log10(abs(number)))
571570
if exponent == 0:
572571
return str(number), "0"
@@ -662,7 +661,6 @@ def raise_bad_float_notation() -> None:
662661
suggested.add(self.to_standard_engineering_notation(value))
663662
if pep515:
664663
suggested.add(self.to_standard_underscore_grouping(value))
665-
print("\tBad float notation, suggesting:", suggested)
666664
return self.add_message(
667665
"bad-float-notation",
668666
args=("' or '".join(sorted(suggested))),
@@ -679,9 +677,8 @@ def raise_bad_float_notation() -> None:
679677
1 <= value < 10 and self.linter.config.strict_scientific_notation
680678
) or 1 <= value < 1000
681679
is_written_complexly = has_underscore or has_exponent
682-
print(f"Checking {string} line {line_num}")
680+
# print(f"Checking {string} line {line_num}")
683681
if should_be_written_simply and is_written_complexly:
684-
print(f"\t{value} is a simple number")
685682
# If the value does not deserve a complex notation then write it in a simple way.
686683
# The threshold is guaranteed to be higher than those value.
687684
# When 1 <= value < 10 the engineering notation is equivalent to the scientific notation
@@ -692,29 +689,26 @@ def raise_bad_float_notation() -> None:
692689
abs_value < self.linter.config.float_notation_threshold # under threshold
693690
and ( # use scientific or engineering notation and under 1/threshold
694691
self.linter.config.strict_underscore_notation
695-
or abs_value > 1 / self.linter.config.float_notation_threshold
692+
or (
693+
value == 0
694+
or abs_value > 1 / self.linter.config.float_notation_threshold
695+
)
696696
)
697697
)
698-
print(
699-
"\tshould_not_be_checked_because_of_threshold=",
700-
should_not_be_checked_because_of_threshold,
701-
)
702698
if not is_written_complexly:
703699
if should_not_be_checked_because_of_threshold:
704700
# This number is free style, we do not have to check it, unless it's
705701
# written complexly, then it could be badly written
706-
print("\tFree style number, no need to check")
707702
return None
708703
return raise_bad_float_notation()
709704
if has_exponent:
710-
if self.linter.config.strict_underscore_notation:
705+
if self.linter.config.strict_underscore_notation or has_underscore:
711706
# If we have exponent it means it's not proper underscore
712707
return raise_bad_float_notation()
713708
base_as_str, exponent_as_str = string.lower().split("e")
714709
base = float(base_as_str)
715-
print("\tBase:", base, "Exponent:", exponent_as_str)
710+
# print("\tBase:", base, "Exponent:", exponent_as_str)
716711
wrong_scientific_notation = not (1 <= base < 10)
717-
print(f"\twrong_scientific_notation:{wrong_scientific_notation}")
718712
if (
719713
self.linter.config.strict_scientific_notation
720714
and wrong_scientific_notation
@@ -723,15 +717,13 @@ def raise_bad_float_notation() -> None:
723717
wrong_engineering_notation = not (
724718
1 <= base < 1000 and int(exponent_as_str) % 3 == 0
725719
)
726-
print(f"\twrong_engineering_notation:{wrong_engineering_notation}")
727720
if (
728721
self.linter.config.strict_engineering_notation
729722
and wrong_engineering_notation
730723
) or (wrong_scientific_notation and wrong_engineering_notation):
731724
return raise_bad_float_notation()
732725
elif has_underscore:
733726
# If we have underscore and exponent, we suggest exponent by default
734-
print("\tHas underscore, checking underscore grouping")
735727
if (
736728
self.linter.config.strict_scientific_notation
737729
or self.linter.config.strict_engineering_notation
@@ -740,7 +732,6 @@ def raise_bad_float_notation() -> None:
740732
wrong_underscore_notation = not re.match(
741733
r"^\d{0,3}(_\d{3})*\.?\d*([eE]-?\d{0,3}(_\d{3})*)?$", string
742734
)
743-
print(f"\twrong_underscore_notation:{wrong_underscore_notation}")
744735
if pep515 and wrong_underscore_notation:
745736
return raise_bad_float_notation()
746737
else:

tests/functional/b/bad_float/bad_float_notation_default.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@
6161
actually_nine = 9e0 # [bad-float-notation]
6262
actually_one = 1.0e0 # [bad-float-notation]
6363

64-
65-
6664
hex_constant = 0x1e4 # Hexadecimal, not scientific notation
6765
hex_constant_bad = 0x10e4
6866
binary_constant = 0b1010
@@ -78,6 +76,7 @@
7876
complex_number = 1.5e3 + 2.5e3j # Complex number with scientific notation
7977
# false negative for complex numbers:
8078
complex_number_wrong = 15e4 + 25e7j # [bad-float-notation]
79+
underscore_binary = 0b1010_1010
8180

8281

8382
#+1: [bad-float-notation, bad-float-notation]
@@ -101,34 +100,31 @@ def function_with_sci(param=10.0e4, other_param=20.0e5):
101100
incorrect_sci_uppercase = 1.234_567E6 # [bad-float-notation]
102101
incorrect_sci_underscore_exp = 1.2e1_0 # [bad-float-notation]
103102
invalid_underscore_float = 1_234.567_89 # [bad-float-notation]
104-
invalid_underscore_binary = 0b1010_1010 # [bad-float-notation]
105-
#+1: [bad-float-notation, bad-float-notation]
106-
wrong_big_underscore = 45.3_45e6
107-
#+1: [bad-float-notation, bad-float-notation]
108-
wrong_small_underscore = 0.000_12e-26
109-
#+1: [bad-float-notation, bad-float-notation]
110-
scientific_double_digit_underscore = 1_2e8
111-
#+1: [bad-float-notation, bad-float-notation]
112-
scientific_triple_digit_underscore = 12_3e3
113-
#+1: [bad-float-notation, bad-float-notation]
114-
invalid_underscore_sci = 1_234.567_89e10
103+
wrong_big_underscore = 45.3_45e6 # [bad-float-notation]
104+
wrong_small_underscore = 0.000_12e-26 # [bad-float-notation]
105+
scientific_double_digit_underscore = 1_2e8 # [bad-float-notation]
106+
scientific_triple_digit_underscore = 12_3e3 # [bad-float-notation]
107+
invalid_underscore_sci = 1_234.567_89e10 # [bad-float-notation]
115108
invalid_underscore_sci_exp = 1.2e1_0 # [bad-float-notation]
116-
#+1: [bad-float-notation, bad-float-notation]
117-
invalid_underscore_sci_combined = 1_2.3_4e5_6
118-
#+1: [bad-float-notation, bad-float-notation]
119-
invalid_uppercase_sci = 1_234.567_89E10
120-
edge_underscore_1 = 1_0e6 # [bad-float-notation, bad-float-notation]
109+
invalid_underscore_sci_combined = 1_2.3_4e5_6 # [bad-float-notation]
110+
invalid_uppercase_sci = 1_234.567_89E10 # [bad-float-notation]
111+
edge_underscore_1 = 1_0e6 # [bad-float-notation]
121112
mixed_underscore_1 = 1_000_000.0e-3 # [bad-float-notation]
122-
#+1: [bad-float-notation, bad-float-notation]
123-
mixed_underscore_2 = 0.000_001e3
124-
mixed_underscore_3 = 1_0.0e2 # [bad-float-notation, bad-float-notation]
113+
mixed_underscore_2 = 0.000_001e3 # [bad-float-notation]
114+
mixed_underscore_3 = 1_0.0e2 # [bad-float-notation]
125115

126116
# Complex numbers with underscores
127117
complex_underscore = 1.5_6e3 + 2.5_6e3j # [bad-float-notation]
128-
#+1: [bad-float-notation, bad-float-notation]
129-
complex_underscore_wrong = 15_6e2 + 25_6e2j
118+
complex_underscore_wrong = 15_6e2 + 25_6e2j # [bad-float-notation]
130119

131-
#+2: [bad-float-notation, bad-float-notation]
132120
#+1: [bad-float-notation, bad-float-notation]
133121
def function_with_underscore(param=10.0_0e3, other_param=20.0_0e3):
134122
return param, other_param
123+
124+
int_under_ten = 9
125+
int_under_a_thousand = 998
126+
127+
for i in range(10):
128+
if i < 0:
129+
continue
130+
print("Let's not be really annoying.")
Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,53 @@
1-
bad-float-notation:3:12:None:None::Scientific notation should be '4.53e1' instead:HIGH
2-
bad-float-notation:4:20:None:None::Scientific notation should be '4.53e1' instead:HIGH
3-
bad-float-notation:5:14:None:None::Scientific notation should be '1.2e-4' instead:HIGH
4-
bad-float-notation:6:26:None:None::Scientific notation should be '1.2e-4' instead:HIGH
5-
bad-float-notation:7:26:None:None::Scientific notation should be '1e1' instead:HIGH
6-
bad-float-notation:8:18:None:None::Scientific notation should be '1.1e4' instead:HIGH
7-
bad-float-notation:9:26:None:None::Scientific notation should be '1.2e1' instead:HIGH
8-
bad-float-notation:10:26:None:None::Scientific notation should be '1.23e2' instead:HIGH
9-
bad-float-notation:11:28:None:None::Scientific notation should be '1e-4' instead:HIGH
10-
bad-float-notation:12:26:None:None::Scientific notation should be '1e-4' instead:HIGH
11-
bad-float-notation:13:20:None:None::Scientific notation should be '5e-1' instead:HIGH
12-
bad-float-notation:14:12:None:None::Scientific notation should be '0e0' instead:HIGH
13-
bad-float-notation:47:23:None:None::Scientific notation should be '1.5e1' instead:HIGH
14-
bad-float-notation:51:28:None:None::Scientific notation should be '1e1' instead:HIGH
15-
bad-float-notation:51:48:None:None::Scientific notation should be '2e1' instead:HIGH
16-
bad-float-notation:55:35:None:None::Scientific notation should be '1e1' instead:HIGH
17-
bad-float-notation:55:27:None:None::Scientific notation should be '2e1' instead:HIGH
18-
bad-float-notation:59:29:None:None::Scientific notation should be '1.2300000012345e8' instead:HIGH
19-
bad-float-notation:60:33:None:None::Scientific notation should be '1.2300000012345e8' instead:HIGH
20-
bad-float-notation:65:34:None:None::Non standard grouping of numeric literals using underscores should be .123456:HIGH
21-
bad-float-notation:66:35:None:None::Non standard grouping of numeric literals using underscores should be 123_456.123456:HIGH
22-
bad-float-notation:67:27:None:None::Non standard grouping of numeric literals using underscores should be 1.234567e6:HIGH
23-
bad-float-notation:68:26:None:None::Non standard grouping of numeric literals using underscores should be 1.234567e6:HIGH
24-
bad-float-notation:69:31:None:None::Non standard grouping of numeric literals using underscores should be 1.2e10:HIGH
25-
bad-float-notation:70:27:None:None::Non standard grouping of numeric literals using underscores should be 1_234.56789:HIGH
26-
bad-float-notation:71:28:None:None::Non standard grouping of numeric literals using underscores should be 0_b10_101_010:HIGH
27-
bad-float-notation:73:23:None:None::Non standard grouping of numeric literals using underscores should be 4.5345e7:HIGH
28-
bad-float-notation:73:23:None:None::Scientific notation should be '4.5345e1' instead:HIGH
29-
bad-float-notation:75:25:None:None::Non standard grouping of numeric literals using underscores should be 1.2e-30:HIGH
30-
bad-float-notation:75:25:None:None::Scientific notation should be '1.2e-4' instead:HIGH
31-
bad-float-notation:77:37:None:None::Non standard grouping of numeric literals using underscores should be 1.2e9:HIGH
32-
bad-float-notation:77:37:None:None::Scientific notation should be '1.2e1' instead:HIGH
33-
bad-float-notation:79:37:None:None::Non standard grouping of numeric literals using underscores should be 1.23e5:HIGH
34-
bad-float-notation:79:37:None:None::Scientific notation should be '1.23e2' instead:HIGH
35-
bad-float-notation:81:25:None:None::Non standard grouping of numeric literals using underscores should be 1.23456789e13:HIGH
36-
bad-float-notation:81:25:None:None::Scientific notation should be '1.23456789e3' instead:HIGH
37-
bad-float-notation:82:29:None:None::Non standard grouping of numeric literals using underscores should be 1.2e10:HIGH
38-
bad-float-notation:84:34:None:None::Non standard grouping of numeric literals using underscores should be 1.234e57:HIGH
39-
bad-float-notation:84:34:None:None::Scientific notation should be '1.234e1' instead:HIGH
40-
bad-float-notation:86:24:None:None::Non standard grouping of numeric literals using underscores should be 1.23456789e13:HIGH
41-
bad-float-notation:86:24:None:None::Scientific notation should be '1.23456789e3' instead:HIGH
42-
bad-float-notation:87:20:None:None::Non standard grouping of numeric literals using underscores should be 1e7:HIGH
43-
bad-float-notation:87:20:None:None::Scientific notation should be '1e1' instead:HIGH
44-
bad-float-notation:88:21:None:None::Scientific notation should be '1e6' instead:HIGH
45-
bad-float-notation:90:21:None:None::Non standard grouping of numeric literals using underscores should be 1e-3:HIGH
46-
bad-float-notation:90:21:None:None::Scientific notation should be '1e-6' instead:HIGH
47-
bad-float-notation:91:21:None:None::Non standard grouping of numeric literals using underscores should be 1e3:HIGH
48-
bad-float-notation:91:21:None:None::Scientific notation should be '1e1' instead:HIGH
49-
bad-float-notation:94:21:None:None::Non standard grouping of numeric literals using underscores should be 1.56e3:HIGH
50-
bad-float-notation:96:27:None:None::Non standard grouping of numeric literals using underscores should be 1.56e4:HIGH
51-
bad-float-notation:96:27:None:None::Scientific notation should be '1.56e2' instead:HIGH
52-
bad-float-notation:100:35:None:None::Non standard grouping of numeric literals using underscores should be 1e4:HIGH
53-
bad-float-notation:100:57:None:None::Non standard grouping of numeric literals using underscores should be 2e4:HIGH
54-
bad-float-notation:100:35:None:None::Scientific notation should be '1e1' instead:HIGH
55-
bad-float-notation:100:57:None:None::Scientific notation should be '2e1' instead:HIGH
1+
bad-float-notation:4:19:4:25::float literal should be written as '1.504e8' or '150.4e6' or '150_400_000.0' instead:HIGH
2+
bad-float-notation:10:23:10:36::float literal should be written as '1.23456789e8' or '123.45678899999999e6' or '123_456_789.0' instead:HIGH
3+
bad-float-notation:11:23:11:38::float literal should be written as '1.2345678e+16' or '1.2345678e16' or '12.345678e15' instead:HIGH
4+
bad-float-notation:12:35:12:44::float literal should be written as '1.23456789e8' or '123.45678899999999e6' or '123_456_789.0' instead:HIGH
5+
bad-float-notation:18:33:18:38::float literal should be written as '1.23e6' or '1_230_000.0' instead:HIGH
6+
bad-float-notation:19:38:19:45::float literal should be written as '1.2345e10' or '12.344999999999999e9' or '12_345_000_000.0' instead:HIGH
7+
bad-float-notation:20:35:20:43::float literal should be written as '10_000_000.0' or '10e6' or '1e7' instead:HIGH
8+
bad-float-notation:21:33:21:38::float literal should be written as '9.9e2' or '990.0' instead:HIGH
9+
bad-float-notation:28:37:28:45::float literal should be written as '10_000_000.0' or '10e6' or '1e7' instead:HIGH
10+
bad-float-notation:29:26:29:31::float literal should be written as '9.9' instead:HIGH
11+
bad-float-notation:35:12:35:18::float literal should be written as '4.53e8' or '453_000_000.0' or '453e6' instead:HIGH
12+
bad-float-notation:36:20:36:26::float literal should be written as '4.53e8' or '453_000_000.0' or '453e6' instead:HIGH
13+
bad-float-notation:37:14:37:25::float literal should be written as '1.2e-30' instead:HIGH
14+
bad-float-notation:38:26:38:37::float literal should be written as '1.2e-30' instead:HIGH
15+
bad-float-notation:39:26:39:30::float literal should be written as '1_000_000.0' or '1e6' instead:HIGH
16+
bad-float-notation:40:18:40:26::float literal should be written as '1.1e+31' or '1.1e31' or '11e30' instead:HIGH
17+
bad-float-notation:41:26:41:30::float literal should be written as '1.2e9' or '1_200_000_000.0' instead:HIGH
18+
bad-float-notation:43:28:43:37::float literal should be written as '1e-09' or '1e-9' instead:HIGH
19+
bad-float-notation:44:26:44:34::float literal should be written as '10.0' or '1e1' instead:HIGH
20+
bad-float-notation:45:20:45:26::float literal should be written as '5_000_000_000.0' or '5e9' instead:HIGH
21+
bad-float-notation:46:12:46:16::float literal should be written as '0.0' instead:HIGH
22+
bad-float-notation:60:25:60:30::float literal should be written as '1.5e1' or '15.0' instead:HIGH
23+
bad-float-notation:61:16:61:19::float literal should be written as '9.0' instead:HIGH
24+
bad-float-notation:62:15:62:20::float literal should be written as '1.0' instead:HIGH
25+
bad-float-notation:78:23:78:27::float literal should be written as '1.5e5' or '150_000.0' or '150e3' instead:HIGH
26+
bad-float-notation:83:28:83:34::float literal should be written as '100_000.0' or '100e3' or '1e5' instead:HIGH
27+
bad-float-notation:83:48:83:54::float literal should be written as '2_000_000.0' or '2e6' instead:HIGH
28+
bad-float-notation:87:35:87:41::float literal should be written as '100_000_000.0' or '100e6' or '1e8' instead:HIGH
29+
bad-float-notation:87:27:87:33::float literal should be written as '200_000.0' or '200e3' or '2e5' instead:HIGH
30+
bad-float-notation:91:29:91:57::float literal should be written as 'inf.0' or 'math.inf' instead:HIGH
31+
bad-float-notation:92:33:92:62::float literal should be written as 'inf.0' or 'math.inf' instead:HIGH
32+
bad-float-notation:97:34:97:42::float literal should be written as '0.123456' or '1.23456e-1' or '123.45600000000002e-3' instead:HIGH
33+
bad-float-notation:98:35:98:50::float literal should be written as '1.23456123456e5' or '123.45612345600001e3' or '123_456.123456' instead:HIGH
34+
bad-float-notation:99:27:99:38::float literal should be written as '1.234567e6' or '1_234_567.0' instead:HIGH
35+
bad-float-notation:100:26:100:37::float literal should be written as '1.234567e6' or '1_234_567.0' instead:HIGH
36+
bad-float-notation:101:31:101:38::float literal should be written as '1.2e10' or '12_000_000_000.0' or '12e9' instead:HIGH
37+
bad-float-notation:102:27:102:39::float literal should be written as '1.23456789e3' or '1_234.56789' instead:HIGH
38+
bad-float-notation:103:23:103:32::float literal should be written as '4.5345e7' or '45.345000000000006e6' or '45_345_000.0' instead:HIGH
39+
bad-float-notation:104:25:104:37::float literal should be written as '1.2e-30' instead:HIGH
40+
bad-float-notation:105:37:105:42::float literal should be written as '1.2e9' or '1_200_000_000.0' instead:HIGH
41+
bad-float-notation:106:37:106:43::float literal should be written as '1.23e5' or '123_000.0' or '123e3' instead:HIGH
42+
bad-float-notation:107:25:107:40::float literal should be written as '1.23456789e13' or '12.3456789e12' or '12_345_678_900_000.0' instead:HIGH
43+
bad-float-notation:108:29:108:36::float literal should be written as '1.2e10' or '12_000_000_000.0' or '12e9' instead:HIGH
44+
bad-float-notation:109:34:109:45::float literal should be written as '1.234e+57' or '1.234e57' instead:HIGH
45+
bad-float-notation:110:24:110:39::float literal should be written as '1.23456789e13' or '12.3456789e12' or '12_345_678_900_000.0' instead:HIGH
46+
bad-float-notation:111:20:111:25::float literal should be written as '10_000_000.0' or '10e6' or '1e7' instead:HIGH
47+
bad-float-notation:112:21:112:35::float literal should be written as '1_000.0' or '1e3' instead:HIGH
48+
bad-float-notation:113:21:113:32::float literal should be written as '0.001' or '1e-3' instead:HIGH
49+
bad-float-notation:114:21:114:28::float literal should be written as '1_000.0' or '1e3' instead:HIGH
50+
bad-float-notation:117:21:117:28::float literal should be written as '1.56e3' or '1_560.0' instead:HIGH
51+
bad-float-notation:118:27:118:33::float literal should be written as '1.56e4' or '15.600000000000001e3' or '15_600.0' instead:HIGH
52+
bad-float-notation:121:35:121:43::float literal should be written as '10_000.0' or '10e3' or '1e4' instead:HIGH
53+
bad-float-notation:121:57:121:65::float literal should be written as '20_000.0' or '20e3' or '2e4' instead:HIGH

tests/functional/b/bad_float/bad_float_pep515.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
engineering_notation = 12.345678e15 # [bad-float-notation]
88

99
proper_grouping = 123_456_789
10+
11+
int_under_ten = 9

tests/functional/b/bad_float/bad_float_scientific_notation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
base_between_one_and_ten = 1e4
77
above_threshold_with_exponent = 1e7
88
under_ten = 9.9
9+
int_under_ten = 9

0 commit comments

Comments
 (0)