astral-sh/ruff

Ruff --fix on SIM118 introducing TypeError

Open

#4,481 创建于 2023年5月17日

在 GitHub 查看
 (6 评论) (4 反应) (0 负责人)Rust (2,088 fork)batch import
bughelp wantedtype-inference

仓库指标

Star
 (47,527 star)
PR 合并指标
 (平均合并 3天 8小时) (30 天内合并 573 个 PR)

描述

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

贡献者指南