sourcery-ai / sourcery

Instant AI code reviews
https://sourcery.ai
MIT License
1.53k stars 66 forks source link

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

Closed adnathanail closed 1 year ago

adnathanail commented 1 year ago

Checklist

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

Hellebore commented 1 year ago

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

Hellebore commented 1 year ago

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

ronnodas commented 1 year ago

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.

Hellebore commented 1 year ago

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.