@@ -548,7 +548,7 @@ def process_tokens(self, tokens: list[tokenize.TokenInfo]) -> None:
548
548
self .add_message ("trailing-newlines" , line = line_num )
549
549
550
550
@classmethod
551
- def to_standard_or_engineering_base (cls , number ) :
551
+ def to_standard_or_engineering_base (cls , number : float ) -> tuple [ str , str ] :
552
552
"""Calculate scientific notation components (base, exponent) for a number.
553
553
554
554
Returns a tuple (base, exponent) where:
@@ -611,13 +611,11 @@ def to_standard_underscore_grouping(cls, number: float) -> str:
611
611
# not use exponential notation for very small number even for strict
612
612
# underscore grouping notation
613
613
return number_str
614
- print (number_str )
615
614
if "." in number_str :
616
615
int_part , dec_part = number_str .split ("." )
617
616
else :
618
617
int_part = number_str
619
618
dec_part = "0"
620
- print ("int_part" , int_part , "dec_part:" , dec_part )
621
619
grouped_int_part = ""
622
620
for i , digit in enumerate (reversed (int_part )):
623
621
if i > 0 and i % 3 == 0 :
@@ -628,62 +626,78 @@ def to_standard_underscore_grouping(cls, number: float) -> str:
628
626
def _check_bad_float_notation (
629
627
self , line_num : int , start : tuple [int , int ], string : str
630
628
) -> 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
+
631
639
value = float (string )
632
640
threshold = self .linter .config .float_notation_threshold
633
641
abs_value = abs (value )
634
642
has_underscore = "_" in string
635
643
has_exponent = "e" in string or "E" in string
636
- violation = False
637
644
if has_exponent :
638
645
if self .linter .config .strict_underscore_notation :
639
646
# 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 :
643
651
value_as_str , exponent_as_str = string .lower ().split ("e" )
644
652
value = float (value_as_str )
645
653
wrong_scientific_notation = (
646
654
self .should_check_scientific_notation and not (1 <= value < 10 )
647
655
)
648
- wrong_engineering_notation = (
649
- self .should_check_engineering_notation
650
- and not (1 <= value < 1000 and int (exponent_as_str ) % 3 == 0 )
651
- )
652
656
if (
653
657
self .linter .config .strict_scientific_notation
654
658
and wrong_scientific_notation
655
659
):
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
+ )
657
667
if (
658
668
self .linter .config .strict_engineering_notation
659
669
and wrong_engineering_notation
660
670
):
661
- suggested = self .to_standard_engineering_notation (value )
671
+ return raise_bad_float_notation (
672
+ {self .to_standard_engineering_notation (value )}
673
+ )
662
674
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
665
684
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 )}
675
687
)
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
686
694
)
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
687
701
688
702
def _check_line_ending (self , line_ending : str , line_num : int ) -> None :
689
703
# check if line endings are mixed
0 commit comments