Skip to content

Commit 35898de

Browse files
Handle time better
1 parent 0e2f2dc commit 35898de

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

pylint/checkers/format.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,25 @@ def to_standard_scientific_notation(cls, number: float) -> str:
586586
return base
587587
return f"{base}.0"
588588

589+
@classmethod
590+
def to_understandable_time(cls, number: float) -> str:
591+
if number % 3600 != 0:
592+
return "" # Not a suspected time
593+
parts: list[int] = [3600]
594+
number //= 3600
595+
for divisor in (
596+
24,
597+
7,
598+
365,
599+
):
600+
if number % divisor == 0:
601+
parts.append(divisor)
602+
number //= divisor
603+
remainder = int(number)
604+
if remainder != 1:
605+
parts.append(remainder)
606+
return " * ".join([str(p) for p in parts])
607+
589608
@classmethod
590609
def to_standard_engineering_notation(cls, number: float) -> str:
591610
base, exp = cls.to_standard_or_engineering_base(number)
@@ -652,14 +671,20 @@ def _check_bad_float_notation( # pylint: disable=too-many-locals
652671
or self.linter.config.strict_underscore_notation
653672
)
654673

655-
def raise_bad_float_notation(reason: str) -> None:
674+
def raise_bad_float_notation(
675+
reason: str, time_suggestion: bool = False
676+
) -> None:
656677
suggested = set()
657678
if scientific:
658679
suggested.add(self.to_standard_scientific_notation(value))
659680
if engineering:
660681
suggested.add(self.to_standard_engineering_notation(value))
661682
if pep515:
662683
suggested.add(self.to_standard_underscore_grouping(value))
684+
if time_suggestion:
685+
maybe_a_time = self.to_understandable_time(value)
686+
if maybe_a_time:
687+
suggested.add(maybe_a_time)
663688
return self.add_message(
664689
"bad-float-notation",
665690
args=(string, reason, "' or '".join(sorted(suggested))),
@@ -708,10 +733,12 @@ def raise_bad_float_notation(reason: str) -> None:
708733
1 / threshold
709734
)
710735
threshold = self.to_standard_scientific_notation(threshold)
736+
if under_threshold:
737+
return raise_bad_float_notation(
738+
f"is smaller than {close_to_zero_threshold}"
739+
)
711740
return raise_bad_float_notation(
712-
f"is smaller than {close_to_zero_threshold}"
713-
if under_threshold
714-
else f"is bigger than {threshold}"
741+
f"is bigger than {threshold}", time_suggestion=True
715742
)
716743
if has_exponent:
717744
if self.linter.config.strict_underscore_notation or has_underscore:

tests/checkers/unittest_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def test_disable_global_option_end_of_line() -> None:
213213
("20e-9", "2e-8", "20e-9", "2e-08"), # 2e-08 is what python offer on str(float)
214214
(
215215
# 15 significant digits because we get rounding error otherwise
216-
# and 15 seems enough especially since we don't autofix
216+
# and 15 seems enough especially since we don't auto-fix
217217
"10_5415_456_465498.16354698489",
218218
"1.05415456465498e14",
219219
"105.415456465498e12",

tests/functional/b/bad_float/bad_float_notation_default.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,11 @@ def function_with_underscore(param=10.0_0e3, other_param=20.0_0e3):
132132
if i < 0:
133133
continue
134134
print("Let's not be really annoying.")
135+
136+
137+
#+3: [bad-float-notation]
138+
#+3: [bad-float-notation]
139+
#+3: [bad-float-notation]
140+
time_in_s_since_two_week_ago_at_16 = 1180800 # 2 * 24 * 3600 - (8 * 3600)
141+
time_in_s_since_last_year = 31536000 # 365 * 24 * 3600
142+
time_in_s_since_last_month = 2592000 # 30 * 24 * 3600

tests/functional/b/bad_float/bad_float_notation_default.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ bad-float-notation:121:21:121:28::'1.5_6e3' has exponent and underscore at the s
5353
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
5454
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
5555
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
56+
bad-float-notation:140:37:140:44::'1180800' is bigger than 1e6, and it should be written as '1.1808e6' or '1_180_800.0' or '3600 * 328' instead:HIGH
57+
bad-float-notation:141:28:141:36::'31536000' 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
58+
bad-float-notation:142:29:142:36::'2592000' 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)