sourcery icon indicating copy to clipboard operation
sourcery copied to clipboard

missing-dict-items and remove-dict-keys interacting so I can't have no errors

Open adnathanail opened this issue 3 years ago • 2 comments

Checklist

  • [x] I have searched the Sourcery documentation for the issue, and found nothing
  • [x] I have checked there are no open bugs referencing the same bug or problem

Description

I have a dictionary with tuples as keys, and I want to destructure those keys as I loop through them, like so:

things = {
    ("a", 1): [1, 2, 3],
    ("b", 2): [4, 5, 6],
    ("c", 5): [7, 8, 9]
}

for letter, number in things:
    print(f"this {letter} is {number}")

The code above causes sourcery to suggest missing-dict-items, because it thinks I am trying to iterate through the keys and values of a dictionary, so it wants to add .items()

So I explicitly added a call to .keys(), and it suggested remove-dict-keys!

Debug Information

IDE Version: PyCharm 2022.2 (Professional Edition)

Sourcery Version: 0.12.6

Operating system and Version: macOS Monterey 12.2.1

adnathanail avatar Sep 11 '22 12:09 adnathanail

Oh no! I'll have to think about the best way to resolve this.

Hellebore avatar Sep 11 '22 12:09 Hellebore

For the moment to remove the error you can add a # sourcery skip: missing-dict-items comment to your function (or switch off this suggestion completely with the Sourcery - turn off this refactoring option).

https://docs.sourcery.ai/IDEs/PyCharm/#skip-a-suggestion

Hellebore avatar Sep 11 '22 12:09 Hellebore

I think it would make sense to not suggest missing-dict-items if the keys of the dictionary have type tuple[A, B], ie tuples of length 2.

ronnodas avatar Oct 31 '22 14:10 ronnodas

That's a good solution - unfortunately our type inference isn't yet capable of handling this. Might be a while before we can address it.

Hellebore avatar Nov 30 '22 12:11 Hellebore