Skip to content

Commit 1535830

Browse files
Optimize 0
1 parent 7d5672d commit 1535830

File tree

3 files changed

+40
-36
lines changed

3 files changed

+40
-36
lines changed

pylint/checkers/format.py

Lines changed: 6 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,7 @@ 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 > 1 / self.linter.config.float_notation_threshold)
696698
)
697699
)
698700
if not is_written_complexly:

tests/functional/b/bad_float/bad_float_notation_default.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
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
4749

4850
one_only = 1e6
4951
correct_1 = 4.53e7

tests/functional/b/bad_float/bad_float_notation_default.txt

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,35 @@ 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:62:25:62:30::'1.5e1' has underscore or exponent, and it should be written as '1.5e1' or '15.0' instead:HIGH
23+
bad-float-notation:63:16:63:19::'9e0' has underscore or exponent, and it should be written as '9.0' instead:HIGH
24+
bad-float-notation:64:15:64:20::'1.0e0' has underscore or exponent, and it should be written as '1.0' instead:HIGH
25+
bad-float-notation:80:23:80: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:85:28:85: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:85:48:85: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:89:35:89: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:89:27:89: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:93:29:93: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:94:33:94: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:99:34:99: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:100:35:100: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:101:27:101: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:102:26:102: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:103:31:103: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:104:27:104: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:105:23:105: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:106:25:106: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:107:37:107: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:108:37:108: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:109:25:109: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:110:29:110: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:111:34:111: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:112:24:112: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:113:20:113: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:114:21:114: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:115:21:115: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:116:21:116: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:119:21:119: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:120:27:120: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:123:35:123: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:123:57:123: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)