Fix false positive `dict-iter-missing-items` for tuple keys (#4939)
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 #3283
diff --git a/ChangeLog b/ChangeLog
index 043639d..aed43aa 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -45,6 +45,10 @@
* 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?
============================
diff --git a/doc/whatsnew/2.11.rst b/doc/whatsnew/2.11.rst
index 8524c0a..e91199f 100644
--- a/doc/whatsnew/2.11.rst
+++ b/doc/whatsnew/2.11.rst
@@ -51,3 +51,7 @@
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
diff --git a/pylint/checkers/typecheck.py b/pylint/checkers/typecheck.py
index 54a4705..a79d2fa 100644
--- a/pylint/checkers/typecheck.py
+++ b/pylint/checkers/typecheck.py
@@ -1862,6 +1862,10 @@
# 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)
diff --git a/tests/functional/d/dict_iter_missing_items.py b/tests/functional/d/dict_iter_missing_items.py
index 333f589..81977b0 100644
--- a/tests/functional/d/dict_iter_missing_items.py
+++ b/tests/functional/d/dict_iter_missing_items.py
@@ -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}
@@ -21,3 +22,5 @@
pass
for k, v in Uninferable:
pass
+for a, b in d_tuple:
+ pass
diff --git a/tests/functional/d/dict_iter_missing_items.txt b/tests/functional/d/dict_iter_missing_items.txt
index 3e1ed80..c3eb12f 100644
--- a/tests/functional/d/dict_iter_missing_items.txt
+++ b/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()