Skip to content

Commit

Permalink
dict-iter-method and dict-view-method no longer determines if the ope…
Browse files Browse the repository at this point in the history
…rand 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.
  • Loading branch information
PCManticore committed Oct 2, 2018
1 parent f9c2ffe commit 540e26d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
24 changes: 4 additions & 20 deletions pylint/checkers/python3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -1201,11 +1186,10 @@ def visit_call(self, node):
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):
Expand Down
26 changes: 14 additions & 12 deletions pylint/test/unittest_checker_python3.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,12 @@ def test_dict_iter_method_on_dict(self):
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:
Expand All @@ -475,9 +481,8 @@ def test_dict_not_iter_method(self):
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):
Expand All @@ -487,7 +492,7 @@ def test_dict_view_method(self):
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
Expand All @@ -496,22 +501,19 @@ def test_dict_view_method_on_dict(self):
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:
message = testutils.Message("dict-view-method", node=node)
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)
Expand Down

0 comments on commit 540e26d

Please sign in to comment.