Skip to content

Commit afc04e3

Browse files
Handle the case where the number is a number of hours in second better
1 parent 5374940 commit afc04e3

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

pylint/checkers/format.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,57 @@ def to_standard_scientific_notation(cls, number: float) -> str:
112112

113113
@classmethod
114114
def to_understandable_time(cls, number: float) -> str:
115-
if number == 0.0 or number % 3600 != 0 or number // 3600 == 1:
116-
return "" # Not a suspected time
117-
parts: list[int] = [3600]
115+
if (
116+
number % 3600 != 0 # not a multiple of the number of second in an hour
117+
or number == 0.0 # 0 is congruent to 3600, but not a time
118+
):
119+
return ""
120+
if number // 3600 == 1:
121+
# If we have to advise a proper time for 3600, it means the threshold
122+
# was under 3600, so we advise even more decomposition
123+
return "60 * 60"
124+
parts: list[int | str] = [3600]
118125
number //= 3600
119126
for divisor in (24, 7, 365):
120127
if number % divisor == 0:
121128
parts.append(divisor)
122129
number //= divisor
123130
remainder = int(number)
131+
days_to_add = None
132+
hours_to_add = None
133+
exp = 0
134+
while remainder > 1 and remainder % 1000 == 0:
135+
# suspected watt hour remove prior to decomposition
136+
remainder //= 1000
137+
exp += 3
138+
if exp:
139+
parts.append(f"1e{exp}")
140+
else:
141+
days = int(remainder // 24)
142+
hours_to_remove = remainder % 24
143+
print(remainder, days, hours_to_remove)
144+
if days > 1 and hours_to_remove != 0:
145+
parts += [24]
146+
hours_to_add = f" + ({hours_to_remove} * 3600)"
147+
remainder -= hours_to_remove
148+
remainder //= 24
149+
year = int(remainder // 365)
150+
days_to_remove = remainder % 365
151+
if year > 1 and days_to_remove != 0:
152+
parts += [365]
153+
days_to_add = f" + ({days_to_remove} * 3600 * 24)"
154+
remainder -= days_to_remove
155+
remainder //= 365
124156
if remainder != 1:
125157
parts.append(remainder)
126-
return " * ".join([str(p) for p in parts])
158+
result = " * ".join([str(p) for p in parts])
159+
if days_to_add:
160+
result = f"({result}){days_to_add}"
161+
if hours_to_add:
162+
result += hours_to_add
163+
elif hours_to_add:
164+
result = f"({result}){hours_to_add}"
165+
return result
127166

128167
@classmethod
129168
def to_standard_engineering_notation(cls, number: float) -> str:

tests/checkers/unittest_format.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,18 @@ def test_to_another_standard_notation(
250250
@pytest.mark.parametrize(
251251
"value,expected",
252252
[
253-
(3600, ""),
254-
(86400, "3600 * 24"),
255-
(604800, "3600 * 24 * 7"),
256-
(2592000, "3600 * 24 * 30"),
257-
(31536000, "3600 * 24 * 365"),
258-
(1180800, "3600 * 24 * 14 - (8 * 3600)"),
259-
(315360000, "3600 * 24 * 365 * 10"),
253+
(3600, "60 * 60"),
254+
(3600 * 1e12, "3600 * 1e12"),
255+
(3600 * 1e6 * 24, "3600 * 24 * 1e6"),
256+
(3600 * 24, "3600 * 24"),
257+
(3600 * 24 * 7, "3600 * 24 * 7"),
258+
(3600 * 24 * 30, "3600 * 24 * 30"),
259+
(3600 * 24 * 365, "3600 * 24 * 365"),
260+
(3600 * 24 * 14 - (8 * 3600), "(3600 * 24 * 13) + (16 * 3600)"),
261+
(3600 * 24 * 365 * 10, "3600 * 24 * 365 * 10"),
262+
(428182 * 3600, "(3600 * 24 * 365 * 48) + (320 * 3600 * 24) + (22 * 3600)"),
260263
],
261264
)
262265
def test_to_understandable_time(value: int, expected: str) -> None:
263266
actual = FloatFormatterHelper.to_understandable_time(value)
264-
assert actual == expected, f"Expected {expected} for {value}, got {actual}"
267+
assert actual == expected, f"Expected {expected!r} for {value!r}, got {actual!r}"

tests/functional/b/bad_float/bad_float_notation_default.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ bad-float-notation:122:21:122:28::'1.5_6e3' has exponent and underscore at the s
5252
bad-float-notation:123:27:123: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
5353
bad-float-notation:126:35:126: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
5454
bad-float-notation:126:57:126: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
55-
bad-float-notation:141:37:141:46::'1180800.0' is bigger than 1e6, and it should be written as '1.1808e6' or '1_180_800.0' or '3600 * 328' instead:HIGH
55+
bad-float-notation:141:37:141:46::'1180800.0' is bigger than 1e6, and it should be written as '(3600 * 24 * 13) + (16 * 3600)' or '1.1808e6' or '1_180_800.0' instead:HIGH
5656
bad-float-notation:142:28:142:38::'31536000.0' is bigger than 1e6, and it should be written as '3.1536e7' or '31.536e6' or '31_536_000.0' or '3600 * 24 * 365' instead:HIGH
5757
bad-float-notation:143:29:143:38::'2592000.0' is bigger than 1e6, and it should be written as '2.592e6' or '2_592_000.0' or '3600 * 24 * 30' instead:HIGH

0 commit comments

Comments
 (0)