diff --git a/ChangeLog b/ChangeLog index 043639dc52d..aed43aa9f0b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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? ============================ diff --git a/doc/whatsnew/2.11.rst b/doc/whatsnew/2.11.rst index 8524c0a900e..e91199fb02a 100644 --- a/doc/whatsnew/2.11.rst +++ b/doc/whatsnew/2.11.rst @@ -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 diff --git a/pylint/checkers/typecheck.py b/pylint/checkers/typecheck.py index 54a47057a8f..a79d2fa6b0c 100644 --- a/pylint/checkers/typecheck.py +++ b/pylint/checkers/typecheck.py @@ -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) diff --git a/tests/functional/d/dict_iter_missing_items.py b/tests/functional/d/dict_iter_missing_items.py index 333f5899087..81977b0267b 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 3e1ed808c00..c3eb12f2c99 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()