-
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
PLC0206 should not flag if dict is being written #14585
Comments
What about using |
The code isn't really meant to be taken seriously and the actual code where I hit this was more complex. If you like a slightly more complex example, take this one, which is also flagged: foo = {}
for key in foo.keys():
if foo[key] == "foo":
foo[key] = "bar"
elif foo[key] == "bar":
foo[key] = "foo" |
That's because the rule triggers the first time it sees I'm actually kind of borderline on whether we should or shouldn't flag, especially because we already don't flag if Already in your second example, it starts to get a little verbose. I kind of find it more readable: for key,val in foo.items():
if val == "foo":
foo[key] = "bar"
elif val == "bar":
foo[key] = "foo" One could imagine even more gymnastics: for key in foo:
# <--- all sorts of complicated expressions with the value foo[key] --->
if cond_now_holds:
foo[key] = 10 It seems in the spirit of the rule to flag this. But I also see your point. I'm on the fence. (P.S. - ignore the linked PR below. Bad copy pasting ... too many tabs open 😅 ) |
Oh, @dylwil3, you're right. This is the same as #13821. That's what you get for replying late in the day 😆 I then agree that the rule should flag this pattern because it helps remove some repetition. It's unfortunate that Python doesn't allow assigning to |
This code is flagged for PLC0206 with v0.8.0:
Arguable, changing
.keys()
to.items()
here does not improve readability because one still needs to usefoo[key]
to write into the dict:Suggestion would be to not trigger the rule when any write to the dict is present. Interestingly, the rule already does not flag when the
if
is removed.The text was updated successfully, but these errors were encountered: