Skip to content

Conversation

Pierre-Sassoulas
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas commented Oct 6, 2025

Type of Changes

Type
✨ New feature

Description

float("nan") and float("inf") are slower than their counterpart math.inf and math.nan by a factor of 4 :

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')
math.nan: 1.24 seconds
float("nan"): 5.15 seconds

But:

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')
math.nan: 9.08 seconds
float("nan"): 5.33 seconds

They are also not well typed when using mypy, see discussion in ruff: astral-sh/ruff#15908

I chose the code style checker without thinking too much about it and because there was already an inference call on the name of a call here. I can move it to the refactoring checker considering the benefit ? (Did this at work in pair so at least someone else than me and 4 person thumbsupping in the ruff issue consider this worthwhile)

@Pierre-Sassoulas Pierre-Sassoulas added the Enhancement ✨ Improvement to a component label Oct 6, 2025
Copy link

codecov bot commented Oct 6, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.96%. Comparing base (a85bf0c) to head (3541b99).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #10621   +/-   ##
=======================================
  Coverage   95.95%   95.96%           
=======================================
  Files         176      176           
  Lines       19504    19517   +13     
=======================================
+ Hits        18716    18729   +13     
  Misses        788      788           
Files with missing lines Coverage Δ
pylint/extensions/code_style.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This comment has been minimized.

@Pierre-Sassoulas Pierre-Sassoulas added the Needs review 🔍 Needs to be reviewed by one or multiple more persons label Oct 6, 2025

This comment has been minimized.

This comment has been minimized.

@Pierre-Sassoulas Pierre-Sassoulas requested review from jacobtylerwalls and removed request for cdce8p October 6, 2025 18:47

This comment has been minimized.

@jacobtylerwalls
Copy link
Member

Off by default? 🤔

@Pierre-Sassoulas
Copy link
Member Author

Do you mean it should be disabled by default in extension or added in checkers instead of extension ? 😄

@Pierre-Sassoulas Pierre-Sassoulas changed the title [use-math-not-float] Add a check for float(inf) and float(nan) [use-math-not-float] Add a check for float("inf"), float("nan") and float("typos") Oct 7, 2025

This comment has been minimized.

@jacobtylerwalls
Copy link
Member

Ah, I didn't realize it was in an extensions. nice.

@Pierre-Sassoulas
Copy link
Member Author

Yeah, originally it wasn't obvious it should be an extension but I suppose the microbenchmark is misleading in the ruff issue because it doesn't take the "import math" into account. I also changed the "use" to "consider" in the message name because of this. If mypy understand better float("inf") one day this is going to be a really situational warning.

@Pierre-Sassoulas Pierre-Sassoulas changed the title [use-math-not-float] Add a check for float("inf"), float("nan") and float("typos") [consider-math-not-float] Add a check for float("inf"), float("nan") and float("typos") Oct 7, 2025

This comment has been minimized.

Copy link
Contributor

github-actions bot commented Oct 7, 2025

🤖 Effect of this PR on checked open source code: 🤖

Effect on home-assistant:
The following messages are now emitted:

  1. consider-math-not-float:
    Consider math.inf instead of float('inf')
    https://github.com/home-assistant/core/blob/b9a1ab4a44c36f3cf2c2073ee8da715ecd1f04ef/homeassistant/helpers/significant_change.py#L164
  2. consider-math-not-float:
    Consider math.inf instead of float('inf')
    https://github.com/home-assistant/core/blob/b9a1ab4a44c36f3cf2c2073ee8da715ecd1f04ef/homeassistant/components/habitica/todo.py#L284
  3. consider-math-not-float:
    Consider math.inf instead of float('inf')
    https://github.com/home-assistant/core/blob/b9a1ab4a44c36f3cf2c2073ee8da715ecd1f04ef/homeassistant/components/habitica/todo.py#L370
  4. consider-math-not-float:
    Consider -math.inf instead of float('-inf')
    https://github.com/home-assistant/core/blob/b9a1ab4a44c36f3cf2c2073ee8da715ecd1f04ef/homeassistant/components/knx/schema.py#L89
  5. consider-math-not-float:
    Consider math.inf instead of float('inf')
    https://github.com/home-assistant/core/blob/b9a1ab4a44c36f3cf2c2073ee8da715ecd1f04ef/homeassistant/components/knx/schema.py#L97
  6. consider-math-not-float:
    Consider -math.inf instead of float('-inf')
    https://github.com/home-assistant/core/blob/b9a1ab4a44c36f3cf2c2073ee8da715ecd1f04ef/homeassistant/components/stream/worker.py#L49
  7. consider-math-not-float:
    Consider -math.inf instead of float('-inf')
    https://github.com/home-assistant/core/blob/b9a1ab4a44c36f3cf2c2073ee8da715ecd1f04ef/homeassistant/components/stream/recorder.py#L79

Effect on music21:
The following messages are now emitted:

  1. consider-math-not-float:
    Consider math.nan instead of float('nan')
    https://github.com/cuthbertLab/music21/blob/cb1bdfeb5303c02a9c2dff1f3e5d9232806dfd96/music21/duration.py#L3961
  2. consider-math-not-float:
    Consider math.nan instead of float('nan')
    https://github.com/cuthbertLab/music21/blob/cb1bdfeb5303c02a9c2dff1f3e5d9232806dfd96/music21/duration.py#L3969
  3. consider-math-not-float:
    Consider -math.inf instead of float('-inf')
    https://github.com/cuthbertLab/music21/blob/cb1bdfeb5303c02a9c2dff1f3e5d9232806dfd96/music21/base.py#L2069
  4. consider-math-not-float:
    Consider math.nan instead of float('nan')
    https://github.com/cuthbertLab/music21/blob/cb1bdfeb5303c02a9c2dff1f3e5d9232806dfd96/music21/base.py#L3770
  5. consider-math-not-float:
    Consider math.nan instead of float('nan')
    https://github.com/cuthbertLab/music21/blob/cb1bdfeb5303c02a9c2dff1f3e5d9232806dfd96/music21/base.py#L3969
  6. consider-math-not-float:
    Consider math.nan instead of float('nan')
    https://github.com/cuthbertLab/music21/blob/cb1bdfeb5303c02a9c2dff1f3e5d9232806dfd96/music21/base.py#L3979
  7. consider-math-not-float:
    Consider math.inf instead of float('inf')
    https://github.com/cuthbertLab/music21/blob/cb1bdfeb5303c02a9c2dff1f3e5d9232806dfd96/music21/midi/realtime.py#L107
  8. consider-math-not-float:
    Consider math.inf instead of float('inf')
    https://github.com/cuthbertLab/music21/blob/cb1bdfeb5303c02a9c2dff1f3e5d9232806dfd96/music21/midi/realtime.py#L139
  9. consider-math-not-float:
    Consider math.nan instead of float('nan')
    https://github.com/cuthbertLab/music21/blob/cb1bdfeb5303c02a9c2dff1f3e5d9232806dfd96/music21/stream/base.py#L8622

Effect on django:
The following messages are now emitted:

  1. consider-math-not-float:
    Consider math.nan instead of float('NaN')
    https://github.com/django/django/blob/6e3287408e128e03a6ec5d23c17cdc2eee8760c0/django/contrib/gis/geos/prototypes/io.py#L267
  2. consider-math-not-float:
    Consider math.nan instead of float('NaN')
    https://github.com/django/django/blob/6e3287408e128e03a6ec5d23c17cdc2eee8760c0/django/contrib/gis/geos/prototypes/io.py#L267

Effect on sentry:
The following messages are now emitted:

  1. consider-math-not-float:
    Consider math.inf instead of float('inf')
    https://github.com/getsentry/sentry/blob/006dead493dea4fb0fbfc1bcf262373c660ee70c/src/sentry/spans/buffer.py#L199
  2. consider-math-not-float:
    Consider -math.inf instead of float('-inf')
    https://github.com/getsentry/sentry/blob/006dead493dea4fb0fbfc1bcf262373c660ee70c/src/sentry/spans/buffer.py#L200
  3. consider-math-not-float:
    Consider math.inf instead of float('inf')
    https://github.com/getsentry/sentry/blob/006dead493dea4fb0fbfc1bcf262373c660ee70c/src/sentry/models/groupsnooze.py#L103
  4. consider-math-not-float:
    Consider math.inf instead of float('inf')
    https://github.com/getsentry/sentry/blob/006dead493dea4fb0fbfc1bcf262373c660ee70c/src/sentry/models/groupsnooze.py#L163
  5. consider-math-not-float:
    Consider math.inf instead of float('inf')
    https://github.com/getsentry/sentry/blob/006dead493dea4fb0fbfc1bcf262373c660ee70c/src/sentry/models/groupsnooze.py#L211
  6. consider-math-not-float:
    Consider math.inf instead of float('inf')
    https://github.com/getsentry/sentry/blob/006dead493dea4fb0fbfc1bcf262373c660ee70c/src/sentry/interfaces/stacktrace.py#L131
  7. consider-math-not-float:
    Consider -math.inf instead of float('-inf')
    https://github.com/getsentry/sentry/blob/006dead493dea4fb0fbfc1bcf262373c660ee70c/src/sentry/interfaces/stacktrace.py#L133
  8. consider-math-not-float:
    Consider math.inf instead of float('inf')
    https://github.com/getsentry/sentry/blob/006dead493dea4fb0fbfc1bcf262373c660ee70c/src/sentry/ratelimits/concurrent.py#L15

This comment was generated for commit 3541b99

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Needs review 🔍 Needs to be reviewed by one or multiple more persons
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants