Skip to content

Conversation

@njhearp
Copy link
Contributor

@njhearp njhearp commented Nov 12, 2025

Summary

Fixes #21393

Now the rule checks if the index variable is initialized as an int type rather than only flagging if the index variable is initialized to 0. I used ResolvedPythonType to check if the index variable is an int type.

Test Plan

Updated snapshot test for SIM113.

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 12, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+3 -0 violations, +0 -0 fixes in 2 projects; 53 projects unchanged)

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

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview

+ rotkehlchen/tests/unit/decoders/test_dripsv1.py:79:9: SIM113 Use `enumerate()` for index variable `idx` in `for` loop

zulip/zulip (+2 -0 violations, +0 -0 fixes)

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

+ zerver/lib/export.py:1962:9: SIM113 Use `enumerate()` for index variable `dump_file_id` in `for` loop
+ zerver/lib/export.py:2892:9: SIM113 Use `enumerate()` for index variable `dump_file_id` in `for` loop

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
SIM113 3 3 0 0 0

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thank you!

Would you mind making this a preview change? It seems to be in the spirit of the rule and also matches the upstream linter for this test case, but I see there are some ecosystem hits.

Preview will also be nice in case we want to iterate further by inferring the type of the argument, for example with is_int. I think ResolvedPythonType only handles literals and simple operations on them like 1 + 2 rather than using type annotations. (I'm not saying we need to or should do that, just an idea)

@dscorbett
Copy link

SIM113’s recommendation is only safe when the index variable’s initial value is a strict instance of int, whereas something annotated int could be an instance of e.g. bool or a custom subclass, which would make the recommendation unsafe because the program’s behavior could change.

@ntBre
Copy link
Contributor

ntBre commented Nov 12, 2025

Ah, thanks for the clarification! Disregard my last paragraph then, but I'd still lean toward making it a preview change.

@njhearp
Copy link
Contributor Author

njhearp commented Nov 12, 2025

Thanks for the feedback! I updated the rule to be gated behind preview for strict instances of int. I also updated the documentation and comments. It looks like some actions failed due to timeouts, so I suspect some GitHub services are temporarily down and they will pass later.

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thank you!

@ntBre ntBre added rule Implementing or modifying a lint rule preview Related to preview mode features labels Nov 12, 2025
@ntBre ntBre changed the title [flake8-simplify] Apply SIM113 when index variable is type int [flake8-simplify] Apply SIM113 when index variable is of type int Nov 12, 2025
@ntBre ntBre enabled auto-merge (squash) November 12, 2025 17:50
@ntBre ntBre merged commit 8a85a29 into astral-sh:main Nov 12, 2025
38 checks passed
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.

SIM113 only applies when the index variable is initialized to 0

3 participants