-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[consider-math-not-float] Add a check for float("inf")
, float("nan")
and float("typos")
#10621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
902096c
[use-math-not-float] Add a check for float(inf) and float(nan)
Pierre-Sassoulas 4fa1375
Take into account the limitation of the microbenchmark
Pierre-Sassoulas bda3d30
Handle typo in float call more gracefully
Pierre-Sassoulas d25b7fb
Make the message less prescriptive as the import need to be taken int…
Pierre-Sassoulas 2cd4cda
Update doc/user_guide/checkers/extensions.rst
Pierre-Sassoulas 38914db
Upgrade doc / code following typos fix
Pierre-Sassoulas 3541b99
Update doc/whatsnew/fragments/10621.new_check
Pierre-Sassoulas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
swag = float("inf") # [consider-math-not-float] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
This is an extension check because the typing advantage could be fixed. | ||
|
||
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. | ||
|
||
.. code-block:: python | ||
|
||
import math | ||
import timeit | ||
|
||
time_math_inf = timeit.timeit('math.nan', globals=globals(), number=10**8) | ||
print(f'math.nan: {time_math_inf:.2f} seconds') | ||
|
||
import timeit | ||
time_inf_str = timeit.timeit('float("nan")', number=10**8) | ||
print(f'float("nan"): {time_inf_str:.2f} seconds') | ||
|
||
Result:: | ||
|
||
math.nan: 1.24 seconds | ||
float("nan"): 5.15 seconds | ||
|
||
But if we take the initial import into account it's worse. | ||
|
||
.. code-block:: python | ||
|
||
import timeit | ||
|
||
time_math_inf = timeit.timeit('import math;math.nan', globals=globals(), number=10**8) | ||
print(f'math.nan: {time_math_inf:.2f} seconds') | ||
|
||
import timeit | ||
time_inf_str = timeit.timeit('float("nan")', number=10**8) | ||
print(f'float("nan"): {time_inf_str:.2f} seconds') | ||
|
||
Result:: | ||
|
||
math.nan: 9.08 seconds | ||
float("nan"): 5.33 seconds | ||
|
||
So the decision depends on how and how often you need to use it and what matter to you. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import math | ||
|
||
swag = math.inf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[MAIN] | ||
load-plugins=pylint.extensions.code_style |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Add a ``consider-math-not-float`` message. ``float("nan")`` and ``float("inf")`` are slower | ||
than their counterpart ``math.inf`` and ``math.nan`` by a factor of 4 (notwithstanding | ||
the initial import of math) and they are also not well typed when using mypy. | ||
|
||
The :ref:`pylint.extensions.code_style` need to be activated for this check to work. | ||
|
||
Refs #10621 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""Functional test for consider-math-not-float.""" | ||
|
||
inf_float = float("inf") # [consider-math-not-float] | ||
neg_inf_float = float('-inf') # [consider-math-not-float] | ||
pos_inf_float = float("+inf") # [consider-math-not-float] | ||
infinity_float = float("infinity") # [consider-math-not-float] | ||
neg_infinity_float = float("-infinity") # [consider-math-not-float] | ||
large_exp_float = float("1e1000") | ||
neg_large_exp_float = float("-1e1000") | ||
very_large_exp_float = float("2.5E9999") | ||
invalid_inf_float = float("in") # [consider-math-not-float] | ||
invalid_float_call = float("in", base=10) # [consider-math-not-float] | ||
nan_float = float("nan") # [consider-math-not-float] | ||
neg_nan_float = float("-nan") # [consider-math-not-float] | ||
pos_nan_float = float("+nan") # [consider-math-not-float] | ||
upper_nan_float = float("NaN") # [consider-math-not-float] | ||
typo_nan_float = float("nani") # [consider-math-not-float] | ||
other_typo_nan_float = float("nna") # [consider-math-not-float] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[MAIN] | ||
load-plugins=pylint.extensions.code_style | ||
enable=consider-math-not-float |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
consider-math-not-float:3:12:3:24::Consider math.inf instead of float('inf'):INFERENCE | ||
consider-math-not-float:4:16:4:29::Consider -math.inf instead of float('-inf'):INFERENCE | ||
consider-math-not-float:5:16:5:29::Consider math.inf instead of float('+inf'):INFERENCE | ||
consider-math-not-float:6:17:6:34::Consider math.inf instead of float('infinity'):INFERENCE | ||
consider-math-not-float:7:21:7:39::Consider -math.inf instead of float('-infinity'):INFERENCE | ||
consider-math-not-float:11:20:11:31::Consider math.inf instead of float('in'):INFERENCE | ||
consider-math-not-float:12:21:12:41::Consider math.inf instead of float('in', base=10):INFERENCE | ||
consider-math-not-float:13:12:13:24::Consider math.nan instead of float('nan'):INFERENCE | ||
consider-math-not-float:14:16:14:29::Consider math.nan instead of float('-nan'):INFERENCE | ||
consider-math-not-float:15:16:15:29::Consider math.nan instead of float('+nan'):INFERENCE | ||
consider-math-not-float:16:18:16:30::Consider math.nan instead of float('NaN'):INFERENCE | ||
consider-math-not-float:17:17:17:30::Consider math.nan instead of float('nani'):INFERENCE | ||
consider-math-not-float:18:23:18:35::Consider math.nan instead of float('nna'):INFERENCE |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.