Skip to content
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

Suggest to iterate thorough dict items() instead of keys #3389

Closed
EugeneZelenko opened this issue Feb 6, 2020 · 6 comments · Fixed by #4445
Closed

Suggest to iterate thorough dict items() instead of keys #3389

EugeneZelenko opened this issue Feb 6, 2020 · 6 comments · Fixed by #4445
Labels
Checkers Related to a checker Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors Help wanted 🙏 Outside help would be appreciated, good for new contributors

Comments

@EugeneZelenko
Copy link

It'll be great to recognize loops that could iterate through items() instead of keys.

Example of problematic code pattern:

for d in a_dict:
    print(d, a_dict[d])
@PCManticore PCManticore added Checkers Related to a checker Good first issue Friendly and approachable by new contributors Enhancement ✨ Improvement to a component labels Feb 10, 2020
@PCManticore
Copy link
Contributor

Thanks for the report!

@Pierre-Sassoulas Pierre-Sassoulas added the Help wanted 🙏 Outside help would be appreciated, good for new contributors label Mar 2, 2021
@Pierre-Sassoulas Pierre-Sassoulas pinned this issue Apr 13, 2021
@ursmaheshj
Copy link

for d in a_dict.values():
print(d)

@yushao2
Copy link
Collaborator

yushao2 commented May 6, 2021

Opened PR #4445 , please take a look :)

There might be some issues as it is my first time contributing and I'm still not 100% sure if I'm doing it right

yushao2 added a commit to yushao2/pylint that referenced this issue May 8, 2021
yushao2 added a commit to yushao2/pylint that referenced this issue May 9, 2021
@cdce8p
Copy link
Member

cdce8p commented May 9, 2021

for d in a_dict.values():
    print(d)

@ursmaheshj Could you explain what you mean, I'm not quite following? At least I can't think of any case where I would use dict.values and later access the keys from it. Just to get them, I would need to iterate over all items again and even then can't be sure that it's the right key.

yushao2 added a commit to yushao2/pylint that referenced this issue May 10, 2021
yushao2 added a commit to yushao2/pylint that referenced this issue May 10, 2021
yushao2 added a commit to yushao2/pylint that referenced this issue May 10, 2021
yushao2 added a commit to yushao2/pylint that referenced this issue May 10, 2021
@ursmaheshj
Copy link

@ursmaheshj Could you explain what you mean, I'm not quite following? At least I can't think of any case where I would use dict.values and later access the keys from it. Just to get them, I would need to iterate over all items again and even then can't be sure that it's the right key.

@cdce8p Yeah got it, i think i just misunderstood the issue.

@Pierre-Sassoulas Pierre-Sassoulas unpinned this issue May 11, 2021
@craiga
Copy link

craiga commented Jun 30, 2021

Thanks so much for your work here!

An hour ago I was going to log a bug against this change, but it turned out that my code contained an insidious little bug where it wasn't iterating over a data structure as it should've been. Thanks again!

stanislavlevin added a commit to stanislavlevin/fc-admin that referenced this issue Feb 17, 2022
Pylint 2.9 introduced new checker:
> Emitted when iterating over the keys of a dictionary and accessing the
  value by index lookup. Both the key and value can be accessed by
  iterating using the .items() method of the dictionary instead.

See pylint-dev/pylint#3389

Fixes: fleet-commander#279
Signed-off-by: Stanislav Levin <slev@altlinux.org>
stanislavlevin added a commit to stanislavlevin/fc-admin that referenced this issue Feb 17, 2022
Pylint 2.9 introduced new checker:
> Emitted when iterating over the keys of a dictionary and accessing the
  value by index lookup. Both the key and value can be accessed by
  iterating using the .items() method of the dictionary instead.

See pylint-dev/pylint#3389

Fixes: fleet-commander#279
Signed-off-by: Stanislav Levin <slev@altlinux.org>
olivergs pushed a commit to fleet-commander/fc-admin that referenced this issue Mar 3, 2022
Pylint 2.9 introduced new checker:
> Emitted when iterating over the keys of a dictionary and accessing the
  value by index lookup. Both the key and value can be accessed by
  iterating using the .items() method of the dictionary instead.

See pylint-dev/pylint#3389

Fixes: #279
Signed-off-by: Stanislav Levin <slev@altlinux.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Checkers Related to a checker Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors Help wanted 🙏 Outside help would be appreciated, good for new contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants