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

getting unnecessary-dict-index-lookup when deleting an item by value #4716

Closed
2bndy5 opened this issue Jul 19, 2021 · 4 comments · Fixed by #5344
Closed

getting unnecessary-dict-index-lookup when deleting an item by value #4716

2bndy5 opened this issue Jul 19, 2021 · 4 comments · Fixed by #5344
Assignees
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Milestone

Comments

@2bndy5
Copy link

2bndy5 commented Jul 19, 2021

I was wondering if I'm doing this wrong. I'm trying to remove a dict item when searching the dict's items() to re-assign a certain unique value to a new key.


NOTES

  1. The dict I'm managing is a private class member and will not have multiple keys with the same value.
  2. I'm using pylint v2.9.3 (had the same problem with v2.9.0 previously)

I've added # pylint: comments around the controversial line

    def _set_address(self, node_id, address, search_by_address=False):
        """Set or change a node_id and network address pair on the master node."""
        for n_id, addr in self._addr_dict.items():
            if not search_by_address:  # search by key
                if n_id == node_id:
                    self._addr_dict[n_id] = address
                    break
            else:  # search by value
                if addr == address:
                    # pylint: disable=unnecessary-dict-index-lookup
                    del self._addr_dict[n_id]
                    # pylint: enable=unnecessary-dict-index-lookup
                    self._addr_dict[node_id] = address
                    break
        if node_id not in self._addr_dict.keys():
            self._addr_dict[node_id] = address

The pylint message suggests I use addr instead, but that doesn't help when deleting an item by key.

@Pierre-Sassoulas Pierre-Sassoulas added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Jul 19, 2021
@Pierre-Sassoulas
Copy link
Member

Thank you for opening the issue, I think it's a false positive.

@2bndy5
Copy link
Author

2bndy5 commented Jul 19, 2021

I was able to work around this in a separate instance by using dict comprehension.

new_dict = {}
for k, v in old_dict.items():
    if v != val_to_remove:
        new_dict[k] = v
old_dict = new_dict

But, applying this workaround to the snippet in the OP would be more work than its worth because I'm also re-assigning the previously used value to a new key.

@cdce8p
Copy link
Member

cdce8p commented Jul 19, 2021

This is a false-positive, unnecessary-dict-index-lookup should not be emitted for del statements.
I'm able to reproduce the error with this snipped:

d = {}
for key, val in d.items():
    del d[key]
    break

/CC: @yushao2

--
Update
The code only works if the del is followed by break. Otherwise Python would raise a Runtime error.

RuntimeError: dictionary keys changed during iteration

@yushao2 yushao2 self-assigned this Jul 25, 2021
@yushao2
Copy link
Contributor

yushao2 commented Jul 25, 2021

Thanks, assigned myself to look into this

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.

5 participants