|
1 | 1 | # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
2 | 2 | # For details: https://github.com/PyCQA/pylint/blob/master/LICENSE
|
3 | 3 |
|
4 |
| -from typing import Optional, Union, cast |
| 4 | +from typing import cast |
5 | 5 |
|
6 | 6 | import astroid
|
7 | 7 |
|
8 | 8 | from pylint import checkers, interfaces
|
9 | 9 | from pylint.checkers import utils
|
10 | 10 |
|
11 | 11 |
|
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 |
| - |
38 | 12 | class RecommendationChecker(checkers.BaseChecker):
|
39 | 13 |
|
40 | 14 | __implements__ = (interfaces.IAstroidChecker,)
|
@@ -166,7 +140,7 @@ def _check_consider_using_dict_items(self, node: astroid.For) -> None:
|
166 | 140 | # that the object which is iterated is used as a subscript in the
|
167 | 141 | # body of the for.
|
168 | 142 |
|
169 |
| - iterating_object_name = _check_if_dict_keys_used(node) |
| 143 | + iterating_object_name = utils.get_iterating_dictionary_name(node) |
170 | 144 | if iterating_object_name is None:
|
171 | 145 | return
|
172 | 146 |
|
@@ -210,7 +184,7 @@ def _check_consider_using_dict_items(self, node: astroid.For) -> None:
|
210 | 184 |
|
211 | 185 | @utils.check_messages("consider-using-dict-items")
|
212 | 186 | 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) |
214 | 188 | if iterating_object_name is None:
|
215 | 189 | return
|
216 | 190 |
|
|
0 commit comments