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

PLR6201 fails but cannot be fixed on list in tuple(list, list) #9928

Closed
gboeing opened this issue Feb 11, 2024 · 1 comment · Fixed by #9956
Closed

PLR6201 fails but cannot be fixed on list in tuple(list, list) #9928

gboeing opened this issue Feb 11, 2024 · 1 comment · Fixed by #9956
Assignees
Labels
bug Something isn't working

Comments

@gboeing
Copy link

gboeing commented Feb 11, 2024

Thanks for developing and supporting this wonderful tool! I'm using ruff 0.2.0. Say I have a bit of code to check if some list appears in a predefined collection of lists:

def f(x):
    list1 = [1, 2, 3]
    list2 = [4, 5, 6]
    return x in (list1, list2)

Running ruff . --preview fails with "PLR6201 Use a set literal when testing for membership"... which you certainly should do whenever you can, but you cannot in this case. Python lists are not hashable, so you cannot insert them into a set. If you try, like return x in {list1, list2}, you get "TypeError: unhashable type: 'list'".

PLR6201 should not fail in this case because it is unfixable. Instead, PLR6201 should only fail if the items in the membership test are hashable.

@charliermarsh charliermarsh added the bug Something isn't working label Feb 11, 2024
@charliermarsh
Copy link
Member

Agreed. It's hard to fix this in general because we need arbitrary type inference, but we can do better in cases like the one above.

@charliermarsh charliermarsh self-assigned this Feb 12, 2024
charliermarsh added a commit that referenced this issue Feb 12, 2024
## Summary

Ensures that `x in [y, z]` does not trigger in `x`, `y`, or `z` are
known _not_ to be hashable.

Closes #9928.
nkxxll pushed a commit to nkxxll/ruff that referenced this issue Mar 10, 2024
…al-sh#9956)

## Summary

Ensures that `x in [y, z]` does not trigger in `x`, `y`, or `z` are
known _not_ to be hashable.

Closes astral-sh#9928.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants