-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 fails to detect nonsensical list index #5082
Labels
good first issue
Good for newcomers
help wanted
Contributions especially welcome
rule
Implementing or modifying a lint rule
Comments
charliermarsh
added
question
Asking for support or clarification
rule
Implementing or modifying a lint rule
labels
Jun 17, 2023
I think this is reasonable but we can probably only support literals (like Python, which raises a syntax warning). This blog post from Adam Johnson is a good explanation of the rule and what we could support: https://adamj.eu/tech/2020/06/21/why-does-python-syntaxwarning-saying-list-indices-must-be-integers-or-slices/. |
charliermarsh
added
good first issue
Good for newcomers
help wanted
Contributions especially welcome
and removed
question
Asking for support or clarification
labels
Jun 22, 2023
zanieb
added a commit
that referenced
this issue
Jul 12, 2023
Detects invalid types for tuple, list, bytes, string indices. For example, the following will raise a `TypeError` at runtime and when imported Python will display a `SyntaxWarning` ```python var = [1, 2, 3]["x"] ``` ``` example.py:1: SyntaxWarning: list indices must be integers or slices, not str; perhaps you missed a comma? var = [1, 2, 3]["x"] Traceback (most recent call last): File "example.py", line 1, in <module> var = [1, 2, 3]["x"] ~~~~~~~~~^^^^^ TypeError: list indices must be integers or slices, not str ``` Previously, Ruff would not report the invalid syntax but now a violation will be reported. This does not apply to cases where a variable, call, or complex expression is used in the index — detection is roughly limited to static definitions, which matches Python's warnings. ``` ❯ ./target/debug/ruff example.py --select RUF015 --show-source --no-cache example.py:1:17: RUF015 Indexed access to type `list` uses type `str` instead of an integer or slice. | 1 | var = [1, 2, 3]["x"] | ^^^ RUF015 | ``` Closes #5082 xref python/cpython@ffff144
konstin
pushed a commit
that referenced
this issue
Jul 19, 2023
Detects invalid types for tuple, list, bytes, string indices. For example, the following will raise a `TypeError` at runtime and when imported Python will display a `SyntaxWarning` ```python var = [1, 2, 3]["x"] ``` ``` example.py:1: SyntaxWarning: list indices must be integers or slices, not str; perhaps you missed a comma? var = [1, 2, 3]["x"] Traceback (most recent call last): File "example.py", line 1, in <module> var = [1, 2, 3]["x"] ~~~~~~~~~^^^^^ TypeError: list indices must be integers or slices, not str ``` Previously, Ruff would not report the invalid syntax but now a violation will be reported. This does not apply to cases where a variable, call, or complex expression is used in the index — detection is roughly limited to static definitions, which matches Python's warnings. ``` ❯ ./target/debug/ruff example.py --select RUF015 --show-source --no-cache example.py:1:17: RUF015 Indexed access to type `list` uses type `str` instead of an integer or slice. | 1 | var = [1, 2, 3]["x"] | ^^^ RUF015 | ``` Closes #5082 xref python/cpython@ffff144
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
good first issue
Good for newcomers
help wanted
Contributions especially welcome
rule
Implementing or modifying a lint rule
ruff
is an awesome project! ❤️ I saw this in the wild today on a PR to fix a bug that passed through CI (withruff check
).Python source code
The developer accidentally deleted the dict variable during a refactor resulting in something structurally identical to this:
Ruff check
Ruff version
The text was updated successfully, but these errors were encountered: