Skip to content

Commit

Permalink
Fix false positive dict-iter-missing-items for tuple keys
Browse files Browse the repository at this point in the history
This fixes a false positive emitted for dictionaries that contain only
tuples as keys. This makes unpacking the dictionary without calling
`.items()` valid.
This closes pylint-dev#3282
  • Loading branch information
DanielNoord committed Aug 31, 2021
1 parent e1da3c0 commit 9557813
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -45,6 +45,10 @@ Release date: TBA

* Fix false positive for ``protected-access`` if a protected member is used in type hints of function definitions

* Fix false positive ``dict-iter-missing-items`` for dictionaries only using tuples as keys

Closes #3282


What's New in Pylint 2.10.3?
============================
Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.11.rst
Expand Up @@ -51,3 +51,7 @@ Other Changes
Closes #4936

* Fix false positive for ``protected-access`` if a protected member is used in type hints of function definitions

* Fix false positive ``dict-iter-missing-items`` for dictionaries only using tuples as keys

Closes #3282
4 changes: 4 additions & 0 deletions pylint/checkers/typecheck.py
Expand Up @@ -1862,6 +1862,10 @@ def visit_for(self, node):
# the iterable is not a dict
return

if all(isinstance(i[0], nodes.Tuple) for i in inferred.items):
# if all keys are tuples
return

self.add_message("dict-iter-missing-items", node=node)


Expand Down
3 changes: 3 additions & 0 deletions tests/functional/d/dict_iter_missing_items.py
Expand Up @@ -2,6 +2,7 @@
from unknown import Uninferable

d = {1: 1, 2: 2}
d_tuple = {(1, 2): 3, (4, 5): 6}
l = [1, 2]
s1 = {1, 2}
s2 = {1, 2, 3}
Expand All @@ -21,3 +22,5 @@
pass
for k, v in Uninferable:
pass
for a, b in d_tuple:
pass
2 changes: 1 addition & 1 deletion tests/functional/d/dict_iter_missing_items.txt
@@ -1 +1 @@
dict-iter-missing-items:10:0::Unpacking a dictionary in iteration without calling .items()
dict-iter-missing-items:11:0::Unpacking a dictionary in iteration without calling .items()

0 comments on commit 9557813

Please sign in to comment.