I came across a bug today that involved the re-definition of a dictionary that
was being looped over within the loop. A simple example is something like:
```
my_dict = {"a": 1, "b": 2, "c": 3}
some_other_dict = {"x": 99}
for key in my_dict.keys():
print(my_dict[key])
my_dict = some_other_dict
```
Python continues to iterate over the original `my_dict`, but to the poor
developer's surprise, Python spits out a KeyError on the second loop. Add in
enough lines of code to disguise it (and, in my case, some bonus recursion of
dictionaries-within-dictionaries), and this turns out to be quite an awkward
error to diagnose. One doesn't receive a RuntimeError, as the original
`my_dict` is not modified during iteration.
It seems neither pylint or flake8 don't catch this. Is it possible to add a
warning for this situation? There are somewhat similar checks already for
imported variables that are redefined, or variables that are defined but then
overwritten without ever being used/read.
Regards,
Daniel
_______________________________________________
code-quality mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/code-quality.python.org/
Member address: [email protected]