-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Should E4702 modified-iterating-dict
be triggered for updating existing keys?
#6179
Comments
How about changing this report from an error to a warning? It is still usually a mistake, even if it doesn't always raise an error. |
@wshanks Thanks for reporting the issue. I think it would be nice to update my checker with:
Regarding the message content suggestions:
Maybe we should change the message to be something more in terms with this logic: |
@DanielNoord - let me know what you think about all this. If you think a change is needed you can assign me to this task |
I like this 👍 |
Regarding warning vs error (which was not my suggestion), I agree that error makes the most sense as long as the instances of false positives are rare. I don't know the internals of pylint well enough to judge that. At least the simple case that I gave could be exempted. There could be more complicated juggling and reassigning of existing keys that it is hard for pylint to follow, but maybe that is rare enough to leave for users to disable the check. |
Completely agree with these points! I'll assign you @orSolocate |
@jacobtylerwalls - thanks for implementing it mate! |
Question
With pylint 2.13, the following file:
results in:
The error can be avoided by changing
for key in my_dict
tofor key in iter(my_dict)
(orfor key in list(my_dict)
; maybe theiter()
version should still be an error).@orSolocate I am using the "support question" issue template because I am a bit conflicted about if this is handled right and thought it would be good to have it at least captured in a closed issue if nothing else.
The example I gave above is a fairly common case, I think -- iterate over the keys of a dict and change the values associated with those keys. Should that case be flagged as an error? Is it an implementation quirk that it works fine in CPython (in principle reassigning to an existing key could result in a new entry internally and have similar problems to adding new keys)? Or should pylint be more careful to check for new items added to the dict?
Documentation for future user
The justification for the error is currently documented as "Emitted when items are added or removed to a dict being iterated through." If the existing behavior is okay, perhaps it should be "Emitted when items are added to, removed from, or reassigned to a dict being iterated through."
Additional context
No response
The text was updated successfully, but these errors were encountered: