@@ -74,6 +74,13 @@ class CodeStyleChecker(BaseChecker):
7474 "default_enabled" : False ,
7575 },
7676 ),
77+ "R6106" : (
78+ "%smath.%s is preferable to %s" ,
79+ "use-math-not-float" ,
80+ "Using math.inf or math.nan permits to benefit from typing "
81+ "and it is 4 time faster than a float call.\n "
82+ "Requires 'import math' in the file." ,
83+ ),
7784 }
7885 options = (
7986 (
@@ -101,14 +108,35 @@ def open(self) -> None:
101108 or self .linter .config .max_line_length
102109 )
103110
104- @only_required_for_messages ("prefer-typing-namedtuple" )
111+ @only_required_for_messages ("prefer-typing-namedtuple" , "use-math-not-float" )
105112 def visit_call (self , node : nodes .Call ) -> None :
106113 if self ._py36_plus :
107114 called = safe_infer (node .func )
108- if called and called .qname () == "collections.namedtuple" :
115+ if not called :
116+ return
117+ if called .qname () == "collections.namedtuple" :
109118 self .add_message (
110119 "prefer-typing-namedtuple" , node = node , confidence = INFERENCE
111120 )
121+ elif called .qname () == "builtins.float" :
122+ if (
123+ node .args
124+ and isinstance (node .args [0 ], nodes .Const )
125+ and isinstance (node .args [0 ].value , str )
126+ and any (
127+ c .isalpha () and c .lower () != "e" for c in node .args [0 ].value
128+ )
129+ ):
130+ is_nan = "nan" in node .args [0 ].value .lower ()
131+ suggestion = (
132+ "-" if not is_nan and node .args [0 ].value .startswith ("-" ) else ""
133+ )
134+ self .add_message (
135+ "use-math-not-float" ,
136+ node = node ,
137+ args = (suggestion , "nan" if is_nan else "inf" , node .as_string ()),
138+ confidence = INFERENCE ,
139+ )
112140
113141 @only_required_for_messages ("consider-using-namedtuple-or-dataclass" )
114142 def visit_dict (self , node : nodes .Dict ) -> None :
0 commit comments