Skip to content

[Adjust Rule] SIM118 - possible issue with objects having a method "keys()" #186

@GitRon

Description

@GitRon

Example

Imagine this scenario:

class X:
    def keys(self):
        return [1, 2, 3]

x = X()

if 1 in x.keys():
    print('yes')

If I run the linter, I get:

SIM118 Use 1 in x instead of 1 in x.keys()

Which will obviously lead to a crash. I know that I wouldn't name a method keys() but still I think the rule might cause some problems.

  • Rule(s): SIM118
  • Adjustment: In which way should it/they be adjusted?

We need some check that it's really a dict if we want to auto-remove the keys...

Explanation

We might crash somebodys code when somebody is auto-fixing it (via ruff) or just blindly following the linter.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions