diff options
author | Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> | 2021-08-31 14:11:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-31 14:11:21 +0200 |
commit | 6cd81934bb9e26cacc3fa5e9165bbaa70066f89b (patch) | |
tree | 2d405f747fe169ccb5deb3fc99e7948faa83b475 | |
parent | e1da3c03d2b91f56b0977480856c2a2a6501758a (diff) | |
download | pylint-git-6cd81934bb9e26cacc3fa5e9165bbaa70066f89b.tar.gz |
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
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | doc/whatsnew/2.11.rst | 4 | ||||
-rw-r--r-- | pylint/checkers/typecheck.py | 4 | ||||
-rw-r--r-- | tests/functional/d/dict_iter_missing_items.py | 3 | ||||
-rw-r--r-- | tests/functional/d/dict_iter_missing_items.txt | 2 |
5 files changed, 16 insertions, 1 deletions
@@ -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 8524c0a90..e91199fb0 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 54a47057a..a79d2fa6b 100644 --- a/pylint/checkers/typecheck.py +++ b/pylint/checkers/typecheck.py @@ -1862,6 +1862,10 @@ accessed. Python regular expressions are accepted.", # 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 333f58990..81977b026 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 @@ for i, v in s1.intersection(s2): 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 3e1ed808c..c3eb12f2c 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() |