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

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

Open
GitRon opened this issue Sep 5, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@GitRon
Copy link

GitRon commented Sep 5, 2023

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.

@GitRon GitRon added the enhancement New feature or request label Sep 5, 2023
@christianplatta1012
Copy link

Hi,
this actually crashed our code yesterday.
Please try to to this only for objects that act like dicts. Maybe something in here can help to identify these structures? https://docs.python.org/3/reference/datamodel.html?emulating-container-types=#emulating-container-types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants