Skip to content

Commit 0e2f2dc

Browse files
Optimize 0
1 parent 7d5672d commit 0e2f2dc

File tree

3 files changed

+45
-36
lines changed

3 files changed

+45
-36
lines changed

pylint/checkers/format.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,11 @@ def raise_bad_float_notation(reason: str) -> None:
670670
confidence=HIGH,
671671
)
672672

673+
if string in {"0", "0.0"}:
674+
# 0 is a special case because it is used very often, and float approximation
675+
# being what they are it needs to be special cased anyway for scientific and
676+
# engineering notation when checking if a number is under 1/threshold
677+
return None
673678
has_underscore = "_" in string
674679
has_exponent = "e" in string or "E" in string
675680
should_be_written_simply = (
@@ -689,10 +694,8 @@ def raise_bad_float_notation(reason: str) -> None:
689694
under_threshold # under threshold
690695
and ( # use scientific or engineering notation and under 1/threshold
691696
self.linter.config.strict_underscore_notation
692-
or (
693-
value == 0
694-
or abs_value > 1 / self.linter.config.float_notation_threshold
695-
)
697+
or abs_value != 0
698+
or abs_value >= 1 / self.linter.config.float_notation_threshold
696699
)
697700
)
698701
if not is_written_complexly:

tests/functional/b/bad_float/bad_float_notation_default.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
zero_before_decimal_big = 0.0001e5 # [bad-float-notation]
4545
negative_decimal = -0.5e10 # [bad-float-notation]
4646
zero_only = 0e10 # [bad-float-notation]
47+
zero_int = 0
48+
zero_float = 0.0
49+
annoying_zero = 00 # [bad-float-notation]
50+
another_annoying_zero = 0. # [bad-float-notation]
4751

4852
one_only = 1e6
4953
correct_1 = 4.53e7

tests/functional/b/bad_float/bad_float_notation_default.txt

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,37 @@ bad-float-notation:43:28:43:37::'0.0001e-5' has a base, '0.0001', that is not be
1919
bad-float-notation:44:26:44:34::'0.0001e5' has underscore or exponent, and it should be written as '10.0' or '1e1' instead:HIGH
2020
bad-float-notation:45:20:45:26::'0.5e10' has a base, '0.5', that is not between 1 and 1000, and it should be written as '5_000_000_000.0' or '5e9' instead:HIGH
2121
bad-float-notation:46:12:46:16::'0e10' has a base, '0.0', that is not between 1 and 1000, and it should be written as '0.0' instead:HIGH
22-
bad-float-notation:60:25:60:30::'1.5e1' has underscore or exponent, and it should be written as '1.5e1' or '15.0' instead:HIGH
23-
bad-float-notation:61:16:61:19::'9e0' has underscore or exponent, and it should be written as '9.0' instead:HIGH
24-
bad-float-notation:62:15:62:20::'1.0e0' has underscore or exponent, and it should be written as '1.0' instead:HIGH
25-
bad-float-notation:78:23:78:27::'15e4' has an exponent '4' that is not a multiple of 3, and it should be written as '1.5e5' or '150_000.0' or '150e3' instead:HIGH
26-
bad-float-notation:83:28:83:34::'10.0e4' has an exponent '4' that is not a multiple of 3, and it should be written as '100_000.0' or '100e3' or '1e5' instead:HIGH
27-
bad-float-notation:83:48:83:54::'20.0e5' has an exponent '5' that is not a multiple of 3, and it should be written as '2_000_000.0' or '2e6' instead:HIGH
28-
bad-float-notation:87:35:87:41::'10.0e7' has an exponent '7' that is not a multiple of 3, and it should be written as '100_000_000.0' or '100e6' or '1e8' instead:HIGH
29-
bad-float-notation:87:27:87:33::'20.0e4' has an exponent '4' that is not a multiple of 3, and it should be written as '200_000.0' or '200e3' or '2e5' instead:HIGH
30-
bad-float-notation:91:29:91:57::'123_000_000.12345e12_000_000' has exponent and underscore at the same time, and it should be written as 'inf.0' or 'math.inf' instead:HIGH
31-
bad-float-notation:92:33:92:62::'123_000_000.12345E123_000_000' has exponent and underscore at the same time, and it should be written as 'inf.0' or 'math.inf' instead:HIGH
32-
bad-float-notation:97:34:97:42::'.123_456' has underscores that are not delimiting packs of three digits, and it should be written as '0.123456' or '1.23456e-1' or '123.45600000000002e-3' instead:HIGH
33-
bad-float-notation:98:35:98: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.45612345600001e3' or '123_456.123456' instead:HIGH
34-
bad-float-notation:99:27:99: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
35-
bad-float-notation:100:26:100: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
36-
bad-float-notation:101:31:101:38::'1.2e1_0' has exponent and underscore at the same time, and it should be written as '1.2e10' or '12_000_000_000.0' or '12e9' instead:HIGH
37-
bad-float-notation:102:27:102: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.56789' instead:HIGH
38-
bad-float-notation:103:23:103:32::'45.3_45e6' has exponent and underscore at the same time, and it should be written as '4.5345e7' or '45.345000000000006e6' or '45_345_000.0' instead:HIGH
39-
bad-float-notation:104:25:104:37::'0.000_12e-26' has exponent and underscore at the same time, and it should be written as '1.2e-30' instead:HIGH
40-
bad-float-notation:105:37:105: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
41-
bad-float-notation:106:37:106:43::'12_3e3' has exponent and underscore at the same time, and it should be written as '1.23e5' or '123_000.0' or '123e3' instead:HIGH
42-
bad-float-notation:107:25:107: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
43-
bad-float-notation:108:29:108:36::'1.2e1_0' has exponent and underscore at the same time, and it should be written as '1.2e10' or '12_000_000_000.0' or '12e9' instead:HIGH
44-
bad-float-notation:109:34:109:45::'1_2.3_4e5_6' has exponent and underscore at the same time, and it should be written as '1.234e+57' or '1.234e57' instead:HIGH
45-
bad-float-notation:110:24:110: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
46-
bad-float-notation:111:20:111:25::'1_0e6' has exponent and underscore at the same time, and it should be written as '10_000_000.0' or '10e6' or '1e7' instead:HIGH
47-
bad-float-notation:112:21:112:35::'1_000_000.0e-3' has exponent and underscore at the same time, and it should be written as '1_000.0' or '1e3' instead:HIGH
48-
bad-float-notation:113:21:113:32::'0.000_001e3' has exponent and underscore at the same time, and it should be written as '0.001' or '1e-3' instead:HIGH
49-
bad-float-notation:114:21:114:28::'1_0.0e2' has exponent and underscore at the same time, and it should be written as '1_000.0' or '1e3' instead:HIGH
50-
bad-float-notation:117:21:117: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
51-
bad-float-notation:118:27:118:33::'15_6e2' has exponent and underscore at the same time, and it should be written as '1.56e4' or '15.600000000000001e3' or '15_600.0' instead:HIGH
52-
bad-float-notation:121:35:121:43::'10.0_0e3' has exponent and underscore at the same time, and it should be written as '10_000.0' or '10e3' or '1e4' instead:HIGH
53-
bad-float-notation:121:57:121:65::'20.0_0e3' has exponent and underscore at the same time, and it should be written as '20_000.0' or '20e3' or '2e4' instead:HIGH
22+
bad-float-notation:49:16:49:18::'00' is smaller than 1e-6, and it should be written as '0.0' instead:HIGH
23+
bad-float-notation:50:24:50:26::'0.' is smaller than 1e-6, and it should be written as '0.0' instead:HIGH
24+
bad-float-notation:64:25:64:30::'1.5e1' has underscore or exponent, and it should be written as '1.5e1' or '15.0' instead:HIGH
25+
bad-float-notation:65:16:65:19::'9e0' has underscore or exponent, and it should be written as '9.0' instead:HIGH
26+
bad-float-notation:66:15:66:20::'1.0e0' has underscore or exponent, and it should be written as '1.0' instead:HIGH
27+
bad-float-notation:82:23:82:27::'15e4' has an exponent '4' that is not a multiple of 3, and it should be written as '1.5e5' or '150_000.0' or '150e3' instead:HIGH
28+
bad-float-notation:87:28:87:34::'10.0e4' has an exponent '4' that is not a multiple of 3, and it should be written as '100_000.0' or '100e3' or '1e5' instead:HIGH
29+
bad-float-notation:87:48:87:54::'20.0e5' has an exponent '5' that is not a multiple of 3, and it should be written as '2_000_000.0' or '2e6' instead:HIGH
30+
bad-float-notation:91:35:91:41::'10.0e7' has an exponent '7' that is not a multiple of 3, and it should be written as '100_000_000.0' or '100e6' or '1e8' instead:HIGH
31+
bad-float-notation:91:27:91:33::'20.0e4' has an exponent '4' that is not a multiple of 3, and it should be written as '200_000.0' or '200e3' or '2e5' instead:HIGH
32+
bad-float-notation:95:29:95:57::'123_000_000.12345e12_000_000' has exponent and underscore at the same time, and it should be written as 'inf.0' or 'math.inf' instead:HIGH
33+
bad-float-notation:96:33:96:62::'123_000_000.12345E123_000_000' has exponent and underscore at the same time, and it should be written as 'inf.0' or 'math.inf' instead:HIGH
34+
bad-float-notation:101:34:101:42::'.123_456' has underscores that are not delimiting packs of three digits, and it should be written as '0.123456' or '1.23456e-1' or '123.45600000000002e-3' instead:HIGH
35+
bad-float-notation:102:35:102: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.45612345600001e3' or '123_456.123456' instead:HIGH
36+
bad-float-notation:103:27:103: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
37+
bad-float-notation:104:26:104: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
38+
bad-float-notation:105:31:105:38::'1.2e1_0' has exponent and underscore at the same time, and it should be written as '1.2e10' or '12_000_000_000.0' or '12e9' instead:HIGH
39+
bad-float-notation:106:27:106: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.56789' instead:HIGH
40+
bad-float-notation:107:23:107:32::'45.3_45e6' has exponent and underscore at the same time, and it should be written as '4.5345e7' or '45.345000000000006e6' or '45_345_000.0' instead:HIGH
41+
bad-float-notation:108:25:108:37::'0.000_12e-26' has exponent and underscore at the same time, and it should be written as '1.2e-30' instead:HIGH
42+
bad-float-notation:109:37:109: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
43+
bad-float-notation:110:37:110:43::'12_3e3' has exponent and underscore at the same time, and it should be written as '1.23e5' or '123_000.0' or '123e3' instead:HIGH
44+
bad-float-notation:111:25:111: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
45+
bad-float-notation:112:29:112:36::'1.2e1_0' has exponent and underscore at the same time, and it should be written as '1.2e10' or '12_000_000_000.0' or '12e9' instead:HIGH
46+
bad-float-notation:113:34:113:45::'1_2.3_4e5_6' has exponent and underscore at the same time, and it should be written as '1.234e+57' or '1.234e57' instead:HIGH
47+
bad-float-notation:114:24:114: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
48+
bad-float-notation:115:20:115:25::'1_0e6' has exponent and underscore at the same time, and it should be written as '10_000_000.0' or '10e6' or '1e7' instead:HIGH
49+
bad-float-notation:116:21:116:35::'1_000_000.0e-3' has exponent and underscore at the same time, and it should be written as '1_000.0' or '1e3' instead:HIGH
50+
bad-float-notation:117:21:117:32::'0.000_001e3' has exponent and underscore at the same time, and it should be written as '0.001' or '1e-3' instead:HIGH
51+
bad-float-notation:118:21:118:28::'1_0.0e2' has exponent and underscore at the same time, and it should be written as '1_000.0' or '1e3' instead:HIGH
52+
bad-float-notation:121:21:121: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
53+
bad-float-notation:122:27:122:33::'15_6e2' has exponent and underscore at the same time, and it should be written as '1.56e4' or '15.600000000000001e3' or '15_600.0' instead:HIGH
54+
bad-float-notation:125:35:125:43::'10.0_0e3' has exponent and underscore at the same time, and it should be written as '10_000.0' or '10e3' or '1e4' instead:HIGH
55+
bad-float-notation:125:57:125:65::'20.0_0e3' has exponent and underscore at the same time, and it should be written as '20_000.0' or '20e3' or '2e4' instead:HIGH

0 commit comments

Comments
 (0)