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

False positive "consider-using-dict-items" when iteration over dict.keys() #9554

Closed
Flamefire opened this issue Apr 18, 2024 · 0 comments · Fixed by #9594
Closed

False positive "consider-using-dict-items" when iteration over dict.keys() #9554

Flamefire opened this issue Apr 18, 2024 · 0 comments · Fixed by #9594
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@Flamefire
Copy link

Flamefire commented Apr 18, 2024

Bug description

"""TEST module"""
import os

for var in os.environ.keys():
    if var.startswith('foo_'):
        del os.environ[var]

The loop triggers both "consider-using-dict-items" & "consider-iterating-dictionary". The former is a false positive.

This is especially annoying when "consider-iterating-dictionary" is disabled because someone insists that .keys() is clearer

However fixing the 2nd warning by removing .keys() both go away

Command used

pylint test.py 
pylint --disable=C0201 test.py

Pylint output

************* Module test
test.py:4:0: C0206: Consider iterating with .items() (consider-using-dict-items)
test.py:4:11: C0201: Consider iterating the dictionary directly instead of calling .keys() (consider-iterating-dictionary)

------------------------------------------------------------------
Your code has been rated at 7.14/10 (previous run: 5.71/10, +1.43)

Expected behavior

No "consider-using-dict-items" warning

Pylint version

pylint 3.1.0
astroid 3.1.0
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
@Flamefire Flamefire added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Apr 18, 2024
@jacobtylerwalls jacobtylerwalls added False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Apr 19, 2024
mbyrnepr2 added a commit to mbyrnepr2/pylint that referenced this issue May 3, 2024
… ``os.environ`` using the ``os.environ.keys()`` operation and then deleting an item using the key as a lookup.

Closes pylint-dev#9554
@Pierre-Sassoulas Pierre-Sassoulas added this to the 3.1.1 milestone May 4, 2024
Pierre-Sassoulas pushed a commit that referenced this issue May 4, 2024
When iterating ``os.environ`` using the ``os.environ.keys()`` operation and then deleting an item using the key as a lookup.

Closes #9554

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>>
github-actions bot pushed a commit that referenced this issue May 4, 2024
When iterating ``os.environ`` using the ``os.environ.keys()`` operation and then deleting an item using the key as a lookup.

Closes #9554

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>>
(cherry picked from commit c864cd4)
Pierre-Sassoulas pushed a commit that referenced this issue May 4, 2024
When iterating ``os.environ`` using the ``os.environ.keys()`` operation and then deleting an item using the key as a lookup.

Closes #9554

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>>
(cherry picked from commit c864cd4)

Co-authored-by: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants