astral-sh/ruff

Ruff --fix on SIM118 introducing TypeError

Open

#4481 aperta il 17 mag 2023

Vedi su GitHub
 (6 commenti) (4 reazioni) (0 assegnatari)Rust (2088 fork)batch import
bughelp wantedtype-inference

Metriche repository

Star
 (47.527 star)
Metriche merge PR
 (Merge medio 3g 8h) (573 PR mergiate in 30 g)

Descrizione

(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!

Guida contributor