astral-sh/ruff

Ruff --fix on SIM118 introducing TypeError

Open

#4,481 opened on May 17, 2023

View on GitHub
 (6 comments) (4 reactions) (0 assignees)Rust (47,527 stars) (2,088 forks)batch import
bughelp wantedtype-inference

Description

(Running a large repo through ruff --fix today, so might have a few of these tickets for you 😬)

Ruff --fix on the SIM118 rule is introducing a bug in our code (ruff=v0.0.267). The rule seems to flag any case where a keys() method is used. This works for dicts but doesn't work for classes with a keys() method but no __iter__.

Minimal example:

class Foo:
    def __init__(self) -> None:
        self._keys = [1,2,3]

    def keys(self) -> list[int]:
        return self._keys


foo = Foo()

for k in foo.keys():
    print(k)

Ruff error: SIM118 [*] Use k in foo instead of k in foo.keys()

ruff --fix converts this to for k in foo: which is a TypeError.

Thanks!

Contributor guide