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

Fix false-positives of unnecessary-dict-index-lookup and consider-using-dict-items #4640

Merged
merged 4 commits into from
Jul 1, 2021

Conversation

yushao2
Copy link
Collaborator

@yushao2 yushao2 commented Jun 30, 2021

Steps

  • Add yourself to CONTRIBUTORS if you are a new contributor.
  • Add a ChangeLog entry describing what your PR does.
  • If it's a new feature or an important bug fix, add a What's New entry in
    doc/whatsnew/<current release.rst>.
  • Write a good description on what the PR does.

Description

Fix for false-positives described in #4630, when an dict index lookup has been reassigned.

dict index lookup after the reassignment are necessary

example:

d = {1: 1}
for k, v in d.items():
  d[k] = 10
  assert d[k] == v, (d[k], v)

output:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
AssertionError: (10, 1)

Therefore, using d[k] after reassignment should be ignored

This is implemented by returning after encountering an astroid.AugAssign or astroid.Assign and ignore
nodes occurring after this.

Type of Changes

Type
🐛 Bug fix

Related Issue

Closes #4630

@yushao2 yushao2 added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Jun 30, 2021
@yushao2 yushao2 added this to the 2.9.2 milestone Jun 30, 2021
@cdce8p cdce8p self-requested a review June 30, 2021 14:25
@coveralls
Copy link

coveralls commented Jun 30, 2021

Coverage Status

Coverage remained the same at 92.038% when pulling 6e6c3a3 on yushao2:fix--false-positive-4630 into 472f25e on PyCQA:main.

cdce8p
cdce8p previously requested changes Jun 30, 2021
Copy link
Member

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to look at this issue! I left a few comments, mostly minor stuff.

@yushao2 yushao2 force-pushed the fix--false-positive-4630 branch 3 times, most recently from 3dae100 to 10bdef1 Compare June 30, 2021 17:49
@yushao2
Copy link
Collaborator Author

yushao2 commented Jul 1, 2021

@Pierre-Sassoulas seems like assigning 0 to a var makes it a constant, and naming convention is to be UPPERCASE.

I assigned an empty list to it instead, and changed += to .append.

@Pierre-Sassoulas Pierre-Sassoulas dismissed cdce8p’s stale review July 1, 2021 08:08

Comment taken into account

@Pierre-Sassoulas Pierre-Sassoulas merged commit eff4ad8 into pylint-dev:main Jul 1, 2021
@Pierre-Sassoulas
Copy link
Member

seems like assigning 0 to a var makes it a constant, and naming convention is to be UPPERCASE. I assigned an empty list to it instead, and changed += to .append.

Oups, I did not see that, I disabled the invalid-name and force pushed. Sorry !

@yushao2
Copy link
Collaborator Author

yushao2 commented Jul 1, 2021

seems like assigning 0 to a var makes it a constant, and naming convention is to be UPPERCASE. I assigned an empty list to it instead, and changed += to .append.

Oups, I did not see that, I disabled the invalid-name and force pushed. Sorry !

that's fine! i think both ways work 😆 thanks!

@yushao2 yushao2 deleted the fix--false-positive-4630 branch January 4, 2023 13:56
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
4 participants