Skip to content

Commit 5441deb

Browse files
Refactor the check
1 parent 77a3d6b commit 5441deb

File tree

1 file changed

+48
-34
lines changed

1 file changed

+48
-34
lines changed

pylint/checkers/format.py

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ def process_tokens(self, tokens: list[tokenize.TokenInfo]) -> None:
548548
self.add_message("trailing-newlines", line=line_num)
549549

550550
@classmethod
551-
def to_standard_or_engineering_base(cls, number):
551+
def to_standard_or_engineering_base(cls, number: float) -> tuple[str, str]:
552552
"""Calculate scientific notation components (base, exponent) for a number.
553553
554554
Returns a tuple (base, exponent) where:
@@ -611,13 +611,11 @@ def to_standard_underscore_grouping(cls, number: float) -> str:
611611
# not use exponential notation for very small number even for strict
612612
# underscore grouping notation
613613
return number_str
614-
print(number_str)
615614
if "." in number_str:
616615
int_part, dec_part = number_str.split(".")
617616
else:
618617
int_part = number_str
619618
dec_part = "0"
620-
print("int_part", int_part, "dec_part:", dec_part)
621619
grouped_int_part = ""
622620
for i, digit in enumerate(reversed(int_part)):
623621
if i > 0 and i % 3 == 0:
@@ -628,62 +626,78 @@ def to_standard_underscore_grouping(cls, number: float) -> str:
628626
def _check_bad_float_notation(
629627
self, line_num: int, start: tuple[int, int], string: str
630628
) -> None:
629+
630+
def raise_bad_float_notation(suggested: set[str]) -> None:
631+
return self.add_message(
632+
"bad-float-notation",
633+
args=("' or '".join(suggested)),
634+
line=line_num,
635+
col_offset=start[1],
636+
confidence=HIGH,
637+
)
638+
631639
value = float(string)
632640
threshold = self.linter.config.float_notation_threshold
633641
abs_value = abs(value)
634642
has_underscore = "_" in string
635643
has_exponent = "e" in string or "E" in string
636-
violation = False
637644
if has_exponent:
638645
if self.linter.config.strict_underscore_notation:
639646
# If we have exponent it means it's not proper underscore
640-
violation = True
641-
suggested = self.to_standard_underscore_grouping(value)
642-
elif abs_value > threshold or abs_value < 1 / threshold:
647+
return raise_bad_float_notation(
648+
{self.to_standard_underscore_grouping(value)}
649+
)
650+
if abs_value > threshold or abs_value < 1 / threshold:
643651
value_as_str, exponent_as_str = string.lower().split("e")
644652
value = float(value_as_str)
645653
wrong_scientific_notation = (
646654
self.should_check_scientific_notation and not (1 <= value < 10)
647655
)
648-
wrong_engineering_notation = (
649-
self.should_check_engineering_notation
650-
and not (1 <= value < 1000 and int(exponent_as_str) % 3 == 0)
651-
)
652656
if (
653657
self.linter.config.strict_scientific_notation
654658
and wrong_scientific_notation
655659
):
656-
suggested = self.to_standard_scientific_notation(value)
660+
return raise_bad_float_notation(
661+
{self.to_standard_scientific_notation(value)}
662+
)
663+
wrong_engineering_notation = (
664+
self.should_check_engineering_notation
665+
and not (1 <= value < 1000 and int(exponent_as_str) % 3 == 0)
666+
)
657667
if (
658668
self.linter.config.strict_engineering_notation
659669
and wrong_engineering_notation
660670
):
661-
suggested = self.to_standard_engineering_notation(value)
671+
return raise_bad_float_notation(
672+
{self.to_standard_engineering_notation(value)}
673+
)
662674
if wrong_scientific_notation and wrong_engineering_notation:
663-
suggested = f"{self.to_standard_scientific_notation(value)} or {self.to_standard_engineering_notation(value)}"
664-
elif has_underscore:
675+
return raise_bad_float_notation(
676+
{
677+
self.to_standard_scientific_notation(value),
678+
self.to_standard_engineering_notation(value),
679+
self.to_standard_underscore_grouping(value),
680+
}
681+
)
682+
if has_underscore:
683+
# If we have underscore and exponent, we suggest exponent by default
665684
if self.linter.config.strict_scientific_notation:
666-
violation = True
667-
suggested = self.to_standard_scientific_notation(value)
668-
elif self.linter.config.strict_engineering_notation:
669-
violation = True
670-
suggested = self.to_standard_engineering_notation(value)
671-
else:
672-
# If we have underscore and exponent, we suggest exponent by default
673-
underscore_notation = has_underscore and re.match(
674-
r"^\d{0,3}(_\d{3})*\.?\d*([eE]-?\d{0,3}(_\d{3})*)?$", string
685+
return raise_bad_float_notation(
686+
{self.to_standard_scientific_notation(value)}
675687
)
676-
suggested = self.to_standard_underscore_grouping(string)
677-
# if "e" in number.lower() or "E" in number.lower():
678-
# return FormatChecker.to_standard_scientific_notation(float(number))
679-
if violation:
680-
self.add_message(
681-
"bad-float-notation",
682-
args=(suggested),
683-
line=line_num,
684-
col_offset=start[1],
685-
confidence=HIGH,
688+
if self.linter.config.strict_engineering_notation:
689+
return raise_bad_float_notation(
690+
{self.to_standard_engineering_notation(value)}
691+
)
692+
underscore_notation = has_underscore and re.match(
693+
r"^\d{0,3}(_\d{3})*\.?\d*([eE]-?\d{0,3}(_\d{3})*)?$", string
686694
)
695+
if not underscore_notation:
696+
return raise_bad_float_notation(
697+
{self.to_standard_underscore_grouping(value)}
698+
)
699+
return None
700+
return None
687701

688702
def _check_line_ending(self, line_ending: str, line_num: int) -> None:
689703
# check if line endings are mixed

0 commit comments

Comments
 (0)