Skip to content

Commit 9993b1e

Browse files
Handle time better
1 parent 1535830 commit 9993b1e

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

pylint/checkers/format.py

Lines changed: 32 additions & 5 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))),
@@ -694,7 +719,7 @@ def raise_bad_float_notation(reason: str) -> None:
694719
under_threshold # under threshold
695720
and ( # use scientific or engineering notation and under 1/threshold
696721
self.linter.config.strict_underscore_notation
697-
or (abs_value > 1 / self.linter.config.float_notation_threshold)
722+
or (abs_value >= 1 / self.linter.config.float_notation_threshold)
698723
)
699724
)
700725
if not is_written_complexly:
@@ -707,10 +732,12 @@ def raise_bad_float_notation(reason: str) -> None:
707732
1 / threshold
708733
)
709734
threshold = self.to_standard_scientific_notation(threshold)
735+
if under_threshold:
736+
return raise_bad_float_notation(
737+
f"is smaller than {close_to_zero_threshold}"
738+
)
710739
return raise_bad_float_notation(
711-
f"is smaller than {close_to_zero_threshold}"
712-
if under_threshold
713-
else f"is bigger than {threshold}"
740+
f"is bigger than {threshold}", time_suggestion=True
714741
)
715742
if has_exponent:
716743
if self.linter.config.strict_underscore_notation or has_underscore:

tests/functional/b/bad_float/bad_float_notation_default.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,11 @@ def function_with_underscore(param=10.0_0e3, other_param=20.0_0e3):
130130
if i < 0:
131131
continue
132132
print("Let's not be really annoying.")
133+
134+
135+
#+3: [bad-float-notation]
136+
#+3: [bad-float-notation]
137+
#+3: [bad-float-notation]
138+
time_in_s_since_two_week_ago_at_16 = 1180800 # 2 * 24 * 3600 - (8 * 3600)
139+
time_in_s_since_last_year = 31536000 # 365 * 24 * 3600
140+
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
@@ -51,3 +51,6 @@ bad-float-notation:119:21:119:28::'1.5_6e3' has exponent and underscore at the s
5151
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
5252
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
5353
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
54+
bad-float-notation:138:37:138:44::'1180800' 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:139:28:139: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
56+
bad-float-notation:140:29:140: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)