Skip to content

Commit be11be6

Browse files
Make the message less prescriptive as the import need to be taken into account
1 parent bda3d30 commit be11be6

File tree

12 files changed

+77
-37
lines changed

12 files changed

+77
-37
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
swag = float("inf") # [consider-math-not-float]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
This is an extension check because the typing advantage could be fixed.
2+
3+
Regarding performance, float("nan") and float("inf") are slower than their counterpart math.inf and math.nan by a factor of 4 after the initial import of math.
4+
5+
.. code-block:: python
6+
7+
import math
8+
import timeit
9+
10+
time_math_inf = timeit.timeit('math.nan', globals=globals(), number=10**8)
11+
print(f'math.nan: {time_math_inf:.2f} seconds')
12+
13+
import timeit
14+
time_inf_str = timeit.timeit('float("nan")', number=10**8)
15+
print(f'float("nan"): {time_inf_str:.2f} seconds')
16+
17+
Result::
18+
19+
math.nan: 1.24 seconds
20+
float("nan"): 5.15 seconds
21+
22+
But if we take the initial import into account it's worse.
23+
24+
.. code-block:: python
25+
26+
import timeit
27+
28+
time_math_inf = timeit.timeit('import math;math.nan', globals=globals(), number=10**8)
29+
print(f'math.nan: {time_math_inf:.2f} seconds')
30+
31+
import timeit
32+
time_inf_str = timeit.timeit('float("nan")', number=10**8)
33+
print(f'float("nan"): {time_inf_str:.2f} seconds')
34+
35+
Result::
36+
37+
math.nan: 9.08 seconds
38+
float("nan"): 5.33 seconds
39+
40+
So the decision depends on how and how often you need to use it and what matter to you.
File renamed without changes.
File renamed without changes.

doc/data/messages/u/use-math-not-float/bad.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

doc/user_guide/checkers/extensions.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ Code Style checker Messages
8888
'typing.NamedTuple' uses the well-known 'class' keyword with type-hints for
8989
readability (it's also faster as it avoids an internal exec call). Disabled
9090
by default!
91-
:use-math-not-float (R6106): *%smath.%s is preferable to %s*
91+
:consider-math-not-float (R6106): *%smath.%s is preferable to %s*
9292
Using math.inf or math.nan permits to benefit from typing and it is 4 time
93-
faster than a float call (notwithstanding the initial import of math).
93+
faster than a float call (after the initial import of math).
9494

9595

9696
.. _pylint.extensions.comparison_placement:

doc/user_guide/messages/messages_overview.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ All messages in the refactor category:
491491
refactor/condition-evals-to-constant
492492
refactor/confusing-consecutive-elif
493493
refactor/consider-alternative-union-syntax
494+
refactor/consider-math-not-float
494495
refactor/consider-merging-isinstance
495496
refactor/consider-refactoring-into-while-condition
496497
refactor/consider-swap-variables
@@ -559,7 +560,6 @@ All messages in the refactor category:
559560
refactor/use-a-generator
560561
refactor/use-dict-literal
561562
refactor/use-list-literal
562-
refactor/use-math-not-float
563563
refactor/use-set-for-membership
564564
refactor/use-yield-from
565565
refactor/useless-object-inheritance

doc/whatsnew/fragments/10621.new_check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Add a ``use-math-not-float`` message. ``float("nan")`` and ``float("inf")`` are slower
1+
Add a ``consider-math-not-float`` message. ``float("nan")`` and ``float("inf")`` are slower
22
than their counterpart ``math.inf`` and ``math.nan`` by a factor of 4 (notwithstanding
33
the initial import of math) and they are also not well typed when using mypy.
44

pylint/extensions/code_style.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ class CodeStyleChecker(BaseChecker):
7777
),
7878
"R6106": (
7979
"%smath.%s is preferable to %s",
80-
"use-math-not-float",
80+
"consider-math-not-float",
8181
"Using math.inf or math.nan permits to benefit from typing "
82-
"and it is 4 time faster than a float call (notwithstanding"
82+
"and it is 4 time faster than a float call (after"
8383
" the initial import of math).",
8484
),
8585
}
@@ -109,7 +109,7 @@ def open(self) -> None:
109109
or self.linter.config.max_line_length
110110
)
111111

112-
@only_required_for_messages("prefer-typing-namedtuple", "use-math-not-float")
112+
@only_required_for_messages("prefer-typing-namedtuple", "consider-math-not-float")
113113
def visit_call(self, node: nodes.Call) -> None:
114114
if self._py36_plus:
115115
called = safe_infer(node.func)
@@ -140,7 +140,7 @@ def visit_call(self, node: nodes.Call) -> None:
140140
)[0]
141141
minus = "-" if math_call == "inf" and value.startswith("-") else ""
142142
self.add_message(
143-
"use-math-not-float",
143+
"consider-math-not-float",
144144
node=node,
145145
args=(minus, math_call, node.as_string()),
146146
confidence=INFERENCE,
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
"""Functional test for use-math-not-float."""
1+
"""Functional test for consider-math-not-float."""
22

3-
inf_float = float("inf") # [use-math-not-float]
4-
neg_inf_float = float('-inf') # [use-math-not-float]
5-
pos_inf_float = float("+inf") # [use-math-not-float]
6-
infinity_float = float("infinity") # [use-math-not-float]
7-
neg_infinity_float = float("-infinity") # [use-math-not-float]
3+
inf_float = float("inf") # [consider-math-not-float]
4+
neg_inf_float = float('-inf') # [consider-math-not-float]
5+
pos_inf_float = float("+inf") # [consider-math-not-float]
6+
infinity_float = float("infinity") # [consider-math-not-float]
7+
neg_infinity_float = float("-infinity") # [consider-math-not-float]
88
large_exp_float = float("1e1000")
99
neg_large_exp_float = float("-1e1000")
1010
very_large_exp_float = float("2.5E9999")
11-
invalid_inf_float = float("in") # [use-math-not-float]
12-
invalid_float_call = float("in", base=10) # [use-math-not-float]
13-
nan_float = float("nan") # [use-math-not-float]
14-
neg_nan_float = float("-nan") # [use-math-not-float]
15-
pos_nan_float = float("+nan") # [use-math-not-float]
16-
upper_nan_float = float("NaN") # [use-math-not-float]
17-
typo_nan_float = float("nani") # [use-math-not-float]
18-
other_typo_nan_float = float("nna") # [use-math-not-float]
11+
invalid_inf_float = float("in") # [consider-math-not-float]
12+
invalid_float_call = float("in", base=10) # [consider-math-not-float]
13+
nan_float = float("nan") # [consider-math-not-float]
14+
neg_nan_float = float("-nan") # [consider-math-not-float]
15+
pos_nan_float = float("+nan") # [consider-math-not-float]
16+
upper_nan_float = float("NaN") # [consider-math-not-float]
17+
typo_nan_float = float("nani") # [consider-math-not-float]
18+
other_typo_nan_float = float("nna") # [consider-math-not-float]

0 commit comments

Comments
 (0)