@@ -74,6 +74,13 @@ class CodeStyleChecker(BaseChecker):
74
74
"default_enabled" : False ,
75
75
},
76
76
),
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
+ ),
77
84
}
78
85
options = (
79
86
(
@@ -101,14 +108,35 @@ def open(self) -> None:
101
108
or self .linter .config .max_line_length
102
109
)
103
110
104
- @only_required_for_messages ("prefer-typing-namedtuple" )
111
+ @only_required_for_messages ("prefer-typing-namedtuple" , "use-math-not-float" )
105
112
def visit_call (self , node : nodes .Call ) -> None :
106
113
if self ._py36_plus :
107
114
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" :
109
118
self .add_message (
110
119
"prefer-typing-namedtuple" , node = node , confidence = INFERENCE
111
120
)
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
+ minus = (
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 = (minus , "nan" if is_nan else "inf" , node .as_string ()),
138
+ confidence = INFERENCE ,
139
+ )
112
140
113
141
@only_required_for_messages ("consider-using-namedtuple-or-dataclass" )
114
142
def visit_dict (self , node : nodes .Dict ) -> None :
0 commit comments