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 fails to detect nonsensical list index #5082

Closed
dgjustice opened this issue Jun 14, 2023 · 1 comment · Fixed by #5602
Closed

Ruff fails to detect nonsensical list index #5082

dgjustice opened this issue Jun 14, 2023 · 1 comment · Fixed by #5602
Assignees
Labels
good first issue Good for newcomers help wanted Contributions especially welcome rule Implementing or modifying a lint rule

Comments

@dgjustice
Copy link

ruff is an awesome project! ❤️ I saw this in the wild today on a PR to fix a bug that passed through CI (with ruff check).

Python source code

The developer accidentally deleted the dict variable during a refactor resulting in something structurally identical to this:

foo = ["abc"]["def"]

Ruff check

$ cat foo.py 
foo = ["abc"]["def"]

$ python foo.py 
/home/djustice/tmp/foo.py:1: SyntaxWarning: list indices must be integers or slices, not str; perhaps you missed a comma?
  foo = ["abc"]["def"]
Traceback (most recent call last):
  File "/home/djustice/tmp/foo.py", line 1, in <module>
    foo = ["abc"]["def"]
TypeError: list indices must be integers or slices, not str

$ ruff check foo.py


$ echo $status
0

Ruff version

$ ruff --version
ruff 0.0.264

$
@charliermarsh charliermarsh added question Asking for support or clarification rule Implementing or modifying a lint rule labels Jun 17, 2023
@charliermarsh
Copy link
Member

charliermarsh commented Jun 22, 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 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 zanieb self-assigned this Jul 7, 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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants