Skip to content
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

[ruff] Unnecessary cast to int (RUF046) #14697

Merged
merged 10 commits into from
Dec 5, 2024

Conversation

InSyncWithFoo
Copy link
Contributor

Summary

Resolves #11412.

Test Plan

cargo nextest run and cargo insta test.

@InSyncWithFoo
Copy link
Contributor Author

Maybe this should be expanded to cover those suggested in #14292 as well, in which case the rule should be renamed to unnecessary-cast-to-int?

Copy link
Contributor

github-actions bot commented Dec 1, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+15 -0 violations, +0 -0 fixes in 5 projects; 50 projects unchanged)

RasaHQ/rasa (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ rasa/shared/core/training_data/loading.py:86:15: RUF046 Value being casted is already an integer

apache/superset (+2 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

+ superset/migrations/versions/2018-07-22_11-59_bebcf3fed1fe_convert_dashboard_v1_positions.py:197:40: RUF046 Value being casted is already an integer
+ superset/migrations/versions/2018-07-22_11-59_bebcf3fed1fe_convert_dashboard_v1_positions.py:199:29: RUF046 Value being casted is already an integer

bokeh/bokeh (+5 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

+ examples/server/app/spectrogram/main.py:107:13: RUF046 Value being casted is already an integer
+ src/bokeh/colors/color.py:338:60: RUF046 Value being casted is already an integer
+ src/bokeh/palettes.py:1530:27: RUF046 Value being casted is already an integer
+ src/bokeh/palettes.py:1568:10: RUF046 Value being casted is already an integer
+ src/bokeh/palettes.py:1569:10: RUF046 Value being casted is already an integer

indico/indico (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ indico/modules/rb/models/reservation_occurrences.py:223:24: RUF046 Value being casted is already an integer

astropy/astropy (+6 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ astropy/io/fits/hdu/compressed/tests/test_compressed.py:375:26: RUF046 Value being casted is already an integer
+ astropy/io/fits/hdu/compressed/utils.py:102:13: RUF046 Value being casted is already an integer
+ astropy/io/fits/tests/test_image.py:1003:26: RUF046 Value being casted is already an integer
+ astropy/io/votable/validator/html.py:246:14: RUF046 Value being casted is already an integer
+ astropy/modeling/_fitting_parallel.py:194:22: RUF046 Value being casted is already an integer
+ astropy/utils/console.py:365:21: RUF046 Value being casted is already an integer

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF046 15 15 0 0 0

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@sbrugman
Copy link
Contributor

sbrugman commented Dec 1, 2024

+1 unnecessary cast to int. This is more future proof: when type inference is available unnecessary int casts could be detected for a much wider class of functions.

Consider expanding this rule to other builtin functions such as “len”. Edit: indeed as suggested in the other issue.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your proposal to use the more generic name unnecessary-cast-to-int.

@InSyncWithFoo InSyncWithFoo changed the title [ruff] Unnecessary round() cast (RUF046) [ruff] Unnecessary cast to int (RUF046) Dec 2, 2024
@MichaReiser MichaReiser added rule Implementing or modifying a lint rule preview Related to preview mode features labels Dec 2, 2024
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

The special casing where ndigitis=0 is incorrect. We should not flag that usage.

@NeilGirdhar
Copy link

NeilGirdhar commented Dec 3, 2024

when type inference is available unnecessary int casts could be detected for a much wider class of functions.

Just FYI, this may not be possible because Boolean values are (unfortunately) integers:

def f(x: int):
  assert isinstance(x, int)
  print(x, int(x))  # If you remove `int` here, you change things.
f(True)

You could detect unnecessary casts, but you have to check that it's not used in any way that would matter, which is a lot trickier.

@InSyncWithFoo
Copy link
Contributor Author

@NeilGirdhar I made sure to take this into account: All functions listed there either delegate to their corresponding dunder methods or return a strict instance of int. If you can find one that doesn't, please let me know.

@NeilGirdhar
Copy link

NeilGirdhar commented Dec 3, 2024

@NeilGirdhar I made sure to take this into account: All functions listed there either delegate to their corresponding dunder methods or return a strict instance of int. If you can find one that doesn't, please let me know.

Yes, I wasn't talking about this excellent PR 😄 . I was just responding to the comment that suggested that type inference can be used to discover unnecessary integer casts.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. I pushed a few smaller nits, updated the documentation, and marked the fix for round(2) as safe. Let me know if that looks good to you and I'll merge

@InSyncWithFoo
Copy link
Contributor Author

@MichaReiser Looks good to me too. Let's get this merged.

@MichaReiser MichaReiser merged commit fda8b1f into astral-sh:main Dec 5, 2024
21 checks passed
@DanielYang59
Copy link

I completely missed the party, but I'm amazed by how fast you respond, couldn't appreciate more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a rule to catch int(round(...)
7 participants