summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2018-10-02 09:59:14 +0200
committerClaudiu Popa <pcmanticore@gmail.com>2018-10-02 10:08:09 +0200
commit540e26db2e05c4f4f555db54d4c046ceff0e7763 (patch)
treef06440fe2ae5435a87efb5a7c04f4faa210fc020
parentf9c2ffe84d235b82583c04677b7ceb370445c303 (diff)
downloadpylint-git-540e26db2e05c4f4f555db54d4c046ceff0e7763.tar.gz
dict-iter-method and dict-view-method no longer determines if the operand is a dictionary
This inhibits the capability of the check of finding occurrences of these methods. There's a low chance of having a false positive.
-rw-r--r--pylint/checkers/python3.py24
-rw-r--r--pylint/test/unittest_checker_python3.py26
2 files changed, 18 insertions, 32 deletions
diff --git a/pylint/checkers/python3.py b/pylint/checkers/python3.py
index 29b5e722d..34be179f0 100644
--- a/pylint/checkers/python3.py
+++ b/pylint/checkers/python3.py
@@ -63,21 +63,6 @@ def _inferred_value_is_dict(value):
return isinstance(value, astroid.Instance) and "dict" in value.basenames
-def _check_dict_node(node):
- inferred_types = set()
- try:
- inferred = node.infer()
- if inferred is not astroid.Uninferable:
- for inferred_node in inferred:
- inferred_types.add(inferred_node)
- except astroid.InferenceError:
- pass
-
- if not inferred_types:
- return True
- return any(_inferred_value_is_dict(value) for value in inferred_types)
-
-
def _is_builtin(node):
return getattr(node, "name", None) in ("__builtin__", "builtins")
@@ -1201,11 +1186,10 @@ class Python3Checker(checkers.BaseChecker):
if node.func.attrname == "next":
self.add_message("next-method-called", node=node)
else:
- if _check_dict_node(node.func.expr):
- if node.func.attrname in ("iterkeys", "itervalues", "iteritems"):
- self.add_message("dict-iter-method", node=node)
- elif node.func.attrname in ("viewkeys", "viewvalues", "viewitems"):
- self.add_message("dict-view-method", node=node)
+ if node.func.attrname in ("iterkeys", "itervalues", "iteritems"):
+ self.add_message("dict-iter-method", node=node)
+ elif node.func.attrname in ("viewkeys", "viewvalues", "viewitems"):
+ self.add_message("dict-view-method", node=node)
elif isinstance(node.func, astroid.Name):
found_node = node.func.lookup(node.func.name)[0]
if _is_builtin(found_node):
diff --git a/pylint/test/unittest_checker_python3.py b/pylint/test/unittest_checker_python3.py
index 184fe053f..99693549a 100644
--- a/pylint/test/unittest_checker_python3.py
+++ b/pylint/test/unittest_checker_python3.py
@@ -464,6 +464,12 @@ class TestPython3Checker(testutils.CheckerTestCase):
class Someclass(dict):
pass
Someclass().iterkeys() #@
+
+ # Emits even though we are not sure they are dicts
+ x.iterkeys() #@
+
+ def func(x):
+ x.iterkeys() #@
"""
)
for node in nodes:
@@ -475,9 +481,8 @@ class TestPython3Checker(testutils.CheckerTestCase):
arg_node = astroid.extract_node("x.iterkeys(x) #@")
stararg_node = astroid.extract_node("x.iterkeys(*x) #@")
kwarg_node = astroid.extract_node("x.iterkeys(y=x) #@")
- non_dict_node = astroid.extract_node("x=[]\nx.iterkeys() #@")
with self.assertNoMessages():
- for node in (arg_node, stararg_node, kwarg_node, non_dict_node):
+ for node in (arg_node, stararg_node, kwarg_node):
self.checker.visit_call(node)
def test_dict_view_method(self):
@@ -487,7 +492,7 @@ class TestPython3Checker(testutils.CheckerTestCase):
with self.assertAddsMessages(message):
self.checker.visit_call(node)
- def test_dict_view_method_on_dict(self):
+ def test_dict_viewkeys(self):
nodes = astroid.extract_node(
"""
from collections import defaultdict
@@ -496,6 +501,12 @@ class TestPython3Checker(testutils.CheckerTestCase):
class Someclass(dict):
pass
Someclass().viewkeys() #@
+
+ # Emits even though they might not be dicts
+ x.viewkeys() #@
+
+ def func(x):
+ x.viewkeys() #@
"""
)
for node in nodes:
@@ -503,15 +514,6 @@ class TestPython3Checker(testutils.CheckerTestCase):
with self.assertAddsMessages(message):
self.checker.visit_call(node)
- def test_dict_not_view_method(self):
- arg_node = astroid.extract_node("x.viewkeys(x) #@")
- stararg_node = astroid.extract_node("x.viewkeys(*x) #@")
- kwarg_node = astroid.extract_node("x.viewkeys(y=x) #@")
- non_dict_node = astroid.extract_node("x=[]\nx.viewkeys() #@")
- with self.assertNoMessages():
- for node in (arg_node, stararg_node, kwarg_node, non_dict_node):
- self.checker.visit_call(node)
-
def test_next_method(self):
node = astroid.extract_node("x.next() #@")
message = testutils.Message("next-method-called", node=node)