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 unnecessary-dict-index-lookup and consider-using-dict-items if value associated with key is changed in loop #4630

Closed
akosthekiss opened this issue Jun 29, 2021 · 2 comments · Fixed by #4640
Assignees
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Milestone

Comments

@akosthekiss
Copy link

Steps to reproduce

Given a file d.py:

"""d.py"""

d = {'key': 'value'}

print(d)

for k, _ in d.items():
    d[k] += 'VALUE'
    if 'V' in d[k]:  # if d[k] is replaced with _, the semantics change
        print('found V')

print(d)

for k in d:  # if this gets rewritten to d.items(), we are back to the above problem
    d[k] += '123'
    if '1' in d[k]:
        print('found 1')

print(d)

Current behavior

Result of pylint d.py:

************* Module d
d.py:9:14: R1733: Unnecessary dictionary index lookup, use '_' instead (unnecessary-dict-index-lookup)
d.py:14:0: C0206: Consider iterating with .items() (consider-using-dict-items)

Expected behavior

No diagnostics.

Following the recommendation for line 9 (i.e., rewriting d[k] to _) would incorrectly change the semantics of the program, as d[k] has already been changed, so it has a different value than _.

Following the recommendation for line 14 (i.e., rewriting for k in d to for k, v in d.items() would lead back to the above problem.

pylint --version output

Result of pylint --version output:

pylint 3.0.0-a4
astroid 2.6.1
Python 3.6.9 (default, Jan 26 2021, 15:33:00) 
[GCC 8.4.0]
pylint 2.9.0
astroid 2.6.1
Python 3.6.9 (default, Jan 26 2021, 15:33:00) 
[GCC 8.4.0]
@Pierre-Sassoulas Pierre-Sassoulas added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Jun 30, 2021
@cdce8p
Copy link
Member

cdce8p commented Jun 30, 2021

@yushao2 Would you like to take a look at it? It seems we need to ignore unnecessary-dict-index-lookup and consider-using-dict-items if an Assign or AugAssign is used.

@yushao2
Copy link
Collaborator

yushao2 commented Jun 30, 2021

@yushao2 Would you like to take a look at it? It seems we need to ignore unnecessary-dict-index-lookup and consider-using-dict-items if an Assign or AugAssign is used.

raised a pr for it: #4640

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants