Skip to content

Commit 9d4f47f

Browse files
committed
Refactored utility function
1 parent 7bfd0a4 commit 9d4f47f

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

pylint/checkers/refactoring/recommendation_checker.py

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,14 @@
11
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22
# For details: https://github.com/PyCQA/pylint/blob/master/LICENSE
33

4-
from typing import Optional, Union, cast
4+
from typing import cast
55

66
import astroid
77

88
from pylint import checkers, interfaces
99
from pylint.checkers import utils
1010

1111

12-
def _check_if_dict_keys_used(
13-
node: Union[astroid.For, astroid.Comprehension]
14-
) -> Optional[str]:
15-
if not isinstance(node.iter, astroid.Call):
16-
# Is it a dictionary?
17-
if not isinstance(node.iter, (astroid.Name, astroid.Attribute)):
18-
return None
19-
inferred = utils.safe_infer(node.iter)
20-
if not isinstance(inferred, (astroid.Dict, astroid.DictComp)):
21-
return None
22-
iterating_object_name = node.iter.as_string()
23-
24-
else:
25-
# Is it a proper keys call?
26-
if (
27-
isinstance(node.iter.func, astroid.Name)
28-
or node.iter.func.attrname != "keys"
29-
):
30-
return None
31-
inferred = utils.safe_infer(node.iter.func)
32-
if not isinstance(inferred, (astroid.BoundMethod, astroid.Dict)):
33-
return None
34-
iterating_object_name = node.iter.as_string().partition(".")[0]
35-
return iterating_object_name
36-
37-
3812
class RecommendationChecker(checkers.BaseChecker):
3913

4014
__implements__ = (interfaces.IAstroidChecker,)
@@ -166,7 +140,7 @@ def _check_consider_using_dict_items(self, node: astroid.For) -> None:
166140
# that the object which is iterated is used as a subscript in the
167141
# body of the for.
168142

169-
iterating_object_name = _check_if_dict_keys_used(node)
143+
iterating_object_name = utils.get_iterating_dictionary_name(node)
170144
if iterating_object_name is None:
171145
return
172146

@@ -210,7 +184,7 @@ def _check_consider_using_dict_items(self, node: astroid.For) -> None:
210184

211185
@utils.check_messages("consider-using-dict-items")
212186
def visit_comprehension(self, node: astroid.Comprehension) -> None:
213-
iterating_object_name = _check_if_dict_keys_used(node)
187+
iterating_object_name = utils.get_iterating_dictionary_name(node)
214188
if iterating_object_name is None:
215189
return
216190

pylint/checkers/utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,3 +1492,33 @@ def is_assign_name_annotated_with(node: astroid.AssignName, typing_name: str) ->
14921492
):
14931493
return True
14941494
return False
1495+
1496+
1497+
def get_iterating_dictionary_name(
1498+
node: Union[astroid.For, astroid.Comprehension]
1499+
) -> Optional[str]:
1500+
"""Get the name of the dictionary which keys are being iterated over on
1501+
a ``astroid.For`` or ``astroid.Comprehension`` node.
1502+
1503+
If the iterating object is not either the keys method of a dictionary
1504+
or a dictionary itself, this returns None
1505+
"""
1506+
if isinstance(node.iter, astroid.Call):
1507+
# Is it a proper keys call?
1508+
if (
1509+
isinstance(node.iter.func, astroid.Name)
1510+
or node.iter.func.attrname != "keys"
1511+
):
1512+
return None
1513+
inferred = safe_infer(node.iter.func)
1514+
if not isinstance(inferred, (astroid.BoundMethod, astroid.Dict)):
1515+
return None
1516+
return node.iter.as_string().partition(".")[0]
1517+
1518+
# Is it a dictionary?
1519+
if not isinstance(node.iter, (astroid.Name, astroid.Attribute)):
1520+
return None
1521+
inferred = safe_infer(node.iter)
1522+
if not isinstance(inferred, (astroid.Dict, astroid.DictComp)):
1523+
return None
1524+
return node.iter.as_string()

0 commit comments

Comments
 (0)