fname
stringlengths
63
176
rel_fname
stringclasses
706 values
line
int64
-1
4.5k
name
stringlengths
1
81
kind
stringclasses
2 values
category
stringclasses
2 values
info
stringlengths
0
77.9k
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
91
is_test_condition
ref
function
if not utils.is_test_condition(node, parent):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
102
add_message
ref
function
self.add_message("use-implicit-booleaness-not-len", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
105
infer
ref
function
instance = next(len_arg.infer())
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
109
base_names_of_instance
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
114
instance_has_bool
ref
function
affected_by_pep8 and not self.instance_has_bool(instance)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
116
add_message
ref
function
self.add_message("use-implicit-booleaness-not-len", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
119
instance_has_bool
def
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
127
check_messages
ref
function
@utils.check_messages("use-implicit-booleaness-not-len")
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
128
visit_unaryop
def
function
def visit_unaryop(self, node: nodes.UnaryOp) -> None: """`not len(S)` must become `not S` regardless if the parent block is a test condition or something else (boolean expression) e.g. `if not len(S):` """ if ( isinstance(node, nodes.UnaryOp) and node.op == "not" and utils.is_call_of_name(node.operand, "len") ): self.add_message("use-implicit-booleaness-not-len", node=node) @utils.check_messages("use-implicit-booleaness-not-comparison") def visit_compare(self, node: nodes.Compare) -> None: self._check_use_implicit_booleaness_not_comparison(node) def _check_use_implicit_booleaness_not_comparison( self, node: nodes.Compare ) -> None: """Check for left side and right side of the node for empty literals.""" is_left_empty_literal = utils.is_base_container( node.left ) or utils.is_empty_dict_literal(node.left) # Check both left-hand side and right-hand side for literals for operator, comparator in node.ops: is_right_empty_literal = utils.is_base_container( comparator ) or utils.is_empty_dict_literal(comparator) # Using Exclusive OR (XOR) to compare between two side. # If two sides are both literal, it should be different error. if is_right_empty_literal ^ is_left_empty_literal: # set target_node to opposite side of literal target_node = node.left if is_right_empty_literal else comparator literal_node = comparator if is_right_empty_literal else node.left # Infer node to check target_instance = utils.safe_infer(target_node) if target_instance is None: continue mother_classes = self.base_names_of_instance(target_instance) is_base_comprehension_type = any( t in mother_classes for t in ("tuple", "list", "dict", "set") ) # Only time we bypass check is when target_node is not inherited by # collection literals and have its own __bool__ implementation. if not is_base_comprehension_type and self.instance_has_bool( target_instance ): continue # No need to check for operator when visiting compare node if operator in {"==", "!=", ">=", ">", "<=", "<"}: collection_literal = "{}" if isinstance(literal_node, nodes.List): collection_literal = "[]" if isinstance(literal_node, nodes.Tuple): collection_literal = "()" instance_name = "x" if isinstance(target_node, nodes.Call) and target_node.func: instance_name = f"{target_node.func.as_string()}(...)" elif isinstance(target_node, (nodes.Attribute, nodes.Name)): instance_name = target_node.as_string() original_comparison = ( f"{instance_name} {operator} {collection_literal}" ) suggestion = ( f"{instance_name}" if operator == "!=" else f"not {instance_name}" ) self.add_message( "use-implicit-booleaness-not-comparison", args=( original_comparison, suggestion, ), node=node, ) @staticmethod def base_names_of_instance( node: Union[bases.Uninferable, bases.Instance] ) -> List[str]: """Return all names inherited by a class instance or those returned by a function. The inherited names include 'object'. """ if isinstance(node, bases.Instance): return [node.name] + [x.name for x in node.ancestors()] return []
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
136
is_call_of_name
ref
function
and utils.is_call_of_name(node.operand, "len")
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
138
add_message
ref
function
self.add_message("use-implicit-booleaness-not-len", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
140
check_messages
ref
function
@utils.check_messages("use-implicit-booleaness-not-comparison")
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
141
visit_compare
def
function
def visit_compare(self, node: nodes.Compare) -> None: self._check_use_implicit_booleaness_not_comparison(node) def _check_use_implicit_booleaness_not_comparison( self, node: nodes.Compare ) -> None: """Check for left side and right side of the node for empty literals.""" is_left_empty_literal = utils.is_base_container( node.left ) or utils.is_empty_dict_literal(node.left) # Check both left-hand side and right-hand side for literals for operator, comparator in node.ops: is_right_empty_literal = utils.is_base_container( comparator ) or utils.is_empty_dict_literal(comparator) # Using Exclusive OR (XOR) to compare between two side. # If two sides are both literal, it should be different error. if is_right_empty_literal ^ is_left_empty_literal: # set target_node to opposite side of literal target_node = node.left if is_right_empty_literal else comparator literal_node = comparator if is_right_empty_literal else node.left # Infer node to check target_instance = utils.safe_infer(target_node) if target_instance is None: continue mother_classes = self.base_names_of_instance(target_instance) is_base_comprehension_type = any( t in mother_classes for t in ("tuple", "list", "dict", "set") ) # Only time we bypass check is when target_node is not inherited by # collection literals and have its own __bool__ implementation. if not is_base_comprehension_type and self.instance_has_bool( target_instance ): continue # No need to check for operator when visiting compare node if operator in {"==", "!=", ">=", ">", "<=", "<"}: collection_literal = "{}" if isinstance(literal_node, nodes.List): collection_literal = "[]" if isinstance(literal_node, nodes.Tuple): collection_literal = "()" instance_name = "x" if isinstance(target_node, nodes.Call) and target_node.func: instance_name = f"{target_node.func.as_string()}(...)" elif isinstance(target_node, (nodes.Attribute, nodes.Name)): instance_name = target_node.as_string() original_comparison = ( f"{instance_name} {operator} {collection_literal}" ) suggestion = ( f"{instance_name}" if operator == "!=" else f"not {instance_name}" ) self.add_message( "use-implicit-booleaness-not-comparison", args=( original_comparison, suggestion, ), node=node, ) @staticmethod def base_names_of_instance( node: Union[bases.Uninferable, bases.Instance] ) -> List[str]: """Return all names inherited by a class instance or those returned by a function. The inherited names include 'object'. """ if isinstance(node, bases.Instance): return [node.name] + [x.name for x in node.ancestors()] return []
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
142
_check_use_implicit_booleaness_not_comparison
ref
function
self._check_use_implicit_booleaness_not_comparison(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
144
_check_use_implicit_booleaness_not_comparison
def
function
def _check_use_implicit_booleaness_not_comparison( self, node: nodes.Compare ) -> None: """Check for left side and right side of the node for empty literals.""" is_left_empty_literal = utils.is_base_container( node.left ) or utils.is_empty_dict_literal(node.left) # Check both left-hand side and right-hand side for literals for operator, comparator in node.ops: is_right_empty_literal = utils.is_base_container( comparator ) or utils.is_empty_dict_literal(comparator) # Using Exclusive OR (XOR) to compare between two side. # If two sides are both literal, it should be different error. if is_right_empty_literal ^ is_left_empty_literal: # set target_node to opposite side of literal target_node = node.left if is_right_empty_literal else comparator literal_node = comparator if is_right_empty_literal else node.left # Infer node to check target_instance = utils.safe_infer(target_node) if target_instance is None: continue mother_classes = self.base_names_of_instance(target_instance) is_base_comprehension_type = any( t in mother_classes for t in ("tuple", "list", "dict", "set") ) # Only time we bypass check is when target_node is not inherited by # collection literals and have its own __bool__ implementation. if not is_base_comprehension_type and self.instance_has_bool( target_instance ): continue # No need to check for operator when visiting compare node if operator in {"==", "!=", ">=", ">", "<=", "<"}: collection_literal = "{}" if isinstance(literal_node, nodes.List): collection_literal = "[]" if isinstance(literal_node, nodes.Tuple): collection_literal = "()" instance_name = "x" if isinstance(target_node, nodes.Call) and target_node.func: instance_name = f"{target_node.func.as_string()}(...)" elif isinstance(target_node, (nodes.Attribute, nodes.Name)): instance_name = target_node.as_string() original_comparison = ( f"{instance_name} {operator} {collection_literal}" ) suggestion = ( f"{instance_name}" if operator == "!=" else f"not {instance_name}" ) self.add_message( "use-implicit-booleaness-not-comparison", args=( original_comparison, suggestion, ), node=node, ) @staticmethod def base_names_of_instance( node: Union[bases.Uninferable, bases.Instance] ) -> List[str]: """Return all names inherited by a class instance or those returned by a function. The inherited names include 'object'. """ if isinstance(node, bases.Instance): return [node.name] + [x.name for x in node.ancestors()] return []
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
148
is_base_container
ref
function
is_left_empty_literal = utils.is_base_container(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
150
is_empty_dict_literal
ref
function
) or utils.is_empty_dict_literal(node.left)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
154
is_base_container
ref
function
is_right_empty_literal = utils.is_base_container(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
156
is_empty_dict_literal
ref
function
) or utils.is_empty_dict_literal(comparator)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
164
safe_infer
ref
function
target_instance = utils.safe_infer(target_node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
167
base_names_of_instance
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
174
instance_has_bool
ref
function
if not is_base_comprehension_type and self.instance_has_bool(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
189
as_string
ref
function
instance_name = f"{target_node.func.as_string()}(...)"
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
191
as_string
ref
function
instance_name = target_node.as_string()
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
201
add_message
ref
function
self.add_message(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
211
base_names_of_instance
def
function
def base_names_of_instance( node: Union[bases.Uninferable, bases.Instance] ) -> List[str]: """Return all names inherited by a class instance or those returned by a function. The inherited names include 'object'. """ if isinstance(node, bases.Instance): return [node.name] + [x.name for x in node.ancestors()] return []
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py
pylint/checkers/refactoring/implicit_booleaness_checker.py
219
ancestors
ref
function
return [node.name] + [x.name for x in node.ancestors()]
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/not_checker.py
pylint/checkers/refactoring/not_checker.py
11
NotChecker
def
class
visit_unaryop
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/not_checker.py
pylint/checkers/refactoring/not_checker.py
43
check_messages
ref
function
@utils.check_messages("unneeded-not")
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/not_checker.py
pylint/checkers/refactoring/not_checker.py
44
visit_unaryop
def
function
def visit_unaryop(self, node: nodes.UnaryOp) -> None: if node.op != "not": return operand = node.operand if isinstance(operand, nodes.UnaryOp) and operand.op == "not": self.add_message( "unneeded-not", node=node, args=(node.as_string(), operand.operand.as_string()), ) elif isinstance(operand, nodes.Compare): left = operand.left # ignore multiple comparisons if len(operand.ops) > 1: return operator, right = operand.ops[0] if operator not in self.reverse_op: return # Ignore __ne__ as function of __eq__ frame = node.frame(future=_True) if frame.name == "__ne__" and operator == "==": return for _type in (utils.node_type(left), utils.node_type(right)): if not _type: return if isinstance(_type, self.skipped_nodes): return if ( isinstance(_type, astroid.Instance) and _type.qname() in self.skipped_classnames ): return suggestion = ( f"{left.as_string()} {self.reverse_op[operator]} {right.as_string()}" ) self.add_message( "unneeded-not", node=node, args=(node.as_string(), suggestion) )
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/not_checker.py
pylint/checkers/refactoring/not_checker.py
50
add_message
ref
function
self.add_message(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/not_checker.py
pylint/checkers/refactoring/not_checker.py
53
as_string
ref
function
args=(node.as_string(), operand.operand.as_string()),
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/not_checker.py
pylint/checkers/refactoring/not_checker.py
53
as_string
ref
function
args=(node.as_string(), operand.operand.as_string()),
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/not_checker.py
pylint/checkers/refactoring/not_checker.py
64
frame
ref
function
frame = node.frame(future=True)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/not_checker.py
pylint/checkers/refactoring/not_checker.py
67
node_type
ref
function
for _type in (utils.node_type(left), utils.node_type(right)):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/not_checker.py
pylint/checkers/refactoring/not_checker.py
67
node_type
ref
function
for _type in (utils.node_type(left), utils.node_type(right)):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/not_checker.py
pylint/checkers/refactoring/not_checker.py
74
qname
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/not_checker.py
pylint/checkers/refactoring/not_checker.py
78
as_string
ref
function
f"{left.as_string()} {self.reverse_op[operator]} {right.as_string()}"
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/not_checker.py
pylint/checkers/refactoring/not_checker.py
78
as_string
ref
function
f"{left.as_string()} {self.reverse_op[operator]} {right.as_string()}"
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/not_checker.py
pylint/checkers/refactoring/not_checker.py
80
add_message
ref
function
self.add_message(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/not_checker.py
pylint/checkers/refactoring/not_checker.py
81
as_string
ref
function
"unneeded-not", node=node, args=(node.as_string(), suggestion)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
12
RecommendationChecker
def
class
open _is_builtin visit_call _check_consider_iterating_dictionary _check_use_maxsplit_arg visit_for _check_consider_using_enumerate _check_consider_using_dict_items visit_comprehension _check_consider_using_dict_items_comprehension _check_use_sequence_for_iteration visit_const _detect_replacable_format_call
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
65
get_global_option
ref
function
py_version = get_global_option(self, "py-version")
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
69
_is_builtin
def
function
def _is_builtin(node, function): inferred = utils.safe_infer(node) if not inferred: return _False return utils.is_builtin_object(inferred) and inferred.name == function @utils.check_messages("consider-iterating-dictionary", "use-maxsplit-arg") def visit_call(self, node: nodes.Call) -> None: self._check_consider_iterating_dictionary(node) self._check_use_maxsplit_arg(node) def _check_consider_iterating_dictionary(self, node: nodes.Call) -> None: if not isinstance(node.func, nodes.Attribute): return if node.func.attrname != "keys": return comp_ancestor = utils.get_node_first_ancestor_of_type(node, nodes.Compare) if ( isinstance(node.parent, (nodes.For, nodes.Comprehension)) or comp_ancestor and any( op for op, comparator in comp_ancestor.ops if op in {"in", "not in"} and (comparator in node.node_ancestors() or comparator is node) ) ): inferred = utils.safe_infer(node.func) if not isinstance(inferred, astroid.BoundMethod) or not isinstance( inferred.bound, nodes.Dict ): return self.add_message("consider-iterating-dictionary", node=node) def _check_use_maxsplit_arg(self, node: nodes.Call) -> None: """Add message when accessing first or last elements of a str.split() or str.rsplit().""" # Check if call is split() or rsplit() if not ( isinstance(node.func, nodes.Attribute) and node.func.attrname in {"split", "rsplit"} and isinstance(utils.safe_infer(node.func), astroid.BoundMethod) ): return try: sep = utils.get_argument_from_call(node, 0, "sep") except utils.NoSuchArgumentError: return try: # Ignore if maxsplit arg has been set utils.get_argument_from_call(node, 1, "maxsplit") return except utils.NoSuchArgumentError: pass if isinstance(node.parent, nodes.Subscript): try: subscript_value = utils.get_subscript_const_value(node.parent).value except utils.InferredTypeError: return # Check for cases where variable (Name) subscripts may be mutated within a loop if isinstance(node.parent.slice, nodes.Name): # Check if loop present within the scope of the node scope = node.scope() for loop_node in scope.nodes_of_class((nodes.For, nodes.While)): if not loop_node.parent_of(node): continue # Check if var is mutated within loop (Assign/AugAssign) for assignment_node in loop_node.nodes_of_class(nodes.AugAssign): if node.parent.slice.name == assignment_node.target.name: return for assignment_node in loop_node.nodes_of_class(nodes.Assign): if node.parent.slice.name in [ n.name for n in assignment_node.targets ]: return if subscript_value in (-1, 0): fn_name = node.func.attrname new_fn = "rsplit" if subscript_value == -1 else "split" new_name = ( node.func.as_string().rsplit(fn_name, maxsplit=1)[0] + new_fn + f"({sep.as_string()}, maxsplit=1)[{subscript_value}]" ) self.add_message("use-maxsplit-arg", node=node, args=(new_name,)) @utils.check_messages( "consider-using-enumerate", "consider-using-dict-items", "use-sequence-for-iteration", ) def visit_for(self, node: nodes.For) -> None: self._check_consider_using_enumerate(node) self._check_consider_using_dict_items(node) self._check_use_sequence_for_iteration(node) def _check_consider_using_enumerate(self, node: nodes.For) -> None: """Emit a convention whenever range and len are used for indexing.""" # Verify that we have a `range([start], len(...), [stop])` call and # that the object which is iterated is used as a subscript in the # body of the for. # Is it a proper range call? if not isinstance(node.iter, nodes.Call): return if not self._is_builtin(node.iter.func, "range"): return if not node.iter.args: return is_constant_zero = ( isinstance(node.iter.args[0], nodes.Const) and node.iter.args[0].value == 0 ) if len(node.iter.args) == 2 and not is_constant_zero: return if len(node.iter.args) > 2: return # Is it a proper len call? if not isinstance(node.iter.args[-1], nodes.Call): return second_func = node.iter.args[-1].func if not self._is_builtin(second_func, "len"): return len_args = node.iter.args[-1].args if not len_args or len(len_args) != 1: return iterating_object = len_args[0] if isinstance(iterating_object, nodes.Name): expected_subscript_val_type = nodes.Name elif isinstance(iterating_object, nodes.Attribute): expected_subscript_val_type = nodes.Attribute else: return # If we're defining __iter__ on self, enumerate won't work scope = node.scope() if ( isinstance(iterating_object, nodes.Name) and iterating_object.name == "self" and scope.name == "__iter__" ): return # Verify that the body of the for loop uses a subscript # with the object that was iterated. This uses some heuristics # in order to make sure that the same object is used in the # for body. for child in node.body: for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, expected_subscript_val_type): continue value = subscript.slice if not isinstance(value, nodes.Name): continue if subscript.value.scope() != node.scope(): # Ignore this subscript if it's not in the same # scope. This means that in the body of the for # loop, another scope was created, where the same # name for the iterating object was used. continue if value.name == node.target.name and ( isinstance(subscript.value, nodes.Name) and iterating_object.name == subscript.value.name or isinstance(subscript.value, nodes.Attribute) and iterating_object.attrname == subscript.value.attrname ): self.add_message("consider-using-enumerate", node=node) return def _check_consider_using_dict_items(self, node: nodes.For) -> None: """Add message when accessing dict values by index lookup.""" # Verify that we have a .keys() call and # that the object which is iterated is used as a subscript in the # body of the for. iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return # Verify that the body of the for loop uses a subscript # with the object that was iterated. This uses some heuristics # in order to make sure that the same object is used in the # for body. for child in node.body: for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue last_definition_lineno = value.lookup(value.name)[1][-1].lineno if last_definition_lineno > node.lineno: # Ignore this subscript if it has been redefined after # the for loop. This checks for the line number using .lookup() # to get the line number where the iterating object was last # defined and compare that to the for loop's line number continue if ( isinstance(subscript.parent, nodes.Assign) and subscript in subscript.parent.targets or isinstance(subscript.parent, nodes.AugAssign) and subscript == subscript.parent.target ): # Ignore this subscript if it is the target of an assignment # Early termination as dict index lookup is necessary return self.add_message("consider-using-dict-items", node=node) return @utils.check_messages( "consider-using-dict-items", "use-sequence-for-iteration", ) def visit_comprehension(self, node: nodes.Comprehension) -> None: self._check_consider_using_dict_items_comprehension(node) self._check_use_sequence_for_iteration(node) def _check_consider_using_dict_items_comprehension( self, node: nodes.Comprehension ) -> None: """Add message when accessing dict values by index lookup.""" iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return for child in node.parent.get_children(): for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue self.add_message("consider-using-dict-items", node=node) return def _check_use_sequence_for_iteration( self, node: Union[nodes.For, nodes.Comprehension] ) -> None: """Check if code iterates over an in-place defined set.""" if isinstance(node.iter, nodes.Set): self.add_message("use-sequence-for-iteration", node=node.iter) @utils.check_messages("consider-using-f-string") def visit_const(self, node: nodes.Const) -> None: if self._py36_plus: # f-strings require Python 3.6 if node.pytype() == "builtins.str" and not isinstance( node.parent, nodes.JoinedStr ): self._detect_replacable_format_call(node) def _detect_replacable_format_call(self, node: nodes.Const) -> None: """Check whether a string is used in a call to format() or '%' and whether it can be replaced by an f-string """ if ( isinstance(node.parent, nodes.Attribute) and node.parent.attrname == "format" ): # Don't warn on referencing / assigning .format without calling it if not isinstance(node.parent.parent, nodes.Call): return if node.parent.parent.args: for arg in node.parent.parent.args: # If star expressions with more than 1 element are being used if isinstance(arg, nodes.Starred): inferred = utils.safe_infer(arg.value) if ( isinstance(inferred, astroid.List) and len(inferred.elts) > 1 ): return # Backslashes can't be in f-string expressions if "\\" in arg.as_string(): return elif node.parent.parent.keywords: keyword_args = [ i[0] for i in utils.parse_format_method_string(node.value)[0] ] for keyword in node.parent.parent.keywords: # If keyword is used multiple times if keyword_args.count(keyword.arg) > 1: return keyword = utils.safe_infer(keyword.value) # If lists of more than one element are being unpacked if isinstance(keyword, nodes.Dict): if len(keyword.items) > 1 and len(keyword_args) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, ) elif isinstance(node.parent, nodes.BinOp) and node.parent.op == "%": # Backslashes can't be in f-string expressions if "\\" in node.parent.right.as_string(): return inferred_right = utils.safe_infer(node.parent.right) # If dicts or lists of length > 1 are used if isinstance(inferred_right, nodes.Dict): if len(inferred_right.items) > 1: return elif isinstance(inferred_right, nodes.List): if len(inferred_right.elts) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, )
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
70
safe_infer
ref
function
inferred = utils.safe_infer(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
73
is_builtin_object
ref
function
return utils.is_builtin_object(inferred) and inferred.name == function
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
75
check_messages
ref
function
@utils.check_messages("consider-iterating-dictionary", "use-maxsplit-arg")
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
76
visit_call
def
function
def visit_call(self, node: nodes.Call) -> None: self._check_consider_iterating_dictionary(node) self._check_use_maxsplit_arg(node) def _check_consider_iterating_dictionary(self, node: nodes.Call) -> None: if not isinstance(node.func, nodes.Attribute): return if node.func.attrname != "keys": return comp_ancestor = utils.get_node_first_ancestor_of_type(node, nodes.Compare) if ( isinstance(node.parent, (nodes.For, nodes.Comprehension)) or comp_ancestor and any( op for op, comparator in comp_ancestor.ops if op in {"in", "not in"} and (comparator in node.node_ancestors() or comparator is node) ) ): inferred = utils.safe_infer(node.func) if not isinstance(inferred, astroid.BoundMethod) or not isinstance( inferred.bound, nodes.Dict ): return self.add_message("consider-iterating-dictionary", node=node) def _check_use_maxsplit_arg(self, node: nodes.Call) -> None: """Add message when accessing first or last elements of a str.split() or str.rsplit().""" # Check if call is split() or rsplit() if not ( isinstance(node.func, nodes.Attribute) and node.func.attrname in {"split", "rsplit"} and isinstance(utils.safe_infer(node.func), astroid.BoundMethod) ): return try: sep = utils.get_argument_from_call(node, 0, "sep") except utils.NoSuchArgumentError: return try: # Ignore if maxsplit arg has been set utils.get_argument_from_call(node, 1, "maxsplit") return except utils.NoSuchArgumentError: pass if isinstance(node.parent, nodes.Subscript): try: subscript_value = utils.get_subscript_const_value(node.parent).value except utils.InferredTypeError: return # Check for cases where variable (Name) subscripts may be mutated within a loop if isinstance(node.parent.slice, nodes.Name): # Check if loop present within the scope of the node scope = node.scope() for loop_node in scope.nodes_of_class((nodes.For, nodes.While)): if not loop_node.parent_of(node): continue # Check if var is mutated within loop (Assign/AugAssign) for assignment_node in loop_node.nodes_of_class(nodes.AugAssign): if node.parent.slice.name == assignment_node.target.name: return for assignment_node in loop_node.nodes_of_class(nodes.Assign): if node.parent.slice.name in [ n.name for n in assignment_node.targets ]: return if subscript_value in (-1, 0): fn_name = node.func.attrname new_fn = "rsplit" if subscript_value == -1 else "split" new_name = ( node.func.as_string().rsplit(fn_name, maxsplit=1)[0] + new_fn + f"({sep.as_string()}, maxsplit=1)[{subscript_value}]" ) self.add_message("use-maxsplit-arg", node=node, args=(new_name,)) @utils.check_messages( "consider-using-enumerate", "consider-using-dict-items", "use-sequence-for-iteration", ) def visit_for(self, node: nodes.For) -> None: self._check_consider_using_enumerate(node) self._check_consider_using_dict_items(node) self._check_use_sequence_for_iteration(node) def _check_consider_using_enumerate(self, node: nodes.For) -> None: """Emit a convention whenever range and len are used for indexing.""" # Verify that we have a `range([start], len(...), [stop])` call and # that the object which is iterated is used as a subscript in the # body of the for. # Is it a proper range call? if not isinstance(node.iter, nodes.Call): return if not self._is_builtin(node.iter.func, "range"): return if not node.iter.args: return is_constant_zero = ( isinstance(node.iter.args[0], nodes.Const) and node.iter.args[0].value == 0 ) if len(node.iter.args) == 2 and not is_constant_zero: return if len(node.iter.args) > 2: return # Is it a proper len call? if not isinstance(node.iter.args[-1], nodes.Call): return second_func = node.iter.args[-1].func if not self._is_builtin(second_func, "len"): return len_args = node.iter.args[-1].args if not len_args or len(len_args) != 1: return iterating_object = len_args[0] if isinstance(iterating_object, nodes.Name): expected_subscript_val_type = nodes.Name elif isinstance(iterating_object, nodes.Attribute): expected_subscript_val_type = nodes.Attribute else: return # If we're defining __iter__ on self, enumerate won't work scope = node.scope() if ( isinstance(iterating_object, nodes.Name) and iterating_object.name == "self" and scope.name == "__iter__" ): return # Verify that the body of the for loop uses a subscript # with the object that was iterated. This uses some heuristics # in order to make sure that the same object is used in the # for body. for child in node.body: for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, expected_subscript_val_type): continue value = subscript.slice if not isinstance(value, nodes.Name): continue if subscript.value.scope() != node.scope(): # Ignore this subscript if it's not in the same # scope. This means that in the body of the for # loop, another scope was created, where the same # name for the iterating object was used. continue if value.name == node.target.name and ( isinstance(subscript.value, nodes.Name) and iterating_object.name == subscript.value.name or isinstance(subscript.value, nodes.Attribute) and iterating_object.attrname == subscript.value.attrname ): self.add_message("consider-using-enumerate", node=node) return def _check_consider_using_dict_items(self, node: nodes.For) -> None: """Add message when accessing dict values by index lookup.""" # Verify that we have a .keys() call and # that the object which is iterated is used as a subscript in the # body of the for. iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return # Verify that the body of the for loop uses a subscript # with the object that was iterated. This uses some heuristics # in order to make sure that the same object is used in the # for body. for child in node.body: for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue last_definition_lineno = value.lookup(value.name)[1][-1].lineno if last_definition_lineno > node.lineno: # Ignore this subscript if it has been redefined after # the for loop. This checks for the line number using .lookup() # to get the line number where the iterating object was last # defined and compare that to the for loop's line number continue if ( isinstance(subscript.parent, nodes.Assign) and subscript in subscript.parent.targets or isinstance(subscript.parent, nodes.AugAssign) and subscript == subscript.parent.target ): # Ignore this subscript if it is the target of an assignment # Early termination as dict index lookup is necessary return self.add_message("consider-using-dict-items", node=node) return @utils.check_messages( "consider-using-dict-items", "use-sequence-for-iteration", ) def visit_comprehension(self, node: nodes.Comprehension) -> None: self._check_consider_using_dict_items_comprehension(node) self._check_use_sequence_for_iteration(node) def _check_consider_using_dict_items_comprehension( self, node: nodes.Comprehension ) -> None: """Add message when accessing dict values by index lookup.""" iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return for child in node.parent.get_children(): for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue self.add_message("consider-using-dict-items", node=node) return def _check_use_sequence_for_iteration( self, node: Union[nodes.For, nodes.Comprehension] ) -> None: """Check if code iterates over an in-place defined set.""" if isinstance(node.iter, nodes.Set): self.add_message("use-sequence-for-iteration", node=node.iter) @utils.check_messages("consider-using-f-string") def visit_const(self, node: nodes.Const) -> None: if self._py36_plus: # f-strings require Python 3.6 if node.pytype() == "builtins.str" and not isinstance( node.parent, nodes.JoinedStr ): self._detect_replacable_format_call(node) def _detect_replacable_format_call(self, node: nodes.Const) -> None: """Check whether a string is used in a call to format() or '%' and whether it can be replaced by an f-string """ if ( isinstance(node.parent, nodes.Attribute) and node.parent.attrname == "format" ): # Don't warn on referencing / assigning .format without calling it if not isinstance(node.parent.parent, nodes.Call): return if node.parent.parent.args: for arg in node.parent.parent.args: # If star expressions with more than 1 element are being used if isinstance(arg, nodes.Starred): inferred = utils.safe_infer(arg.value) if ( isinstance(inferred, astroid.List) and len(inferred.elts) > 1 ): return # Backslashes can't be in f-string expressions if "\\" in arg.as_string(): return elif node.parent.parent.keywords: keyword_args = [ i[0] for i in utils.parse_format_method_string(node.value)[0] ] for keyword in node.parent.parent.keywords: # If keyword is used multiple times if keyword_args.count(keyword.arg) > 1: return keyword = utils.safe_infer(keyword.value) # If lists of more than one element are being unpacked if isinstance(keyword, nodes.Dict): if len(keyword.items) > 1 and len(keyword_args) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, ) elif isinstance(node.parent, nodes.BinOp) and node.parent.op == "%": # Backslashes can't be in f-string expressions if "\\" in node.parent.right.as_string(): return inferred_right = utils.safe_infer(node.parent.right) # If dicts or lists of length > 1 are used if isinstance(inferred_right, nodes.Dict): if len(inferred_right.items) > 1: return elif isinstance(inferred_right, nodes.List): if len(inferred_right.elts) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, )
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
77
_check_consider_iterating_dictionary
ref
function
self._check_consider_iterating_dictionary(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
78
_check_use_maxsplit_arg
ref
function
self._check_use_maxsplit_arg(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
80
_check_consider_iterating_dictionary
def
function
def _check_consider_iterating_dictionary(self, node: nodes.Call) -> None: if not isinstance(node.func, nodes.Attribute): return if node.func.attrname != "keys": return comp_ancestor = utils.get_node_first_ancestor_of_type(node, nodes.Compare) if ( isinstance(node.parent, (nodes.For, nodes.Comprehension)) or comp_ancestor and any( op for op, comparator in comp_ancestor.ops if op in {"in", "not in"} and (comparator in node.node_ancestors() or comparator is node) ) ): inferred = utils.safe_infer(node.func) if not isinstance(inferred, astroid.BoundMethod) or not isinstance( inferred.bound, nodes.Dict ): return self.add_message("consider-iterating-dictionary", node=node) def _check_use_maxsplit_arg(self, node: nodes.Call) -> None: """Add message when accessing first or last elements of a str.split() or str.rsplit().""" # Check if call is split() or rsplit() if not ( isinstance(node.func, nodes.Attribute) and node.func.attrname in {"split", "rsplit"} and isinstance(utils.safe_infer(node.func), astroid.BoundMethod) ): return try: sep = utils.get_argument_from_call(node, 0, "sep") except utils.NoSuchArgumentError: return try: # Ignore if maxsplit arg has been set utils.get_argument_from_call(node, 1, "maxsplit") return except utils.NoSuchArgumentError: pass if isinstance(node.parent, nodes.Subscript): try: subscript_value = utils.get_subscript_const_value(node.parent).value except utils.InferredTypeError: return # Check for cases where variable (Name) subscripts may be mutated within a loop if isinstance(node.parent.slice, nodes.Name): # Check if loop present within the scope of the node scope = node.scope() for loop_node in scope.nodes_of_class((nodes.For, nodes.While)): if not loop_node.parent_of(node): continue # Check if var is mutated within loop (Assign/AugAssign) for assignment_node in loop_node.nodes_of_class(nodes.AugAssign): if node.parent.slice.name == assignment_node.target.name: return for assignment_node in loop_node.nodes_of_class(nodes.Assign): if node.parent.slice.name in [ n.name for n in assignment_node.targets ]: return if subscript_value in (-1, 0): fn_name = node.func.attrname new_fn = "rsplit" if subscript_value == -1 else "split" new_name = ( node.func.as_string().rsplit(fn_name, maxsplit=1)[0] + new_fn + f"({sep.as_string()}, maxsplit=1)[{subscript_value}]" ) self.add_message("use-maxsplit-arg", node=node, args=(new_name,)) @utils.check_messages( "consider-using-enumerate", "consider-using-dict-items", "use-sequence-for-iteration", ) def visit_for(self, node: nodes.For) -> None: self._check_consider_using_enumerate(node) self._check_consider_using_dict_items(node) self._check_use_sequence_for_iteration(node) def _check_consider_using_enumerate(self, node: nodes.For) -> None: """Emit a convention whenever range and len are used for indexing.""" # Verify that we have a `range([start], len(...), [stop])` call and # that the object which is iterated is used as a subscript in the # body of the for. # Is it a proper range call? if not isinstance(node.iter, nodes.Call): return if not self._is_builtin(node.iter.func, "range"): return if not node.iter.args: return is_constant_zero = ( isinstance(node.iter.args[0], nodes.Const) and node.iter.args[0].value == 0 ) if len(node.iter.args) == 2 and not is_constant_zero: return if len(node.iter.args) > 2: return # Is it a proper len call? if not isinstance(node.iter.args[-1], nodes.Call): return second_func = node.iter.args[-1].func if not self._is_builtin(second_func, "len"): return len_args = node.iter.args[-1].args if not len_args or len(len_args) != 1: return iterating_object = len_args[0] if isinstance(iterating_object, nodes.Name): expected_subscript_val_type = nodes.Name elif isinstance(iterating_object, nodes.Attribute): expected_subscript_val_type = nodes.Attribute else: return # If we're defining __iter__ on self, enumerate won't work scope = node.scope() if ( isinstance(iterating_object, nodes.Name) and iterating_object.name == "self" and scope.name == "__iter__" ): return # Verify that the body of the for loop uses a subscript # with the object that was iterated. This uses some heuristics # in order to make sure that the same object is used in the # for body. for child in node.body: for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, expected_subscript_val_type): continue value = subscript.slice if not isinstance(value, nodes.Name): continue if subscript.value.scope() != node.scope(): # Ignore this subscript if it's not in the same # scope. This means that in the body of the for # loop, another scope was created, where the same # name for the iterating object was used. continue if value.name == node.target.name and ( isinstance(subscript.value, nodes.Name) and iterating_object.name == subscript.value.name or isinstance(subscript.value, nodes.Attribute) and iterating_object.attrname == subscript.value.attrname ): self.add_message("consider-using-enumerate", node=node) return def _check_consider_using_dict_items(self, node: nodes.For) -> None: """Add message when accessing dict values by index lookup.""" # Verify that we have a .keys() call and # that the object which is iterated is used as a subscript in the # body of the for. iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return # Verify that the body of the for loop uses a subscript # with the object that was iterated. This uses some heuristics # in order to make sure that the same object is used in the # for body. for child in node.body: for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue last_definition_lineno = value.lookup(value.name)[1][-1].lineno if last_definition_lineno > node.lineno: # Ignore this subscript if it has been redefined after # the for loop. This checks for the line number using .lookup() # to get the line number where the iterating object was last # defined and compare that to the for loop's line number continue if ( isinstance(subscript.parent, nodes.Assign) and subscript in subscript.parent.targets or isinstance(subscript.parent, nodes.AugAssign) and subscript == subscript.parent.target ): # Ignore this subscript if it is the target of an assignment # Early termination as dict index lookup is necessary return self.add_message("consider-using-dict-items", node=node) return @utils.check_messages( "consider-using-dict-items", "use-sequence-for-iteration", ) def visit_comprehension(self, node: nodes.Comprehension) -> None: self._check_consider_using_dict_items_comprehension(node) self._check_use_sequence_for_iteration(node) def _check_consider_using_dict_items_comprehension( self, node: nodes.Comprehension ) -> None: """Add message when accessing dict values by index lookup.""" iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return for child in node.parent.get_children(): for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue self.add_message("consider-using-dict-items", node=node) return def _check_use_sequence_for_iteration( self, node: Union[nodes.For, nodes.Comprehension] ) -> None: """Check if code iterates over an in-place defined set.""" if isinstance(node.iter, nodes.Set): self.add_message("use-sequence-for-iteration", node=node.iter) @utils.check_messages("consider-using-f-string") def visit_const(self, node: nodes.Const) -> None: if self._py36_plus: # f-strings require Python 3.6 if node.pytype() == "builtins.str" and not isinstance( node.parent, nodes.JoinedStr ): self._detect_replacable_format_call(node) def _detect_replacable_format_call(self, node: nodes.Const) -> None: """Check whether a string is used in a call to format() or '%' and whether it can be replaced by an f-string """ if ( isinstance(node.parent, nodes.Attribute) and node.parent.attrname == "format" ): # Don't warn on referencing / assigning .format without calling it if not isinstance(node.parent.parent, nodes.Call): return if node.parent.parent.args: for arg in node.parent.parent.args: # If star expressions with more than 1 element are being used if isinstance(arg, nodes.Starred): inferred = utils.safe_infer(arg.value) if ( isinstance(inferred, astroid.List) and len(inferred.elts) > 1 ): return # Backslashes can't be in f-string expressions if "\\" in arg.as_string(): return elif node.parent.parent.keywords: keyword_args = [ i[0] for i in utils.parse_format_method_string(node.value)[0] ] for keyword in node.parent.parent.keywords: # If keyword is used multiple times if keyword_args.count(keyword.arg) > 1: return keyword = utils.safe_infer(keyword.value) # If lists of more than one element are being unpacked if isinstance(keyword, nodes.Dict): if len(keyword.items) > 1 and len(keyword_args) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, ) elif isinstance(node.parent, nodes.BinOp) and node.parent.op == "%": # Backslashes can't be in f-string expressions if "\\" in node.parent.right.as_string(): return inferred_right = utils.safe_infer(node.parent.right) # If dicts or lists of length > 1 are used if isinstance(inferred_right, nodes.Dict): if len(inferred_right.items) > 1: return elif isinstance(inferred_right, nodes.List): if len(inferred_right.elts) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, )
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
85
get_node_first_ancestor_of_type
ref
function
comp_ancestor = utils.get_node_first_ancestor_of_type(node, nodes.Compare)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
93
node_ancestors
ref
function
and (comparator in node.node_ancestors() or comparator is node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
96
safe_infer
ref
function
inferred = utils.safe_infer(node.func)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
101
add_message
ref
function
self.add_message("consider-iterating-dictionary", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
103
_check_use_maxsplit_arg
def
function
def _check_use_maxsplit_arg(self, node: nodes.Call) -> None: """Add message when accessing first or last elements of a str.split() or str.rsplit().""" # Check if call is split() or rsplit() if not ( isinstance(node.func, nodes.Attribute) and node.func.attrname in {"split", "rsplit"} and isinstance(utils.safe_infer(node.func), astroid.BoundMethod) ): return try: sep = utils.get_argument_from_call(node, 0, "sep") except utils.NoSuchArgumentError: return try: # Ignore if maxsplit arg has been set utils.get_argument_from_call(node, 1, "maxsplit") return except utils.NoSuchArgumentError: pass if isinstance(node.parent, nodes.Subscript): try: subscript_value = utils.get_subscript_const_value(node.parent).value except utils.InferredTypeError: return # Check for cases where variable (Name) subscripts may be mutated within a loop if isinstance(node.parent.slice, nodes.Name): # Check if loop present within the scope of the node scope = node.scope() for loop_node in scope.nodes_of_class((nodes.For, nodes.While)): if not loop_node.parent_of(node): continue # Check if var is mutated within loop (Assign/AugAssign) for assignment_node in loop_node.nodes_of_class(nodes.AugAssign): if node.parent.slice.name == assignment_node.target.name: return for assignment_node in loop_node.nodes_of_class(nodes.Assign): if node.parent.slice.name in [ n.name for n in assignment_node.targets ]: return if subscript_value in (-1, 0): fn_name = node.func.attrname new_fn = "rsplit" if subscript_value == -1 else "split" new_name = ( node.func.as_string().rsplit(fn_name, maxsplit=1)[0] + new_fn + f"({sep.as_string()}, maxsplit=1)[{subscript_value}]" ) self.add_message("use-maxsplit-arg", node=node, args=(new_name,)) @utils.check_messages( "consider-using-enumerate", "consider-using-dict-items", "use-sequence-for-iteration", ) def visit_for(self, node: nodes.For) -> None: self._check_consider_using_enumerate(node) self._check_consider_using_dict_items(node) self._check_use_sequence_for_iteration(node) def _check_consider_using_enumerate(self, node: nodes.For) -> None: """Emit a convention whenever range and len are used for indexing.""" # Verify that we have a `range([start], len(...), [stop])` call and # that the object which is iterated is used as a subscript in the # body of the for. # Is it a proper range call? if not isinstance(node.iter, nodes.Call): return if not self._is_builtin(node.iter.func, "range"): return if not node.iter.args: return is_constant_zero = ( isinstance(node.iter.args[0], nodes.Const) and node.iter.args[0].value == 0 ) if len(node.iter.args) == 2 and not is_constant_zero: return if len(node.iter.args) > 2: return # Is it a proper len call? if not isinstance(node.iter.args[-1], nodes.Call): return second_func = node.iter.args[-1].func if not self._is_builtin(second_func, "len"): return len_args = node.iter.args[-1].args if not len_args or len(len_args) != 1: return iterating_object = len_args[0] if isinstance(iterating_object, nodes.Name): expected_subscript_val_type = nodes.Name elif isinstance(iterating_object, nodes.Attribute): expected_subscript_val_type = nodes.Attribute else: return # If we're defining __iter__ on self, enumerate won't work scope = node.scope() if ( isinstance(iterating_object, nodes.Name) and iterating_object.name == "self" and scope.name == "__iter__" ): return # Verify that the body of the for loop uses a subscript # with the object that was iterated. This uses some heuristics # in order to make sure that the same object is used in the # for body. for child in node.body: for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, expected_subscript_val_type): continue value = subscript.slice if not isinstance(value, nodes.Name): continue if subscript.value.scope() != node.scope(): # Ignore this subscript if it's not in the same # scope. This means that in the body of the for # loop, another scope was created, where the same # name for the iterating object was used. continue if value.name == node.target.name and ( isinstance(subscript.value, nodes.Name) and iterating_object.name == subscript.value.name or isinstance(subscript.value, nodes.Attribute) and iterating_object.attrname == subscript.value.attrname ): self.add_message("consider-using-enumerate", node=node) return def _check_consider_using_dict_items(self, node: nodes.For) -> None: """Add message when accessing dict values by index lookup.""" # Verify that we have a .keys() call and # that the object which is iterated is used as a subscript in the # body of the for. iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return # Verify that the body of the for loop uses a subscript # with the object that was iterated. This uses some heuristics # in order to make sure that the same object is used in the # for body. for child in node.body: for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue last_definition_lineno = value.lookup(value.name)[1][-1].lineno if last_definition_lineno > node.lineno: # Ignore this subscript if it has been redefined after # the for loop. This checks for the line number using .lookup() # to get the line number where the iterating object was last # defined and compare that to the for loop's line number continue if ( isinstance(subscript.parent, nodes.Assign) and subscript in subscript.parent.targets or isinstance(subscript.parent, nodes.AugAssign) and subscript == subscript.parent.target ): # Ignore this subscript if it is the target of an assignment # Early termination as dict index lookup is necessary return self.add_message("consider-using-dict-items", node=node) return @utils.check_messages( "consider-using-dict-items", "use-sequence-for-iteration", ) def visit_comprehension(self, node: nodes.Comprehension) -> None: self._check_consider_using_dict_items_comprehension(node) self._check_use_sequence_for_iteration(node) def _check_consider_using_dict_items_comprehension( self, node: nodes.Comprehension ) -> None: """Add message when accessing dict values by index lookup.""" iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return for child in node.parent.get_children(): for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue self.add_message("consider-using-dict-items", node=node) return def _check_use_sequence_for_iteration( self, node: Union[nodes.For, nodes.Comprehension] ) -> None: """Check if code iterates over an in-place defined set.""" if isinstance(node.iter, nodes.Set): self.add_message("use-sequence-for-iteration", node=node.iter) @utils.check_messages("consider-using-f-string") def visit_const(self, node: nodes.Const) -> None: if self._py36_plus: # f-strings require Python 3.6 if node.pytype() == "builtins.str" and not isinstance( node.parent, nodes.JoinedStr ): self._detect_replacable_format_call(node) def _detect_replacable_format_call(self, node: nodes.Const) -> None: """Check whether a string is used in a call to format() or '%' and whether it can be replaced by an f-string """ if ( isinstance(node.parent, nodes.Attribute) and node.parent.attrname == "format" ): # Don't warn on referencing / assigning .format without calling it if not isinstance(node.parent.parent, nodes.Call): return if node.parent.parent.args: for arg in node.parent.parent.args: # If star expressions with more than 1 element are being used if isinstance(arg, nodes.Starred): inferred = utils.safe_infer(arg.value) if ( isinstance(inferred, astroid.List) and len(inferred.elts) > 1 ): return # Backslashes can't be in f-string expressions if "\\" in arg.as_string(): return elif node.parent.parent.keywords: keyword_args = [ i[0] for i in utils.parse_format_method_string(node.value)[0] ] for keyword in node.parent.parent.keywords: # If keyword is used multiple times if keyword_args.count(keyword.arg) > 1: return keyword = utils.safe_infer(keyword.value) # If lists of more than one element are being unpacked if isinstance(keyword, nodes.Dict): if len(keyword.items) > 1 and len(keyword_args) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, ) elif isinstance(node.parent, nodes.BinOp) and node.parent.op == "%": # Backslashes can't be in f-string expressions if "\\" in node.parent.right.as_string(): return inferred_right = utils.safe_infer(node.parent.right) # If dicts or lists of length > 1 are used if isinstance(inferred_right, nodes.Dict): if len(inferred_right.items) > 1: return elif isinstance(inferred_right, nodes.List): if len(inferred_right.elts) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, )
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
110
safe_infer
ref
function
and isinstance(utils.safe_infer(node.func), astroid.BoundMethod)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
115
get_argument_from_call
ref
function
sep = utils.get_argument_from_call(node, 0, "sep")
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
121
get_argument_from_call
ref
function
utils.get_argument_from_call(node, 1, "maxsplit")
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
128
get_subscript_const_value
ref
function
subscript_value = utils.get_subscript_const_value(node.parent).value
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
135
scope
ref
function
scope = node.scope()
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
136
nodes_of_class
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
137
parent_of
ref
function
if not loop_node.parent_of(node):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
141
nodes_of_class
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
144
nodes_of_class
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
154
as_string
ref
function
node.func.as_string().rsplit(fn_name, maxsplit=1)[0]
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
156
as_string
ref
function
+ f"({sep.as_string()}, maxsplit=1)[{subscript_value}]"
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
158
add_message
ref
function
self.add_message("use-maxsplit-arg", node=node, args=(new_name,))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
160
check_messages
ref
function
@utils.check_messages(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
165
visit_for
def
function
def visit_for(self, node: nodes.For) -> None: self._check_consider_using_enumerate(node) self._check_consider_using_dict_items(node) self._check_use_sequence_for_iteration(node) def _check_consider_using_enumerate(self, node: nodes.For) -> None: """Emit a convention whenever range and len are used for indexing.""" # Verify that we have a `range([start], len(...), [stop])` call and # that the object which is iterated is used as a subscript in the # body of the for. # Is it a proper range call? if not isinstance(node.iter, nodes.Call): return if not self._is_builtin(node.iter.func, "range"): return if not node.iter.args: return is_constant_zero = ( isinstance(node.iter.args[0], nodes.Const) and node.iter.args[0].value == 0 ) if len(node.iter.args) == 2 and not is_constant_zero: return if len(node.iter.args) > 2: return # Is it a proper len call? if not isinstance(node.iter.args[-1], nodes.Call): return second_func = node.iter.args[-1].func if not self._is_builtin(second_func, "len"): return len_args = node.iter.args[-1].args if not len_args or len(len_args) != 1: return iterating_object = len_args[0] if isinstance(iterating_object, nodes.Name): expected_subscript_val_type = nodes.Name elif isinstance(iterating_object, nodes.Attribute): expected_subscript_val_type = nodes.Attribute else: return # If we're defining __iter__ on self, enumerate won't work scope = node.scope() if ( isinstance(iterating_object, nodes.Name) and iterating_object.name == "self" and scope.name == "__iter__" ): return # Verify that the body of the for loop uses a subscript # with the object that was iterated. This uses some heuristics # in order to make sure that the same object is used in the # for body. for child in node.body: for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, expected_subscript_val_type): continue value = subscript.slice if not isinstance(value, nodes.Name): continue if subscript.value.scope() != node.scope(): # Ignore this subscript if it's not in the same # scope. This means that in the body of the for # loop, another scope was created, where the same # name for the iterating object was used. continue if value.name == node.target.name and ( isinstance(subscript.value, nodes.Name) and iterating_object.name == subscript.value.name or isinstance(subscript.value, nodes.Attribute) and iterating_object.attrname == subscript.value.attrname ): self.add_message("consider-using-enumerate", node=node) return def _check_consider_using_dict_items(self, node: nodes.For) -> None: """Add message when accessing dict values by index lookup.""" # Verify that we have a .keys() call and # that the object which is iterated is used as a subscript in the # body of the for. iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return # Verify that the body of the for loop uses a subscript # with the object that was iterated. This uses some heuristics # in order to make sure that the same object is used in the # for body. for child in node.body: for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue last_definition_lineno = value.lookup(value.name)[1][-1].lineno if last_definition_lineno > node.lineno: # Ignore this subscript if it has been redefined after # the for loop. This checks for the line number using .lookup() # to get the line number where the iterating object was last # defined and compare that to the for loop's line number continue if ( isinstance(subscript.parent, nodes.Assign) and subscript in subscript.parent.targets or isinstance(subscript.parent, nodes.AugAssign) and subscript == subscript.parent.target ): # Ignore this subscript if it is the target of an assignment # Early termination as dict index lookup is necessary return self.add_message("consider-using-dict-items", node=node) return @utils.check_messages( "consider-using-dict-items", "use-sequence-for-iteration", ) def visit_comprehension(self, node: nodes.Comprehension) -> None: self._check_consider_using_dict_items_comprehension(node) self._check_use_sequence_for_iteration(node) def _check_consider_using_dict_items_comprehension( self, node: nodes.Comprehension ) -> None: """Add message when accessing dict values by index lookup.""" iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return for child in node.parent.get_children(): for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue self.add_message("consider-using-dict-items", node=node) return def _check_use_sequence_for_iteration( self, node: Union[nodes.For, nodes.Comprehension] ) -> None: """Check if code iterates over an in-place defined set.""" if isinstance(node.iter, nodes.Set): self.add_message("use-sequence-for-iteration", node=node.iter) @utils.check_messages("consider-using-f-string") def visit_const(self, node: nodes.Const) -> None: if self._py36_plus: # f-strings require Python 3.6 if node.pytype() == "builtins.str" and not isinstance( node.parent, nodes.JoinedStr ): self._detect_replacable_format_call(node) def _detect_replacable_format_call(self, node: nodes.Const) -> None: """Check whether a string is used in a call to format() or '%' and whether it can be replaced by an f-string """ if ( isinstance(node.parent, nodes.Attribute) and node.parent.attrname == "format" ): # Don't warn on referencing / assigning .format without calling it if not isinstance(node.parent.parent, nodes.Call): return if node.parent.parent.args: for arg in node.parent.parent.args: # If star expressions with more than 1 element are being used if isinstance(arg, nodes.Starred): inferred = utils.safe_infer(arg.value) if ( isinstance(inferred, astroid.List) and len(inferred.elts) > 1 ): return # Backslashes can't be in f-string expressions if "\\" in arg.as_string(): return elif node.parent.parent.keywords: keyword_args = [ i[0] for i in utils.parse_format_method_string(node.value)[0] ] for keyword in node.parent.parent.keywords: # If keyword is used multiple times if keyword_args.count(keyword.arg) > 1: return keyword = utils.safe_infer(keyword.value) # If lists of more than one element are being unpacked if isinstance(keyword, nodes.Dict): if len(keyword.items) > 1 and len(keyword_args) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, ) elif isinstance(node.parent, nodes.BinOp) and node.parent.op == "%": # Backslashes can't be in f-string expressions if "\\" in node.parent.right.as_string(): return inferred_right = utils.safe_infer(node.parent.right) # If dicts or lists of length > 1 are used if isinstance(inferred_right, nodes.Dict): if len(inferred_right.items) > 1: return elif isinstance(inferred_right, nodes.List): if len(inferred_right.elts) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, )
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
166
_check_consider_using_enumerate
ref
function
self._check_consider_using_enumerate(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
167
_check_consider_using_dict_items
ref
function
self._check_consider_using_dict_items(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
168
_check_use_sequence_for_iteration
ref
function
self._check_use_sequence_for_iteration(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
170
_check_consider_using_enumerate
def
function
def _check_consider_using_enumerate(self, node: nodes.For) -> None: """Emit a convention whenever range and len are used for indexing.""" # Verify that we have a `range([start], len(...), [stop])` call and # that the object which is iterated is used as a subscript in the # body of the for. # Is it a proper range call? if not isinstance(node.iter, nodes.Call): return if not self._is_builtin(node.iter.func, "range"): return if not node.iter.args: return is_constant_zero = ( isinstance(node.iter.args[0], nodes.Const) and node.iter.args[0].value == 0 ) if len(node.iter.args) == 2 and not is_constant_zero: return if len(node.iter.args) > 2: return # Is it a proper len call? if not isinstance(node.iter.args[-1], nodes.Call): return second_func = node.iter.args[-1].func if not self._is_builtin(second_func, "len"): return len_args = node.iter.args[-1].args if not len_args or len(len_args) != 1: return iterating_object = len_args[0] if isinstance(iterating_object, nodes.Name): expected_subscript_val_type = nodes.Name elif isinstance(iterating_object, nodes.Attribute): expected_subscript_val_type = nodes.Attribute else: return # If we're defining __iter__ on self, enumerate won't work scope = node.scope() if ( isinstance(iterating_object, nodes.Name) and iterating_object.name == "self" and scope.name == "__iter__" ): return # Verify that the body of the for loop uses a subscript # with the object that was iterated. This uses some heuristics # in order to make sure that the same object is used in the # for body. for child in node.body: for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, expected_subscript_val_type): continue value = subscript.slice if not isinstance(value, nodes.Name): continue if subscript.value.scope() != node.scope(): # Ignore this subscript if it's not in the same # scope. This means that in the body of the for # loop, another scope was created, where the same # name for the iterating object was used. continue if value.name == node.target.name and ( isinstance(subscript.value, nodes.Name) and iterating_object.name == subscript.value.name or isinstance(subscript.value, nodes.Attribute) and iterating_object.attrname == subscript.value.attrname ): self.add_message("consider-using-enumerate", node=node) return def _check_consider_using_dict_items(self, node: nodes.For) -> None: """Add message when accessing dict values by index lookup.""" # Verify that we have a .keys() call and # that the object which is iterated is used as a subscript in the # body of the for. iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return # Verify that the body of the for loop uses a subscript # with the object that was iterated. This uses some heuristics # in order to make sure that the same object is used in the # for body. for child in node.body: for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue last_definition_lineno = value.lookup(value.name)[1][-1].lineno if last_definition_lineno > node.lineno: # Ignore this subscript if it has been redefined after # the for loop. This checks for the line number using .lookup() # to get the line number where the iterating object was last # defined and compare that to the for loop's line number continue if ( isinstance(subscript.parent, nodes.Assign) and subscript in subscript.parent.targets or isinstance(subscript.parent, nodes.AugAssign) and subscript == subscript.parent.target ): # Ignore this subscript if it is the target of an assignment # Early termination as dict index lookup is necessary return self.add_message("consider-using-dict-items", node=node) return @utils.check_messages( "consider-using-dict-items", "use-sequence-for-iteration", ) def visit_comprehension(self, node: nodes.Comprehension) -> None: self._check_consider_using_dict_items_comprehension(node) self._check_use_sequence_for_iteration(node) def _check_consider_using_dict_items_comprehension( self, node: nodes.Comprehension ) -> None: """Add message when accessing dict values by index lookup.""" iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return for child in node.parent.get_children(): for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue self.add_message("consider-using-dict-items", node=node) return def _check_use_sequence_for_iteration( self, node: Union[nodes.For, nodes.Comprehension] ) -> None: """Check if code iterates over an in-place defined set.""" if isinstance(node.iter, nodes.Set): self.add_message("use-sequence-for-iteration", node=node.iter) @utils.check_messages("consider-using-f-string") def visit_const(self, node: nodes.Const) -> None: if self._py36_plus: # f-strings require Python 3.6 if node.pytype() == "builtins.str" and not isinstance( node.parent, nodes.JoinedStr ): self._detect_replacable_format_call(node) def _detect_replacable_format_call(self, node: nodes.Const) -> None: """Check whether a string is used in a call to format() or '%' and whether it can be replaced by an f-string """ if ( isinstance(node.parent, nodes.Attribute) and node.parent.attrname == "format" ): # Don't warn on referencing / assigning .format without calling it if not isinstance(node.parent.parent, nodes.Call): return if node.parent.parent.args: for arg in node.parent.parent.args: # If star expressions with more than 1 element are being used if isinstance(arg, nodes.Starred): inferred = utils.safe_infer(arg.value) if ( isinstance(inferred, astroid.List) and len(inferred.elts) > 1 ): return # Backslashes can't be in f-string expressions if "\\" in arg.as_string(): return elif node.parent.parent.keywords: keyword_args = [ i[0] for i in utils.parse_format_method_string(node.value)[0] ] for keyword in node.parent.parent.keywords: # If keyword is used multiple times if keyword_args.count(keyword.arg) > 1: return keyword = utils.safe_infer(keyword.value) # If lists of more than one element are being unpacked if isinstance(keyword, nodes.Dict): if len(keyword.items) > 1 and len(keyword_args) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, ) elif isinstance(node.parent, nodes.BinOp) and node.parent.op == "%": # Backslashes can't be in f-string expressions if "\\" in node.parent.right.as_string(): return inferred_right = utils.safe_infer(node.parent.right) # If dicts or lists of length > 1 are used if isinstance(inferred_right, nodes.Dict): if len(inferred_right.items) > 1: return elif isinstance(inferred_right, nodes.List): if len(inferred_right.elts) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, )
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
179
_is_builtin
ref
function
if not self._is_builtin(node.iter.func, "range"):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
195
_is_builtin
ref
function
if not self._is_builtin(second_func, "len"):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
208
scope
ref
function
scope = node.scope()
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
221
nodes_of_class
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
228
scope
ref
function
if subscript.value.scope() != node.scope():
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
228
scope
ref
function
if subscript.value.scope() != node.scope():
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
240
add_message
ref
function
self.add_message("consider-using-enumerate", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
243
_check_consider_using_dict_items
def
function
def _check_consider_using_dict_items(self, node: nodes.For) -> None: """Add message when accessing dict values by index lookup.""" # Verify that we have a .keys() call and # that the object which is iterated is used as a subscript in the # body of the for. iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return # Verify that the body of the for loop uses a subscript # with the object that was iterated. This uses some heuristics # in order to make sure that the same object is used in the # for body. for child in node.body: for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue last_definition_lineno = value.lookup(value.name)[1][-1].lineno if last_definition_lineno > node.lineno: # Ignore this subscript if it has been redefined after # the for loop. This checks for the line number using .lookup() # to get the line number where the iterating object was last # defined and compare that to the for loop's line number continue if ( isinstance(subscript.parent, nodes.Assign) and subscript in subscript.parent.targets or isinstance(subscript.parent, nodes.AugAssign) and subscript == subscript.parent.target ): # Ignore this subscript if it is the target of an assignment # Early termination as dict index lookup is necessary return self.add_message("consider-using-dict-items", node=node) return @utils.check_messages( "consider-using-dict-items", "use-sequence-for-iteration", ) def visit_comprehension(self, node: nodes.Comprehension) -> None: self._check_consider_using_dict_items_comprehension(node) self._check_use_sequence_for_iteration(node) def _check_consider_using_dict_items_comprehension( self, node: nodes.Comprehension ) -> None: """Add message when accessing dict values by index lookup.""" iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return for child in node.parent.get_children(): for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue self.add_message("consider-using-dict-items", node=node) return def _check_use_sequence_for_iteration( self, node: Union[nodes.For, nodes.Comprehension] ) -> None: """Check if code iterates over an in-place defined set.""" if isinstance(node.iter, nodes.Set): self.add_message("use-sequence-for-iteration", node=node.iter) @utils.check_messages("consider-using-f-string") def visit_const(self, node: nodes.Const) -> None: if self._py36_plus: # f-strings require Python 3.6 if node.pytype() == "builtins.str" and not isinstance( node.parent, nodes.JoinedStr ): self._detect_replacable_format_call(node) def _detect_replacable_format_call(self, node: nodes.Const) -> None: """Check whether a string is used in a call to format() or '%' and whether it can be replaced by an f-string """ if ( isinstance(node.parent, nodes.Attribute) and node.parent.attrname == "format" ): # Don't warn on referencing / assigning .format without calling it if not isinstance(node.parent.parent, nodes.Call): return if node.parent.parent.args: for arg in node.parent.parent.args: # If star expressions with more than 1 element are being used if isinstance(arg, nodes.Starred): inferred = utils.safe_infer(arg.value) if ( isinstance(inferred, astroid.List) and len(inferred.elts) > 1 ): return # Backslashes can't be in f-string expressions if "\\" in arg.as_string(): return elif node.parent.parent.keywords: keyword_args = [ i[0] for i in utils.parse_format_method_string(node.value)[0] ] for keyword in node.parent.parent.keywords: # If keyword is used multiple times if keyword_args.count(keyword.arg) > 1: return keyword = utils.safe_infer(keyword.value) # If lists of more than one element are being unpacked if isinstance(keyword, nodes.Dict): if len(keyword.items) > 1 and len(keyword_args) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, ) elif isinstance(node.parent, nodes.BinOp) and node.parent.op == "%": # Backslashes can't be in f-string expressions if "\\" in node.parent.right.as_string(): return inferred_right = utils.safe_infer(node.parent.right) # If dicts or lists of length > 1 are used if isinstance(inferred_right, nodes.Dict): if len(inferred_right.items) > 1: return elif isinstance(inferred_right, nodes.List): if len(inferred_right.elts) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, )
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
249
get_iterating_dictionary_name
ref
function
iterating_object_name = utils.get_iterating_dictionary_name(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
258
nodes_of_class
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
266
as_string
ref
function
or iterating_object_name != subscript.value.as_string()
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
269
lookup
ref
function
last_definition_lineno = value.lookup(value.name)[1][-1].lineno
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
286
add_message
ref
function
self.add_message("consider-using-dict-items", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
289
check_messages
ref
function
@utils.check_messages(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
293
visit_comprehension
def
function
def visit_comprehension(self, node: nodes.Comprehension) -> None: self._check_consider_using_dict_items_comprehension(node) self._check_use_sequence_for_iteration(node) def _check_consider_using_dict_items_comprehension( self, node: nodes.Comprehension ) -> None: """Add message when accessing dict values by index lookup.""" iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return for child in node.parent.get_children(): for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue self.add_message("consider-using-dict-items", node=node) return def _check_use_sequence_for_iteration( self, node: Union[nodes.For, nodes.Comprehension] ) -> None: """Check if code iterates over an in-place defined set.""" if isinstance(node.iter, nodes.Set): self.add_message("use-sequence-for-iteration", node=node.iter) @utils.check_messages("consider-using-f-string") def visit_const(self, node: nodes.Const) -> None: if self._py36_plus: # f-strings require Python 3.6 if node.pytype() == "builtins.str" and not isinstance( node.parent, nodes.JoinedStr ): self._detect_replacable_format_call(node) def _detect_replacable_format_call(self, node: nodes.Const) -> None: """Check whether a string is used in a call to format() or '%' and whether it can be replaced by an f-string """ if ( isinstance(node.parent, nodes.Attribute) and node.parent.attrname == "format" ): # Don't warn on referencing / assigning .format without calling it if not isinstance(node.parent.parent, nodes.Call): return if node.parent.parent.args: for arg in node.parent.parent.args: # If star expressions with more than 1 element are being used if isinstance(arg, nodes.Starred): inferred = utils.safe_infer(arg.value) if ( isinstance(inferred, astroid.List) and len(inferred.elts) > 1 ): return # Backslashes can't be in f-string expressions if "\\" in arg.as_string(): return elif node.parent.parent.keywords: keyword_args = [ i[0] for i in utils.parse_format_method_string(node.value)[0] ] for keyword in node.parent.parent.keywords: # If keyword is used multiple times if keyword_args.count(keyword.arg) > 1: return keyword = utils.safe_infer(keyword.value) # If lists of more than one element are being unpacked if isinstance(keyword, nodes.Dict): if len(keyword.items) > 1 and len(keyword_args) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, ) elif isinstance(node.parent, nodes.BinOp) and node.parent.op == "%": # Backslashes can't be in f-string expressions if "\\" in node.parent.right.as_string(): return inferred_right = utils.safe_infer(node.parent.right) # If dicts or lists of length > 1 are used if isinstance(inferred_right, nodes.Dict): if len(inferred_right.items) > 1: return elif isinstance(inferred_right, nodes.List): if len(inferred_right.elts) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, )
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
294
_check_consider_using_dict_items_comprehension
ref
function
self._check_consider_using_dict_items_comprehension(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
295
_check_use_sequence_for_iteration
ref
function
self._check_use_sequence_for_iteration(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
297
_check_consider_using_dict_items_comprehension
def
function
def _check_consider_using_dict_items_comprehension( self, node: nodes.Comprehension ) -> None: """Add message when accessing dict values by index lookup.""" iterating_object_name = utils.get_iterating_dictionary_name(node) if iterating_object_name is None: return for child in node.parent.get_children(): for subscript in child.nodes_of_class(nodes.Subscript): if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)): continue value = subscript.slice if ( not isinstance(value, nodes.Name) or value.name != node.target.name or iterating_object_name != subscript.value.as_string() ): continue self.add_message("consider-using-dict-items", node=node) return def _check_use_sequence_for_iteration( self, node: Union[nodes.For, nodes.Comprehension] ) -> None: """Check if code iterates over an in-place defined set.""" if isinstance(node.iter, nodes.Set): self.add_message("use-sequence-for-iteration", node=node.iter) @utils.check_messages("consider-using-f-string") def visit_const(self, node: nodes.Const) -> None: if self._py36_plus: # f-strings require Python 3.6 if node.pytype() == "builtins.str" and not isinstance( node.parent, nodes.JoinedStr ): self._detect_replacable_format_call(node) def _detect_replacable_format_call(self, node: nodes.Const) -> None: """Check whether a string is used in a call to format() or '%' and whether it can be replaced by an f-string """ if ( isinstance(node.parent, nodes.Attribute) and node.parent.attrname == "format" ): # Don't warn on referencing / assigning .format without calling it if not isinstance(node.parent.parent, nodes.Call): return if node.parent.parent.args: for arg in node.parent.parent.args: # If star expressions with more than 1 element are being used if isinstance(arg, nodes.Starred): inferred = utils.safe_infer(arg.value) if ( isinstance(inferred, astroid.List) and len(inferred.elts) > 1 ): return # Backslashes can't be in f-string expressions if "\\" in arg.as_string(): return elif node.parent.parent.keywords: keyword_args = [ i[0] for i in utils.parse_format_method_string(node.value)[0] ] for keyword in node.parent.parent.keywords: # If keyword is used multiple times if keyword_args.count(keyword.arg) > 1: return keyword = utils.safe_infer(keyword.value) # If lists of more than one element are being unpacked if isinstance(keyword, nodes.Dict): if len(keyword.items) > 1 and len(keyword_args) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, ) elif isinstance(node.parent, nodes.BinOp) and node.parent.op == "%": # Backslashes can't be in f-string expressions if "\\" in node.parent.right.as_string(): return inferred_right = utils.safe_infer(node.parent.right) # If dicts or lists of length > 1 are used if isinstance(inferred_right, nodes.Dict): if len(inferred_right.items) > 1: return elif isinstance(inferred_right, nodes.List): if len(inferred_right.elts) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, )
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
301
get_iterating_dictionary_name
ref
function
iterating_object_name = utils.get_iterating_dictionary_name(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
305
get_children
ref
function
for child in node.parent.get_children():
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
306
nodes_of_class
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
314
as_string
ref
function
or iterating_object_name != subscript.value.as_string()
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
318
add_message
ref
function
self.add_message("consider-using-dict-items", node=node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
321
_check_use_sequence_for_iteration
def
function
def _check_use_sequence_for_iteration( self, node: Union[nodes.For, nodes.Comprehension] ) -> None: """Check if code iterates over an in-place defined set.""" if isinstance(node.iter, nodes.Set): self.add_message("use-sequence-for-iteration", node=node.iter) @utils.check_messages("consider-using-f-string") def visit_const(self, node: nodes.Const) -> None: if self._py36_plus: # f-strings require Python 3.6 if node.pytype() == "builtins.str" and not isinstance( node.parent, nodes.JoinedStr ): self._detect_replacable_format_call(node) def _detect_replacable_format_call(self, node: nodes.Const) -> None: """Check whether a string is used in a call to format() or '%' and whether it can be replaced by an f-string """ if ( isinstance(node.parent, nodes.Attribute) and node.parent.attrname == "format" ): # Don't warn on referencing / assigning .format without calling it if not isinstance(node.parent.parent, nodes.Call): return if node.parent.parent.args: for arg in node.parent.parent.args: # If star expressions with more than 1 element are being used if isinstance(arg, nodes.Starred): inferred = utils.safe_infer(arg.value) if ( isinstance(inferred, astroid.List) and len(inferred.elts) > 1 ): return # Backslashes can't be in f-string expressions if "\\" in arg.as_string(): return elif node.parent.parent.keywords: keyword_args = [ i[0] for i in utils.parse_format_method_string(node.value)[0] ] for keyword in node.parent.parent.keywords: # If keyword is used multiple times if keyword_args.count(keyword.arg) > 1: return keyword = utils.safe_infer(keyword.value) # If lists of more than one element are being unpacked if isinstance(keyword, nodes.Dict): if len(keyword.items) > 1 and len(keyword_args) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, ) elif isinstance(node.parent, nodes.BinOp) and node.parent.op == "%": # Backslashes can't be in f-string expressions if "\\" in node.parent.right.as_string(): return inferred_right = utils.safe_infer(node.parent.right) # If dicts or lists of length > 1 are used if isinstance(inferred_right, nodes.Dict): if len(inferred_right.items) > 1: return elif isinstance(inferred_right, nodes.List): if len(inferred_right.elts) > 1: return # If all tests pass, then raise message self.add_message( "consider-using-f-string", node=node, line=node.lineno, col_offset=node.col_offset, )
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
326
add_message
ref
function
self.add_message("use-sequence-for-iteration", node=node.iter)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/recommendation_checker.py
pylint/checkers/refactoring/recommendation_checker.py
328
check_messages
ref
function
@utils.check_messages("consider-using-f-string")