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/variables.py
pylint/checkers/variables.py
381
nodes_of_class
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
385
_is_type_checking_import
def
function
def _is_type_checking_import(node: Union[nodes.Import, nodes.ImportFrom]) -> bool: """Check if an import node is guarded by a TYPE_CHECKS guard.""" return any( isinstance(ancestor, nodes.If) and ancestor.test.as_string() in TYPING_TYPE_CHECKS_GUARDS for ancestor in node.node_ancestors() )
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
389
as_string
ref
function
and ancestor.test.as_string() in TYPING_TYPE_CHECKS_GUARDS
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
390
node_ancestors
ref
function
for ancestor in node.node_ancestors()
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
394
_has_locals_call_after_node
def
function
def _has_locals_call_after_node(stmt, scope): skip_nodes = ( nodes.FunctionDef, nodes.ClassDef, nodes.Import, nodes.ImportFrom, ) for call in scope.nodes_of_class(nodes.Call, skip_klass=skip_nodes): inferred = utils.safe_infer(call.func) if ( utils.is_builtin_object(inferred) and getattr(inferred, "name", None) == "locals" ): if stmt.lineno < call.lineno: return _True return _False
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
401
nodes_of_class
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
402
safe_infer
ref
function
inferred = utils.safe_infer(call.func)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
404
is_builtin_object
ref
function
utils.is_builtin_object(inferred)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
548
ScopeConsumer
def
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
557
NamesConsumer
def
class
__init__ __repr__ __iter__ to_consume consumed consumed_uncertain scope_type mark_as_consumed get_next_to_consume _uncertain_nodes_in_except_blocks _defines_name_raises_or_returns _check_loop_finishes_via_except _recursive_search_for_continue_before_break _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
561
ScopeConsumer
ref
function
self._atomic = ScopeConsumer(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
586
to_consume
def
function
def to_consume(self): return self._atomic.to_consume @property def consumed(self): return self._atomic.consumed @property def consumed_uncertain(self) -> DefaultDict[str, List[nodes.NodeNG]]: """Retrieves nodes filtered out by get_next_to_consume() that may not have executed, such as statements in except blocks, or statements in try blocks (when evaluating their corresponding except and finally blocks). Checkers that want to treat the statements as executed (e.g. for unused-variable) may need to add them back. """ return self._atomic.consumed_uncertain @property def scope_type(self): return self._atomic.scope_type def mark_as_consumed(self, name, consumed_nodes): """Mark the given nodes as consumed for the name. If all of the nodes for the name were consumed, delete the name from the to_consume dictionary """ unconsumed = [n for n in self.to_consume[name] if n not in set(consumed_nodes)] self.consumed[name] = consumed_nodes if unconsumed: self.to_consume[name] = unconsumed else: del self.to_consume[name] def get_next_to_consume(self, node: nodes.Name) -> Optional[List[nodes.NodeNG]]: """Return a list of the nodes that define `node` from this scope. If it is uncertain whether a node will be consumed, such as for statements in except blocks, add it to self.consumed_uncertain instead of returning it. Return None to indicate a special case that needs to be handled by the caller. """ name = node.name parent_node = node.parent found_nodes = self.to_consume.get(name) node_statement = node.statement(future=_True) if ( found_nodes and isinstance(parent_node, nodes.Assign) and parent_node == found_nodes[0].parent ): lhs = found_nodes[0].parent.targets[0] if lhs.name == name: # this name is defined in this very statement found_nodes = None if ( found_nodes and isinstance(parent_node, nodes.For) and parent_node.iter == node and parent_node.target in found_nodes ): found_nodes = None # Filter out assignments in ExceptHandlers that node is not contained in # unless this is a test in a filtered comprehension # Example: [e for e in range(3) if e] <--- followed by except e: if found_nodes and ( not isinstance(parent_node, nodes.Comprehension) or node not in parent_node.ifs ): found_nodes = [ n for n in found_nodes if not isinstance(n.statement(future=_True), nodes.ExceptHandler) or n.statement(future=_True).parent_of(node) ] # Filter out assignments in an Except clause that the node is not # contained in, assuming they may fail if found_nodes: uncertain_nodes = self._uncertain_nodes_in_except_blocks( found_nodes, node, node_statement ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] # If this node is in a Finally block of a Try/Finally, # filter out assignments in the try portion, assuming they may fail if found_nodes: uncertain_nodes = ( self._uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes, node_statement ) ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] # If this node is in an ExceptHandler, # filter out assignments in the try portion, assuming they may fail if found_nodes: uncertain_nodes = ( self._uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes, node_statement ) ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] return found_nodes @staticmethod def _uncertain_nodes_in_except_blocks( found_nodes: List[nodes.NodeNG], node: nodes.NodeNG, node_statement: nodes.Statement, ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in an except block. """ uncertain_nodes = [] for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # Only testing for statements in the except block of TryExcept closest_except_handler = utils.get_node_first_ancestor_of_type( other_node_statement, nodes.ExceptHandler ) if not closest_except_handler: continue # If the other node is in the same scope as this node, assume it executes if closest_except_handler.parent_of(node): continue closest_try_except: nodes.TryExcept = closest_except_handler.parent try_block_returns = any( isinstance(try_statement, nodes.Return) for try_statement in closest_try_except.body ) # If the try block returns, assume the except blocks execute. if try_block_returns: # Exception: if this node is in the final block of the other_node_statement, # it will execute before returning. Assume the except statements are uncertain. if ( isinstance(node_statement.parent, nodes.TryFinally) and node_statement in node_statement.parent.finalbody and closest_try_except.parent.parent_of(node_statement) ): uncertain_nodes.append(other_node) # Assume the except blocks execute, so long as each handler # defines the name, raises, or returns. elif all( NamesConsumer._defines_name_raises_or_returns(node.name, handler) for handler in closest_try_except.handlers ): continue if NamesConsumer._check_loop_finishes_via_except(node, closest_try_except): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _defines_name_raises_or_returns( name: str, handler: nodes.ExceptHandler ) -> bool: """Return _True if some child of `handler` defines the name `name`, raises, or returns. """ def _define_raise_or_return(stmt: nodes.NodeNG) -> bool: if isinstance(stmt, (nodes.Raise, nodes.Return)): return _True if isinstance(stmt, nodes.Assign): for target in stmt.targets: for elt in utils.get_all_elements(target): if isinstance(elt, nodes.AssignName) and elt.name == name: return _True if isinstance(stmt, nodes.If): # Check for assignments inside the test if ( isinstance(stmt.test, nodes.NamedExpr) and stmt.test.target.name == name ): return _True if isinstance(stmt.test, nodes.Call): for arg_or_kwarg in stmt.test.args + [ kw.value for kw in stmt.test.keywords ]: if ( isinstance(arg_or_kwarg, nodes.NamedExpr) and arg_or_kwarg.target.name == name ): return _True return _False for stmt in handler.get_children(): if _define_raise_or_return(stmt): return _True if isinstance(stmt, (nodes.If, nodes.With)): if any( _define_raise_or_return(nested_stmt) for nested_stmt in stmt.get_children() ): return _True return _False @staticmethod def _check_loop_finishes_via_except( node: nodes.NodeNG, other_node_try_except: nodes.TryExcept ) -> bool: """Check for a case described in https://github.com/PyCQA/pylint/issues/5683. It consists of a specific control flow scenario where the only non-break exit from a loop consists of the very except handler we are examining, such that code in the `else` branch of the loop can depend on it being assigned. Example: for _ in range(3): try: do_something() except: name = 1 <-- only non-break exit from loop else: break else: print(name) """ if not other_node_try_except.orelse: return _False closest_loop: Optional[ Union[nodes.For, nodes.While] ] = utils.get_node_first_ancestor_of_type(node, (nodes.For, nodes.While)) if closest_loop is None: return _False if not any( else_statement is node or else_statement.parent_of(node) for else_statement in closest_loop.orelse ): # `node` not guarded by `else` return _False for inner_else_statement in other_node_try_except.orelse: if isinstance(inner_else_statement, nodes.Break): break_stmt = inner_else_statement break else: # No break statement return _False def _try_in_loop_body( other_node_try_except: nodes.TryExcept, loop: Union[nodes.For, nodes.While] ) -> bool: """Return _True if `other_node_try_except` is a descendant of `loop`.""" return any( loop_body_statement is other_node_try_except or loop_body_statement.parent_of(other_node_try_except) for loop_body_statement in loop.body ) if not _try_in_loop_body(other_node_try_except, closest_loop): for ancestor in closest_loop.node_ancestors(): if isinstance(ancestor, (nodes.For, nodes.While)): if _try_in_loop_body(other_node_try_except, ancestor): break else: # `other_node_try_except` didn't have a shared ancestor loop return _False for loop_stmt in closest_loop.body: if NamesConsumer._recursive_search_for_continue_before_break( loop_stmt, break_stmt ): break else: # No continue found, so we arrived at our special case! return _True return _False @staticmethod def _recursive_search_for_continue_before_break( stmt: nodes.Statement, break_stmt: nodes.Break ) -> bool: """Return _True if any Continue node can be found in descendants of `stmt` before encountering `break_stmt`, ignoring any nested loops. """ if stmt is break_stmt: return _False if isinstance(stmt, nodes.Continue): return _True for child in stmt.get_children(): if isinstance(stmt, (nodes.For, nodes.While)): continue if NamesConsumer._recursive_search_for_continue_before_break( child, break_stmt ): return _True return _False @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in a try block and the ``node_statement`` being evaluated is in one of its except handlers. """ uncertain_nodes: List[nodes.NodeNG] = [] closest_except_handler = utils.get_node_first_ancestor_of_type( node_statement, nodes.ExceptHandler ) if closest_except_handler is None: return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # If the other statement is the except handler guarding `node`, it executes if other_node_statement is closest_except_handler: continue # Ensure other_node is in a try block ( other_node_try_ancestor, other_node_try_ancestor_visited_child, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryExcept ) if other_node_try_ancestor is None: continue if ( other_node_try_ancestor_visited_child not in other_node_try_ancestor.body ): continue # Make sure nesting is correct -- there should be at least one # except handler that is a sibling attached to the try ancestor, # or is an ancestor of the try ancestor. if not any( closest_except_handler in other_node_try_ancestor.handlers or other_node_try_ancestor_except_handler in closest_except_handler.node_ancestors() for other_node_try_ancestor_except_handler in other_node_try_ancestor.handlers ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: uncertain_nodes: List[nodes.NodeNG] = [] ( closest_try_finally_ancestor, child_of_closest_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( node_statement, nodes.TryFinally ) if closest_try_finally_ancestor is None: return uncertain_nodes if ( child_of_closest_try_finally_ancestor not in closest_try_finally_ancestor.finalbody ): return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) ( other_node_try_finally_ancestor, child_of_other_node_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryFinally ) if other_node_try_finally_ancestor is None: continue # other_node needs to descend from the try of a try/finally. if ( child_of_other_node_try_finally_ancestor not in other_node_try_finally_ancestor.body ): continue # If the two try/finally ancestors are not the same, then # node_statement's closest try/finally ancestor needs to be in # the final body of other_node's try/finally ancestor, or # descend from one of the statements in that final body. if ( other_node_try_finally_ancestor is not closest_try_finally_ancestor and not any( other_node_final_statement is closest_try_finally_ancestor or other_node_final_statement.parent_of( closest_try_finally_ancestor ) for other_node_final_statement in other_node_try_finally_ancestor.finalbody ) ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
590
consumed
def
function
def consumed(self): return self._atomic.consumed @property def consumed_uncertain(self) -> DefaultDict[str, List[nodes.NodeNG]]: """Retrieves nodes filtered out by get_next_to_consume() that may not have executed, such as statements in except blocks, or statements in try blocks (when evaluating their corresponding except and finally blocks). Checkers that want to treat the statements as executed (e.g. for unused-variable) may need to add them back. """ return self._atomic.consumed_uncertain @property def scope_type(self): return self._atomic.scope_type def mark_as_consumed(self, name, consumed_nodes): """Mark the given nodes as consumed for the name. If all of the nodes for the name were consumed, delete the name from the to_consume dictionary """ unconsumed = [n for n in self.to_consume[name] if n not in set(consumed_nodes)] self.consumed[name] = consumed_nodes if unconsumed: self.to_consume[name] = unconsumed else: del self.to_consume[name] def get_next_to_consume(self, node: nodes.Name) -> Optional[List[nodes.NodeNG]]: """Return a list of the nodes that define `node` from this scope. If it is uncertain whether a node will be consumed, such as for statements in except blocks, add it to self.consumed_uncertain instead of returning it. Return None to indicate a special case that needs to be handled by the caller. """ name = node.name parent_node = node.parent found_nodes = self.to_consume.get(name) node_statement = node.statement(future=_True) if ( found_nodes and isinstance(parent_node, nodes.Assign) and parent_node == found_nodes[0].parent ): lhs = found_nodes[0].parent.targets[0] if lhs.name == name: # this name is defined in this very statement found_nodes = None if ( found_nodes and isinstance(parent_node, nodes.For) and parent_node.iter == node and parent_node.target in found_nodes ): found_nodes = None # Filter out assignments in ExceptHandlers that node is not contained in # unless this is a test in a filtered comprehension # Example: [e for e in range(3) if e] <--- followed by except e: if found_nodes and ( not isinstance(parent_node, nodes.Comprehension) or node not in parent_node.ifs ): found_nodes = [ n for n in found_nodes if not isinstance(n.statement(future=_True), nodes.ExceptHandler) or n.statement(future=_True).parent_of(node) ] # Filter out assignments in an Except clause that the node is not # contained in, assuming they may fail if found_nodes: uncertain_nodes = self._uncertain_nodes_in_except_blocks( found_nodes, node, node_statement ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] # If this node is in a Finally block of a Try/Finally, # filter out assignments in the try portion, assuming they may fail if found_nodes: uncertain_nodes = ( self._uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes, node_statement ) ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] # If this node is in an ExceptHandler, # filter out assignments in the try portion, assuming they may fail if found_nodes: uncertain_nodes = ( self._uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes, node_statement ) ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] return found_nodes @staticmethod def _uncertain_nodes_in_except_blocks( found_nodes: List[nodes.NodeNG], node: nodes.NodeNG, node_statement: nodes.Statement, ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in an except block. """ uncertain_nodes = [] for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # Only testing for statements in the except block of TryExcept closest_except_handler = utils.get_node_first_ancestor_of_type( other_node_statement, nodes.ExceptHandler ) if not closest_except_handler: continue # If the other node is in the same scope as this node, assume it executes if closest_except_handler.parent_of(node): continue closest_try_except: nodes.TryExcept = closest_except_handler.parent try_block_returns = any( isinstance(try_statement, nodes.Return) for try_statement in closest_try_except.body ) # If the try block returns, assume the except blocks execute. if try_block_returns: # Exception: if this node is in the final block of the other_node_statement, # it will execute before returning. Assume the except statements are uncertain. if ( isinstance(node_statement.parent, nodes.TryFinally) and node_statement in node_statement.parent.finalbody and closest_try_except.parent.parent_of(node_statement) ): uncertain_nodes.append(other_node) # Assume the except blocks execute, so long as each handler # defines the name, raises, or returns. elif all( NamesConsumer._defines_name_raises_or_returns(node.name, handler) for handler in closest_try_except.handlers ): continue if NamesConsumer._check_loop_finishes_via_except(node, closest_try_except): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _defines_name_raises_or_returns( name: str, handler: nodes.ExceptHandler ) -> bool: """Return _True if some child of `handler` defines the name `name`, raises, or returns. """ def _define_raise_or_return(stmt: nodes.NodeNG) -> bool: if isinstance(stmt, (nodes.Raise, nodes.Return)): return _True if isinstance(stmt, nodes.Assign): for target in stmt.targets: for elt in utils.get_all_elements(target): if isinstance(elt, nodes.AssignName) and elt.name == name: return _True if isinstance(stmt, nodes.If): # Check for assignments inside the test if ( isinstance(stmt.test, nodes.NamedExpr) and stmt.test.target.name == name ): return _True if isinstance(stmt.test, nodes.Call): for arg_or_kwarg in stmt.test.args + [ kw.value for kw in stmt.test.keywords ]: if ( isinstance(arg_or_kwarg, nodes.NamedExpr) and arg_or_kwarg.target.name == name ): return _True return _False for stmt in handler.get_children(): if _define_raise_or_return(stmt): return _True if isinstance(stmt, (nodes.If, nodes.With)): if any( _define_raise_or_return(nested_stmt) for nested_stmt in stmt.get_children() ): return _True return _False @staticmethod def _check_loop_finishes_via_except( node: nodes.NodeNG, other_node_try_except: nodes.TryExcept ) -> bool: """Check for a case described in https://github.com/PyCQA/pylint/issues/5683. It consists of a specific control flow scenario where the only non-break exit from a loop consists of the very except handler we are examining, such that code in the `else` branch of the loop can depend on it being assigned. Example: for _ in range(3): try: do_something() except: name = 1 <-- only non-break exit from loop else: break else: print(name) """ if not other_node_try_except.orelse: return _False closest_loop: Optional[ Union[nodes.For, nodes.While] ] = utils.get_node_first_ancestor_of_type(node, (nodes.For, nodes.While)) if closest_loop is None: return _False if not any( else_statement is node or else_statement.parent_of(node) for else_statement in closest_loop.orelse ): # `node` not guarded by `else` return _False for inner_else_statement in other_node_try_except.orelse: if isinstance(inner_else_statement, nodes.Break): break_stmt = inner_else_statement break else: # No break statement return _False def _try_in_loop_body( other_node_try_except: nodes.TryExcept, loop: Union[nodes.For, nodes.While] ) -> bool: """Return _True if `other_node_try_except` is a descendant of `loop`.""" return any( loop_body_statement is other_node_try_except or loop_body_statement.parent_of(other_node_try_except) for loop_body_statement in loop.body ) if not _try_in_loop_body(other_node_try_except, closest_loop): for ancestor in closest_loop.node_ancestors(): if isinstance(ancestor, (nodes.For, nodes.While)): if _try_in_loop_body(other_node_try_except, ancestor): break else: # `other_node_try_except` didn't have a shared ancestor loop return _False for loop_stmt in closest_loop.body: if NamesConsumer._recursive_search_for_continue_before_break( loop_stmt, break_stmt ): break else: # No continue found, so we arrived at our special case! return _True return _False @staticmethod def _recursive_search_for_continue_before_break( stmt: nodes.Statement, break_stmt: nodes.Break ) -> bool: """Return _True if any Continue node can be found in descendants of `stmt` before encountering `break_stmt`, ignoring any nested loops. """ if stmt is break_stmt: return _False if isinstance(stmt, nodes.Continue): return _True for child in stmt.get_children(): if isinstance(stmt, (nodes.For, nodes.While)): continue if NamesConsumer._recursive_search_for_continue_before_break( child, break_stmt ): return _True return _False @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in a try block and the ``node_statement`` being evaluated is in one of its except handlers. """ uncertain_nodes: List[nodes.NodeNG] = [] closest_except_handler = utils.get_node_first_ancestor_of_type( node_statement, nodes.ExceptHandler ) if closest_except_handler is None: return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # If the other statement is the except handler guarding `node`, it executes if other_node_statement is closest_except_handler: continue # Ensure other_node is in a try block ( other_node_try_ancestor, other_node_try_ancestor_visited_child, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryExcept ) if other_node_try_ancestor is None: continue if ( other_node_try_ancestor_visited_child not in other_node_try_ancestor.body ): continue # Make sure nesting is correct -- there should be at least one # except handler that is a sibling attached to the try ancestor, # or is an ancestor of the try ancestor. if not any( closest_except_handler in other_node_try_ancestor.handlers or other_node_try_ancestor_except_handler in closest_except_handler.node_ancestors() for other_node_try_ancestor_except_handler in other_node_try_ancestor.handlers ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: uncertain_nodes: List[nodes.NodeNG] = [] ( closest_try_finally_ancestor, child_of_closest_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( node_statement, nodes.TryFinally ) if closest_try_finally_ancestor is None: return uncertain_nodes if ( child_of_closest_try_finally_ancestor not in closest_try_finally_ancestor.finalbody ): return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) ( other_node_try_finally_ancestor, child_of_other_node_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryFinally ) if other_node_try_finally_ancestor is None: continue # other_node needs to descend from the try of a try/finally. if ( child_of_other_node_try_finally_ancestor not in other_node_try_finally_ancestor.body ): continue # If the two try/finally ancestors are not the same, then # node_statement's closest try/finally ancestor needs to be in # the final body of other_node's try/finally ancestor, or # descend from one of the statements in that final body. if ( other_node_try_finally_ancestor is not closest_try_finally_ancestor and not any( other_node_final_statement is closest_try_finally_ancestor or other_node_final_statement.parent_of( closest_try_finally_ancestor ) for other_node_final_statement in other_node_try_finally_ancestor.finalbody ) ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
594
consumed_uncertain
def
function
def consumed_uncertain(self) -> DefaultDict[str, List[nodes.NodeNG]]: """Retrieves nodes filtered out by get_next_to_consume() that may not have executed, such as statements in except blocks, or statements in try blocks (when evaluating their corresponding except and finally blocks). Checkers that want to treat the statements as executed (e.g. for unused-variable) may need to add them back. """ return self._atomic.consumed_uncertain @property def scope_type(self): return self._atomic.scope_type def mark_as_consumed(self, name, consumed_nodes): """Mark the given nodes as consumed for the name. If all of the nodes for the name were consumed, delete the name from the to_consume dictionary """ unconsumed = [n for n in self.to_consume[name] if n not in set(consumed_nodes)] self.consumed[name] = consumed_nodes if unconsumed: self.to_consume[name] = unconsumed else: del self.to_consume[name] def get_next_to_consume(self, node: nodes.Name) -> Optional[List[nodes.NodeNG]]: """Return a list of the nodes that define `node` from this scope. If it is uncertain whether a node will be consumed, such as for statements in except blocks, add it to self.consumed_uncertain instead of returning it. Return None to indicate a special case that needs to be handled by the caller. """ name = node.name parent_node = node.parent found_nodes = self.to_consume.get(name) node_statement = node.statement(future=_True) if ( found_nodes and isinstance(parent_node, nodes.Assign) and parent_node == found_nodes[0].parent ): lhs = found_nodes[0].parent.targets[0] if lhs.name == name: # this name is defined in this very statement found_nodes = None if ( found_nodes and isinstance(parent_node, nodes.For) and parent_node.iter == node and parent_node.target in found_nodes ): found_nodes = None # Filter out assignments in ExceptHandlers that node is not contained in # unless this is a test in a filtered comprehension # Example: [e for e in range(3) if e] <--- followed by except e: if found_nodes and ( not isinstance(parent_node, nodes.Comprehension) or node not in parent_node.ifs ): found_nodes = [ n for n in found_nodes if not isinstance(n.statement(future=_True), nodes.ExceptHandler) or n.statement(future=_True).parent_of(node) ] # Filter out assignments in an Except clause that the node is not # contained in, assuming they may fail if found_nodes: uncertain_nodes = self._uncertain_nodes_in_except_blocks( found_nodes, node, node_statement ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] # If this node is in a Finally block of a Try/Finally, # filter out assignments in the try portion, assuming they may fail if found_nodes: uncertain_nodes = ( self._uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes, node_statement ) ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] # If this node is in an ExceptHandler, # filter out assignments in the try portion, assuming they may fail if found_nodes: uncertain_nodes = ( self._uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes, node_statement ) ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] return found_nodes @staticmethod def _uncertain_nodes_in_except_blocks( found_nodes: List[nodes.NodeNG], node: nodes.NodeNG, node_statement: nodes.Statement, ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in an except block. """ uncertain_nodes = [] for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # Only testing for statements in the except block of TryExcept closest_except_handler = utils.get_node_first_ancestor_of_type( other_node_statement, nodes.ExceptHandler ) if not closest_except_handler: continue # If the other node is in the same scope as this node, assume it executes if closest_except_handler.parent_of(node): continue closest_try_except: nodes.TryExcept = closest_except_handler.parent try_block_returns = any( isinstance(try_statement, nodes.Return) for try_statement in closest_try_except.body ) # If the try block returns, assume the except blocks execute. if try_block_returns: # Exception: if this node is in the final block of the other_node_statement, # it will execute before returning. Assume the except statements are uncertain. if ( isinstance(node_statement.parent, nodes.TryFinally) and node_statement in node_statement.parent.finalbody and closest_try_except.parent.parent_of(node_statement) ): uncertain_nodes.append(other_node) # Assume the except blocks execute, so long as each handler # defines the name, raises, or returns. elif all( NamesConsumer._defines_name_raises_or_returns(node.name, handler) for handler in closest_try_except.handlers ): continue if NamesConsumer._check_loop_finishes_via_except(node, closest_try_except): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _defines_name_raises_or_returns( name: str, handler: nodes.ExceptHandler ) -> bool: """Return _True if some child of `handler` defines the name `name`, raises, or returns. """ def _define_raise_or_return(stmt: nodes.NodeNG) -> bool: if isinstance(stmt, (nodes.Raise, nodes.Return)): return _True if isinstance(stmt, nodes.Assign): for target in stmt.targets: for elt in utils.get_all_elements(target): if isinstance(elt, nodes.AssignName) and elt.name == name: return _True if isinstance(stmt, nodes.If): # Check for assignments inside the test if ( isinstance(stmt.test, nodes.NamedExpr) and stmt.test.target.name == name ): return _True if isinstance(stmt.test, nodes.Call): for arg_or_kwarg in stmt.test.args + [ kw.value for kw in stmt.test.keywords ]: if ( isinstance(arg_or_kwarg, nodes.NamedExpr) and arg_or_kwarg.target.name == name ): return _True return _False for stmt in handler.get_children(): if _define_raise_or_return(stmt): return _True if isinstance(stmt, (nodes.If, nodes.With)): if any( _define_raise_or_return(nested_stmt) for nested_stmt in stmt.get_children() ): return _True return _False @staticmethod def _check_loop_finishes_via_except( node: nodes.NodeNG, other_node_try_except: nodes.TryExcept ) -> bool: """Check for a case described in https://github.com/PyCQA/pylint/issues/5683. It consists of a specific control flow scenario where the only non-break exit from a loop consists of the very except handler we are examining, such that code in the `else` branch of the loop can depend on it being assigned. Example: for _ in range(3): try: do_something() except: name = 1 <-- only non-break exit from loop else: break else: print(name) """ if not other_node_try_except.orelse: return _False closest_loop: Optional[ Union[nodes.For, nodes.While] ] = utils.get_node_first_ancestor_of_type(node, (nodes.For, nodes.While)) if closest_loop is None: return _False if not any( else_statement is node or else_statement.parent_of(node) for else_statement in closest_loop.orelse ): # `node` not guarded by `else` return _False for inner_else_statement in other_node_try_except.orelse: if isinstance(inner_else_statement, nodes.Break): break_stmt = inner_else_statement break else: # No break statement return _False def _try_in_loop_body( other_node_try_except: nodes.TryExcept, loop: Union[nodes.For, nodes.While] ) -> bool: """Return _True if `other_node_try_except` is a descendant of `loop`.""" return any( loop_body_statement is other_node_try_except or loop_body_statement.parent_of(other_node_try_except) for loop_body_statement in loop.body ) if not _try_in_loop_body(other_node_try_except, closest_loop): for ancestor in closest_loop.node_ancestors(): if isinstance(ancestor, (nodes.For, nodes.While)): if _try_in_loop_body(other_node_try_except, ancestor): break else: # `other_node_try_except` didn't have a shared ancestor loop return _False for loop_stmt in closest_loop.body: if NamesConsumer._recursive_search_for_continue_before_break( loop_stmt, break_stmt ): break else: # No continue found, so we arrived at our special case! return _True return _False @staticmethod def _recursive_search_for_continue_before_break( stmt: nodes.Statement, break_stmt: nodes.Break ) -> bool: """Return _True if any Continue node can be found in descendants of `stmt` before encountering `break_stmt`, ignoring any nested loops. """ if stmt is break_stmt: return _False if isinstance(stmt, nodes.Continue): return _True for child in stmt.get_children(): if isinstance(stmt, (nodes.For, nodes.While)): continue if NamesConsumer._recursive_search_for_continue_before_break( child, break_stmt ): return _True return _False @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in a try block and the ``node_statement`` being evaluated is in one of its except handlers. """ uncertain_nodes: List[nodes.NodeNG] = [] closest_except_handler = utils.get_node_first_ancestor_of_type( node_statement, nodes.ExceptHandler ) if closest_except_handler is None: return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # If the other statement is the except handler guarding `node`, it executes if other_node_statement is closest_except_handler: continue # Ensure other_node is in a try block ( other_node_try_ancestor, other_node_try_ancestor_visited_child, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryExcept ) if other_node_try_ancestor is None: continue if ( other_node_try_ancestor_visited_child not in other_node_try_ancestor.body ): continue # Make sure nesting is correct -- there should be at least one # except handler that is a sibling attached to the try ancestor, # or is an ancestor of the try ancestor. if not any( closest_except_handler in other_node_try_ancestor.handlers or other_node_try_ancestor_except_handler in closest_except_handler.node_ancestors() for other_node_try_ancestor_except_handler in other_node_try_ancestor.handlers ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: uncertain_nodes: List[nodes.NodeNG] = [] ( closest_try_finally_ancestor, child_of_closest_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( node_statement, nodes.TryFinally ) if closest_try_finally_ancestor is None: return uncertain_nodes if ( child_of_closest_try_finally_ancestor not in closest_try_finally_ancestor.finalbody ): return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) ( other_node_try_finally_ancestor, child_of_other_node_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryFinally ) if other_node_try_finally_ancestor is None: continue # other_node needs to descend from the try of a try/finally. if ( child_of_other_node_try_finally_ancestor not in other_node_try_finally_ancestor.body ): continue # If the two try/finally ancestors are not the same, then # node_statement's closest try/finally ancestor needs to be in # the final body of other_node's try/finally ancestor, or # descend from one of the statements in that final body. if ( other_node_try_finally_ancestor is not closest_try_finally_ancestor and not any( other_node_final_statement is closest_try_finally_ancestor or other_node_final_statement.parent_of( closest_try_finally_ancestor ) for other_node_final_statement in other_node_try_finally_ancestor.finalbody ) ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
604
scope_type
def
function
def scope_type(self): return self._atomic.scope_type def mark_as_consumed(self, name, consumed_nodes): """Mark the given nodes as consumed for the name. If all of the nodes for the name were consumed, delete the name from the to_consume dictionary """ unconsumed = [n for n in self.to_consume[name] if n not in set(consumed_nodes)] self.consumed[name] = consumed_nodes if unconsumed: self.to_consume[name] = unconsumed else: del self.to_consume[name] def get_next_to_consume(self, node: nodes.Name) -> Optional[List[nodes.NodeNG]]: """Return a list of the nodes that define `node` from this scope. If it is uncertain whether a node will be consumed, such as for statements in except blocks, add it to self.consumed_uncertain instead of returning it. Return None to indicate a special case that needs to be handled by the caller. """ name = node.name parent_node = node.parent found_nodes = self.to_consume.get(name) node_statement = node.statement(future=_True) if ( found_nodes and isinstance(parent_node, nodes.Assign) and parent_node == found_nodes[0].parent ): lhs = found_nodes[0].parent.targets[0] if lhs.name == name: # this name is defined in this very statement found_nodes = None if ( found_nodes and isinstance(parent_node, nodes.For) and parent_node.iter == node and parent_node.target in found_nodes ): found_nodes = None # Filter out assignments in ExceptHandlers that node is not contained in # unless this is a test in a filtered comprehension # Example: [e for e in range(3) if e] <--- followed by except e: if found_nodes and ( not isinstance(parent_node, nodes.Comprehension) or node not in parent_node.ifs ): found_nodes = [ n for n in found_nodes if not isinstance(n.statement(future=_True), nodes.ExceptHandler) or n.statement(future=_True).parent_of(node) ] # Filter out assignments in an Except clause that the node is not # contained in, assuming they may fail if found_nodes: uncertain_nodes = self._uncertain_nodes_in_except_blocks( found_nodes, node, node_statement ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] # If this node is in a Finally block of a Try/Finally, # filter out assignments in the try portion, assuming they may fail if found_nodes: uncertain_nodes = ( self._uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes, node_statement ) ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] # If this node is in an ExceptHandler, # filter out assignments in the try portion, assuming they may fail if found_nodes: uncertain_nodes = ( self._uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes, node_statement ) ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] return found_nodes @staticmethod def _uncertain_nodes_in_except_blocks( found_nodes: List[nodes.NodeNG], node: nodes.NodeNG, node_statement: nodes.Statement, ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in an except block. """ uncertain_nodes = [] for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # Only testing for statements in the except block of TryExcept closest_except_handler = utils.get_node_first_ancestor_of_type( other_node_statement, nodes.ExceptHandler ) if not closest_except_handler: continue # If the other node is in the same scope as this node, assume it executes if closest_except_handler.parent_of(node): continue closest_try_except: nodes.TryExcept = closest_except_handler.parent try_block_returns = any( isinstance(try_statement, nodes.Return) for try_statement in closest_try_except.body ) # If the try block returns, assume the except blocks execute. if try_block_returns: # Exception: if this node is in the final block of the other_node_statement, # it will execute before returning. Assume the except statements are uncertain. if ( isinstance(node_statement.parent, nodes.TryFinally) and node_statement in node_statement.parent.finalbody and closest_try_except.parent.parent_of(node_statement) ): uncertain_nodes.append(other_node) # Assume the except blocks execute, so long as each handler # defines the name, raises, or returns. elif all( NamesConsumer._defines_name_raises_or_returns(node.name, handler) for handler in closest_try_except.handlers ): continue if NamesConsumer._check_loop_finishes_via_except(node, closest_try_except): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _defines_name_raises_or_returns( name: str, handler: nodes.ExceptHandler ) -> bool: """Return _True if some child of `handler` defines the name `name`, raises, or returns. """ def _define_raise_or_return(stmt: nodes.NodeNG) -> bool: if isinstance(stmt, (nodes.Raise, nodes.Return)): return _True if isinstance(stmt, nodes.Assign): for target in stmt.targets: for elt in utils.get_all_elements(target): if isinstance(elt, nodes.AssignName) and elt.name == name: return _True if isinstance(stmt, nodes.If): # Check for assignments inside the test if ( isinstance(stmt.test, nodes.NamedExpr) and stmt.test.target.name == name ): return _True if isinstance(stmt.test, nodes.Call): for arg_or_kwarg in stmt.test.args + [ kw.value for kw in stmt.test.keywords ]: if ( isinstance(arg_or_kwarg, nodes.NamedExpr) and arg_or_kwarg.target.name == name ): return _True return _False for stmt in handler.get_children(): if _define_raise_or_return(stmt): return _True if isinstance(stmt, (nodes.If, nodes.With)): if any( _define_raise_or_return(nested_stmt) for nested_stmt in stmt.get_children() ): return _True return _False @staticmethod def _check_loop_finishes_via_except( node: nodes.NodeNG, other_node_try_except: nodes.TryExcept ) -> bool: """Check for a case described in https://github.com/PyCQA/pylint/issues/5683. It consists of a specific control flow scenario where the only non-break exit from a loop consists of the very except handler we are examining, such that code in the `else` branch of the loop can depend on it being assigned. Example: for _ in range(3): try: do_something() except: name = 1 <-- only non-break exit from loop else: break else: print(name) """ if not other_node_try_except.orelse: return _False closest_loop: Optional[ Union[nodes.For, nodes.While] ] = utils.get_node_first_ancestor_of_type(node, (nodes.For, nodes.While)) if closest_loop is None: return _False if not any( else_statement is node or else_statement.parent_of(node) for else_statement in closest_loop.orelse ): # `node` not guarded by `else` return _False for inner_else_statement in other_node_try_except.orelse: if isinstance(inner_else_statement, nodes.Break): break_stmt = inner_else_statement break else: # No break statement return _False def _try_in_loop_body( other_node_try_except: nodes.TryExcept, loop: Union[nodes.For, nodes.While] ) -> bool: """Return _True if `other_node_try_except` is a descendant of `loop`.""" return any( loop_body_statement is other_node_try_except or loop_body_statement.parent_of(other_node_try_except) for loop_body_statement in loop.body ) if not _try_in_loop_body(other_node_try_except, closest_loop): for ancestor in closest_loop.node_ancestors(): if isinstance(ancestor, (nodes.For, nodes.While)): if _try_in_loop_body(other_node_try_except, ancestor): break else: # `other_node_try_except` didn't have a shared ancestor loop return _False for loop_stmt in closest_loop.body: if NamesConsumer._recursive_search_for_continue_before_break( loop_stmt, break_stmt ): break else: # No continue found, so we arrived at our special case! return _True return _False @staticmethod def _recursive_search_for_continue_before_break( stmt: nodes.Statement, break_stmt: nodes.Break ) -> bool: """Return _True if any Continue node can be found in descendants of `stmt` before encountering `break_stmt`, ignoring any nested loops. """ if stmt is break_stmt: return _False if isinstance(stmt, nodes.Continue): return _True for child in stmt.get_children(): if isinstance(stmt, (nodes.For, nodes.While)): continue if NamesConsumer._recursive_search_for_continue_before_break( child, break_stmt ): return _True return _False @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in a try block and the ``node_statement`` being evaluated is in one of its except handlers. """ uncertain_nodes: List[nodes.NodeNG] = [] closest_except_handler = utils.get_node_first_ancestor_of_type( node_statement, nodes.ExceptHandler ) if closest_except_handler is None: return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # If the other statement is the except handler guarding `node`, it executes if other_node_statement is closest_except_handler: continue # Ensure other_node is in a try block ( other_node_try_ancestor, other_node_try_ancestor_visited_child, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryExcept ) if other_node_try_ancestor is None: continue if ( other_node_try_ancestor_visited_child not in other_node_try_ancestor.body ): continue # Make sure nesting is correct -- there should be at least one # except handler that is a sibling attached to the try ancestor, # or is an ancestor of the try ancestor. if not any( closest_except_handler in other_node_try_ancestor.handlers or other_node_try_ancestor_except_handler in closest_except_handler.node_ancestors() for other_node_try_ancestor_except_handler in other_node_try_ancestor.handlers ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: uncertain_nodes: List[nodes.NodeNG] = [] ( closest_try_finally_ancestor, child_of_closest_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( node_statement, nodes.TryFinally ) if closest_try_finally_ancestor is None: return uncertain_nodes if ( child_of_closest_try_finally_ancestor not in closest_try_finally_ancestor.finalbody ): return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) ( other_node_try_finally_ancestor, child_of_other_node_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryFinally ) if other_node_try_finally_ancestor is None: continue # other_node needs to descend from the try of a try/finally. if ( child_of_other_node_try_finally_ancestor not in other_node_try_finally_ancestor.body ): continue # If the two try/finally ancestors are not the same, then # node_statement's closest try/finally ancestor needs to be in # the final body of other_node's try/finally ancestor, or # descend from one of the statements in that final body. if ( other_node_try_finally_ancestor is not closest_try_finally_ancestor and not any( other_node_final_statement is closest_try_finally_ancestor or other_node_final_statement.parent_of( closest_try_finally_ancestor ) for other_node_final_statement in other_node_try_finally_ancestor.finalbody ) ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
607
mark_as_consumed
def
function
def mark_as_consumed(self, name, consumed_nodes): """Mark the given nodes as consumed for the name. If all of the nodes for the name were consumed, delete the name from the to_consume dictionary """ unconsumed = [n for n in self.to_consume[name] if n not in set(consumed_nodes)] self.consumed[name] = consumed_nodes if unconsumed: self.to_consume[name] = unconsumed else: del self.to_consume[name] def get_next_to_consume(self, node: nodes.Name) -> Optional[List[nodes.NodeNG]]: """Return a list of the nodes that define `node` from this scope. If it is uncertain whether a node will be consumed, such as for statements in except blocks, add it to self.consumed_uncertain instead of returning it. Return None to indicate a special case that needs to be handled by the caller. """ name = node.name parent_node = node.parent found_nodes = self.to_consume.get(name) node_statement = node.statement(future=_True) if ( found_nodes and isinstance(parent_node, nodes.Assign) and parent_node == found_nodes[0].parent ): lhs = found_nodes[0].parent.targets[0] if lhs.name == name: # this name is defined in this very statement found_nodes = None if ( found_nodes and isinstance(parent_node, nodes.For) and parent_node.iter == node and parent_node.target in found_nodes ): found_nodes = None # Filter out assignments in ExceptHandlers that node is not contained in # unless this is a test in a filtered comprehension # Example: [e for e in range(3) if e] <--- followed by except e: if found_nodes and ( not isinstance(parent_node, nodes.Comprehension) or node not in parent_node.ifs ): found_nodes = [ n for n in found_nodes if not isinstance(n.statement(future=_True), nodes.ExceptHandler) or n.statement(future=_True).parent_of(node) ] # Filter out assignments in an Except clause that the node is not # contained in, assuming they may fail if found_nodes: uncertain_nodes = self._uncertain_nodes_in_except_blocks( found_nodes, node, node_statement ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] # If this node is in a Finally block of a Try/Finally, # filter out assignments in the try portion, assuming they may fail if found_nodes: uncertain_nodes = ( self._uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes, node_statement ) ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] # If this node is in an ExceptHandler, # filter out assignments in the try portion, assuming they may fail if found_nodes: uncertain_nodes = ( self._uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes, node_statement ) ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] return found_nodes @staticmethod def _uncertain_nodes_in_except_blocks( found_nodes: List[nodes.NodeNG], node: nodes.NodeNG, node_statement: nodes.Statement, ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in an except block. """ uncertain_nodes = [] for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # Only testing for statements in the except block of TryExcept closest_except_handler = utils.get_node_first_ancestor_of_type( other_node_statement, nodes.ExceptHandler ) if not closest_except_handler: continue # If the other node is in the same scope as this node, assume it executes if closest_except_handler.parent_of(node): continue closest_try_except: nodes.TryExcept = closest_except_handler.parent try_block_returns = any( isinstance(try_statement, nodes.Return) for try_statement in closest_try_except.body ) # If the try block returns, assume the except blocks execute. if try_block_returns: # Exception: if this node is in the final block of the other_node_statement, # it will execute before returning. Assume the except statements are uncertain. if ( isinstance(node_statement.parent, nodes.TryFinally) and node_statement in node_statement.parent.finalbody and closest_try_except.parent.parent_of(node_statement) ): uncertain_nodes.append(other_node) # Assume the except blocks execute, so long as each handler # defines the name, raises, or returns. elif all( NamesConsumer._defines_name_raises_or_returns(node.name, handler) for handler in closest_try_except.handlers ): continue if NamesConsumer._check_loop_finishes_via_except(node, closest_try_except): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _defines_name_raises_or_returns( name: str, handler: nodes.ExceptHandler ) -> bool: """Return _True if some child of `handler` defines the name `name`, raises, or returns. """ def _define_raise_or_return(stmt: nodes.NodeNG) -> bool: if isinstance(stmt, (nodes.Raise, nodes.Return)): return _True if isinstance(stmt, nodes.Assign): for target in stmt.targets: for elt in utils.get_all_elements(target): if isinstance(elt, nodes.AssignName) and elt.name == name: return _True if isinstance(stmt, nodes.If): # Check for assignments inside the test if ( isinstance(stmt.test, nodes.NamedExpr) and stmt.test.target.name == name ): return _True if isinstance(stmt.test, nodes.Call): for arg_or_kwarg in stmt.test.args + [ kw.value for kw in stmt.test.keywords ]: if ( isinstance(arg_or_kwarg, nodes.NamedExpr) and arg_or_kwarg.target.name == name ): return _True return _False for stmt in handler.get_children(): if _define_raise_or_return(stmt): return _True if isinstance(stmt, (nodes.If, nodes.With)): if any( _define_raise_or_return(nested_stmt) for nested_stmt in stmt.get_children() ): return _True return _False @staticmethod def _check_loop_finishes_via_except( node: nodes.NodeNG, other_node_try_except: nodes.TryExcept ) -> bool: """Check for a case described in https://github.com/PyCQA/pylint/issues/5683. It consists of a specific control flow scenario where the only non-break exit from a loop consists of the very except handler we are examining, such that code in the `else` branch of the loop can depend on it being assigned. Example: for _ in range(3): try: do_something() except: name = 1 <-- only non-break exit from loop else: break else: print(name) """ if not other_node_try_except.orelse: return _False closest_loop: Optional[ Union[nodes.For, nodes.While] ] = utils.get_node_first_ancestor_of_type(node, (nodes.For, nodes.While)) if closest_loop is None: return _False if not any( else_statement is node or else_statement.parent_of(node) for else_statement in closest_loop.orelse ): # `node` not guarded by `else` return _False for inner_else_statement in other_node_try_except.orelse: if isinstance(inner_else_statement, nodes.Break): break_stmt = inner_else_statement break else: # No break statement return _False def _try_in_loop_body( other_node_try_except: nodes.TryExcept, loop: Union[nodes.For, nodes.While] ) -> bool: """Return _True if `other_node_try_except` is a descendant of `loop`.""" return any( loop_body_statement is other_node_try_except or loop_body_statement.parent_of(other_node_try_except) for loop_body_statement in loop.body ) if not _try_in_loop_body(other_node_try_except, closest_loop): for ancestor in closest_loop.node_ancestors(): if isinstance(ancestor, (nodes.For, nodes.While)): if _try_in_loop_body(other_node_try_except, ancestor): break else: # `other_node_try_except` didn't have a shared ancestor loop return _False for loop_stmt in closest_loop.body: if NamesConsumer._recursive_search_for_continue_before_break( loop_stmt, break_stmt ): break else: # No continue found, so we arrived at our special case! return _True return _False @staticmethod def _recursive_search_for_continue_before_break( stmt: nodes.Statement, break_stmt: nodes.Break ) -> bool: """Return _True if any Continue node can be found in descendants of `stmt` before encountering `break_stmt`, ignoring any nested loops. """ if stmt is break_stmt: return _False if isinstance(stmt, nodes.Continue): return _True for child in stmt.get_children(): if isinstance(stmt, (nodes.For, nodes.While)): continue if NamesConsumer._recursive_search_for_continue_before_break( child, break_stmt ): return _True return _False @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in a try block and the ``node_statement`` being evaluated is in one of its except handlers. """ uncertain_nodes: List[nodes.NodeNG] = [] closest_except_handler = utils.get_node_first_ancestor_of_type( node_statement, nodes.ExceptHandler ) if closest_except_handler is None: return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # If the other statement is the except handler guarding `node`, it executes if other_node_statement is closest_except_handler: continue # Ensure other_node is in a try block ( other_node_try_ancestor, other_node_try_ancestor_visited_child, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryExcept ) if other_node_try_ancestor is None: continue if ( other_node_try_ancestor_visited_child not in other_node_try_ancestor.body ): continue # Make sure nesting is correct -- there should be at least one # except handler that is a sibling attached to the try ancestor, # or is an ancestor of the try ancestor. if not any( closest_except_handler in other_node_try_ancestor.handlers or other_node_try_ancestor_except_handler in closest_except_handler.node_ancestors() for other_node_try_ancestor_except_handler in other_node_try_ancestor.handlers ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: uncertain_nodes: List[nodes.NodeNG] = [] ( closest_try_finally_ancestor, child_of_closest_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( node_statement, nodes.TryFinally ) if closest_try_finally_ancestor is None: return uncertain_nodes if ( child_of_closest_try_finally_ancestor not in closest_try_finally_ancestor.finalbody ): return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) ( other_node_try_finally_ancestor, child_of_other_node_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryFinally ) if other_node_try_finally_ancestor is None: continue # other_node needs to descend from the try of a try/finally. if ( child_of_other_node_try_finally_ancestor not in other_node_try_finally_ancestor.body ): continue # If the two try/finally ancestors are not the same, then # node_statement's closest try/finally ancestor needs to be in # the final body of other_node's try/finally ancestor, or # descend from one of the statements in that final body. if ( other_node_try_finally_ancestor is not closest_try_finally_ancestor and not any( other_node_final_statement is closest_try_finally_ancestor or other_node_final_statement.parent_of( closest_try_finally_ancestor ) for other_node_final_statement in other_node_try_finally_ancestor.finalbody ) ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
620
get_next_to_consume
def
function
def get_next_to_consume(self, node: nodes.Name) -> Optional[List[nodes.NodeNG]]: """Return a list of the nodes that define `node` from this scope. If it is uncertain whether a node will be consumed, such as for statements in except blocks, add it to self.consumed_uncertain instead of returning it. Return None to indicate a special case that needs to be handled by the caller. """ name = node.name parent_node = node.parent found_nodes = self.to_consume.get(name) node_statement = node.statement(future=_True) if ( found_nodes and isinstance(parent_node, nodes.Assign) and parent_node == found_nodes[0].parent ): lhs = found_nodes[0].parent.targets[0] if lhs.name == name: # this name is defined in this very statement found_nodes = None if ( found_nodes and isinstance(parent_node, nodes.For) and parent_node.iter == node and parent_node.target in found_nodes ): found_nodes = None # Filter out assignments in ExceptHandlers that node is not contained in # unless this is a test in a filtered comprehension # Example: [e for e in range(3) if e] <--- followed by except e: if found_nodes and ( not isinstance(parent_node, nodes.Comprehension) or node not in parent_node.ifs ): found_nodes = [ n for n in found_nodes if not isinstance(n.statement(future=_True), nodes.ExceptHandler) or n.statement(future=_True).parent_of(node) ] # Filter out assignments in an Except clause that the node is not # contained in, assuming they may fail if found_nodes: uncertain_nodes = self._uncertain_nodes_in_except_blocks( found_nodes, node, node_statement ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] # If this node is in a Finally block of a Try/Finally, # filter out assignments in the try portion, assuming they may fail if found_nodes: uncertain_nodes = ( self._uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes, node_statement ) ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] # If this node is in an ExceptHandler, # filter out assignments in the try portion, assuming they may fail if found_nodes: uncertain_nodes = ( self._uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes, node_statement ) ) self.consumed_uncertain[node.name] += uncertain_nodes uncertain_nodes_set = set(uncertain_nodes) found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set] return found_nodes @staticmethod def _uncertain_nodes_in_except_blocks( found_nodes: List[nodes.NodeNG], node: nodes.NodeNG, node_statement: nodes.Statement, ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in an except block. """ uncertain_nodes = [] for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # Only testing for statements in the except block of TryExcept closest_except_handler = utils.get_node_first_ancestor_of_type( other_node_statement, nodes.ExceptHandler ) if not closest_except_handler: continue # If the other node is in the same scope as this node, assume it executes if closest_except_handler.parent_of(node): continue closest_try_except: nodes.TryExcept = closest_except_handler.parent try_block_returns = any( isinstance(try_statement, nodes.Return) for try_statement in closest_try_except.body ) # If the try block returns, assume the except blocks execute. if try_block_returns: # Exception: if this node is in the final block of the other_node_statement, # it will execute before returning. Assume the except statements are uncertain. if ( isinstance(node_statement.parent, nodes.TryFinally) and node_statement in node_statement.parent.finalbody and closest_try_except.parent.parent_of(node_statement) ): uncertain_nodes.append(other_node) # Assume the except blocks execute, so long as each handler # defines the name, raises, or returns. elif all( NamesConsumer._defines_name_raises_or_returns(node.name, handler) for handler in closest_try_except.handlers ): continue if NamesConsumer._check_loop_finishes_via_except(node, closest_try_except): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _defines_name_raises_or_returns( name: str, handler: nodes.ExceptHandler ) -> bool: """Return _True if some child of `handler` defines the name `name`, raises, or returns. """ def _define_raise_or_return(stmt: nodes.NodeNG) -> bool: if isinstance(stmt, (nodes.Raise, nodes.Return)): return _True if isinstance(stmt, nodes.Assign): for target in stmt.targets: for elt in utils.get_all_elements(target): if isinstance(elt, nodes.AssignName) and elt.name == name: return _True if isinstance(stmt, nodes.If): # Check for assignments inside the test if ( isinstance(stmt.test, nodes.NamedExpr) and stmt.test.target.name == name ): return _True if isinstance(stmt.test, nodes.Call): for arg_or_kwarg in stmt.test.args + [ kw.value for kw in stmt.test.keywords ]: if ( isinstance(arg_or_kwarg, nodes.NamedExpr) and arg_or_kwarg.target.name == name ): return _True return _False for stmt in handler.get_children(): if _define_raise_or_return(stmt): return _True if isinstance(stmt, (nodes.If, nodes.With)): if any( _define_raise_or_return(nested_stmt) for nested_stmt in stmt.get_children() ): return _True return _False @staticmethod def _check_loop_finishes_via_except( node: nodes.NodeNG, other_node_try_except: nodes.TryExcept ) -> bool: """Check for a case described in https://github.com/PyCQA/pylint/issues/5683. It consists of a specific control flow scenario where the only non-break exit from a loop consists of the very except handler we are examining, such that code in the `else` branch of the loop can depend on it being assigned. Example: for _ in range(3): try: do_something() except: name = 1 <-- only non-break exit from loop else: break else: print(name) """ if not other_node_try_except.orelse: return _False closest_loop: Optional[ Union[nodes.For, nodes.While] ] = utils.get_node_first_ancestor_of_type(node, (nodes.For, nodes.While)) if closest_loop is None: return _False if not any( else_statement is node or else_statement.parent_of(node) for else_statement in closest_loop.orelse ): # `node` not guarded by `else` return _False for inner_else_statement in other_node_try_except.orelse: if isinstance(inner_else_statement, nodes.Break): break_stmt = inner_else_statement break else: # No break statement return _False def _try_in_loop_body( other_node_try_except: nodes.TryExcept, loop: Union[nodes.For, nodes.While] ) -> bool: """Return _True if `other_node_try_except` is a descendant of `loop`.""" return any( loop_body_statement is other_node_try_except or loop_body_statement.parent_of(other_node_try_except) for loop_body_statement in loop.body ) if not _try_in_loop_body(other_node_try_except, closest_loop): for ancestor in closest_loop.node_ancestors(): if isinstance(ancestor, (nodes.For, nodes.While)): if _try_in_loop_body(other_node_try_except, ancestor): break else: # `other_node_try_except` didn't have a shared ancestor loop return _False for loop_stmt in closest_loop.body: if NamesConsumer._recursive_search_for_continue_before_break( loop_stmt, break_stmt ): break else: # No continue found, so we arrived at our special case! return _True return _False @staticmethod def _recursive_search_for_continue_before_break( stmt: nodes.Statement, break_stmt: nodes.Break ) -> bool: """Return _True if any Continue node can be found in descendants of `stmt` before encountering `break_stmt`, ignoring any nested loops. """ if stmt is break_stmt: return _False if isinstance(stmt, nodes.Continue): return _True for child in stmt.get_children(): if isinstance(stmt, (nodes.For, nodes.While)): continue if NamesConsumer._recursive_search_for_continue_before_break( child, break_stmt ): return _True return _False @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in a try block and the ``node_statement`` being evaluated is in one of its except handlers. """ uncertain_nodes: List[nodes.NodeNG] = [] closest_except_handler = utils.get_node_first_ancestor_of_type( node_statement, nodes.ExceptHandler ) if closest_except_handler is None: return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # If the other statement is the except handler guarding `node`, it executes if other_node_statement is closest_except_handler: continue # Ensure other_node is in a try block ( other_node_try_ancestor, other_node_try_ancestor_visited_child, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryExcept ) if other_node_try_ancestor is None: continue if ( other_node_try_ancestor_visited_child not in other_node_try_ancestor.body ): continue # Make sure nesting is correct -- there should be at least one # except handler that is a sibling attached to the try ancestor, # or is an ancestor of the try ancestor. if not any( closest_except_handler in other_node_try_ancestor.handlers or other_node_try_ancestor_except_handler in closest_except_handler.node_ancestors() for other_node_try_ancestor_except_handler in other_node_try_ancestor.handlers ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: uncertain_nodes: List[nodes.NodeNG] = [] ( closest_try_finally_ancestor, child_of_closest_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( node_statement, nodes.TryFinally ) if closest_try_finally_ancestor is None: return uncertain_nodes if ( child_of_closest_try_finally_ancestor not in closest_try_finally_ancestor.finalbody ): return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) ( other_node_try_finally_ancestor, child_of_other_node_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryFinally ) if other_node_try_finally_ancestor is None: continue # other_node needs to descend from the try of a try/finally. if ( child_of_other_node_try_finally_ancestor not in other_node_try_finally_ancestor.body ): continue # If the two try/finally ancestors are not the same, then # node_statement's closest try/finally ancestor needs to be in # the final body of other_node's try/finally ancestor, or # descend from one of the statements in that final body. if ( other_node_try_finally_ancestor is not closest_try_finally_ancestor and not any( other_node_final_statement is closest_try_finally_ancestor or other_node_final_statement.parent_of( closest_try_finally_ancestor ) for other_node_final_statement in other_node_try_finally_ancestor.finalbody ) ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
629
statement
ref
function
node_statement = node.statement(future=True)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
657
statement
ref
function
if not isinstance(n.statement(future=True), nodes.ExceptHandler)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
658
statement
ref
function
or n.statement(future=True).parent_of(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
658
parent_of
ref
function
or n.statement(future=True).parent_of(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
664
_uncertain_nodes_in_except_blocks
ref
function
uncertain_nodes = self._uncertain_nodes_in_except_blocks(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
675
_uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks
ref
function
self._uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
687
_uncertain_nodes_in_try_blocks_when_evaluating_except_blocks
ref
function
self._uncertain_nodes_in_try_blocks_when_evaluating_except_blocks(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
698
_uncertain_nodes_in_except_blocks
def
function
def _uncertain_nodes_in_except_blocks( found_nodes: List[nodes.NodeNG], node: nodes.NodeNG, node_statement: nodes.Statement, ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in an except block. """ uncertain_nodes = [] for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # Only testing for statements in the except block of TryExcept closest_except_handler = utils.get_node_first_ancestor_of_type( other_node_statement, nodes.ExceptHandler ) if not closest_except_handler: continue # If the other node is in the same scope as this node, assume it executes if closest_except_handler.parent_of(node): continue closest_try_except: nodes.TryExcept = closest_except_handler.parent try_block_returns = any( isinstance(try_statement, nodes.Return) for try_statement in closest_try_except.body ) # If the try block returns, assume the except blocks execute. if try_block_returns: # Exception: if this node is in the final block of the other_node_statement, # it will execute before returning. Assume the except statements are uncertain. if ( isinstance(node_statement.parent, nodes.TryFinally) and node_statement in node_statement.parent.finalbody and closest_try_except.parent.parent_of(node_statement) ): uncertain_nodes.append(other_node) # Assume the except blocks execute, so long as each handler # defines the name, raises, or returns. elif all( NamesConsumer._defines_name_raises_or_returns(node.name, handler) for handler in closest_try_except.handlers ): continue if NamesConsumer._check_loop_finishes_via_except(node, closest_try_except): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _defines_name_raises_or_returns( name: str, handler: nodes.ExceptHandler ) -> bool: """Return _True if some child of `handler` defines the name `name`, raises, or returns. """ def _define_raise_or_return(stmt: nodes.NodeNG) -> bool: if isinstance(stmt, (nodes.Raise, nodes.Return)): return _True if isinstance(stmt, nodes.Assign): for target in stmt.targets: for elt in utils.get_all_elements(target): if isinstance(elt, nodes.AssignName) and elt.name == name: return _True if isinstance(stmt, nodes.If): # Check for assignments inside the test if ( isinstance(stmt.test, nodes.NamedExpr) and stmt.test.target.name == name ): return _True if isinstance(stmt.test, nodes.Call): for arg_or_kwarg in stmt.test.args + [ kw.value for kw in stmt.test.keywords ]: if ( isinstance(arg_or_kwarg, nodes.NamedExpr) and arg_or_kwarg.target.name == name ): return _True return _False for stmt in handler.get_children(): if _define_raise_or_return(stmt): return _True if isinstance(stmt, (nodes.If, nodes.With)): if any( _define_raise_or_return(nested_stmt) for nested_stmt in stmt.get_children() ): return _True return _False @staticmethod def _check_loop_finishes_via_except( node: nodes.NodeNG, other_node_try_except: nodes.TryExcept ) -> bool: """Check for a case described in https://github.com/PyCQA/pylint/issues/5683. It consists of a specific control flow scenario where the only non-break exit from a loop consists of the very except handler we are examining, such that code in the `else` branch of the loop can depend on it being assigned. Example: for _ in range(3): try: do_something() except: name = 1 <-- only non-break exit from loop else: break else: print(name) """ if not other_node_try_except.orelse: return _False closest_loop: Optional[ Union[nodes.For, nodes.While] ] = utils.get_node_first_ancestor_of_type(node, (nodes.For, nodes.While)) if closest_loop is None: return _False if not any( else_statement is node or else_statement.parent_of(node) for else_statement in closest_loop.orelse ): # `node` not guarded by `else` return _False for inner_else_statement in other_node_try_except.orelse: if isinstance(inner_else_statement, nodes.Break): break_stmt = inner_else_statement break else: # No break statement return _False def _try_in_loop_body( other_node_try_except: nodes.TryExcept, loop: Union[nodes.For, nodes.While] ) -> bool: """Return _True if `other_node_try_except` is a descendant of `loop`.""" return any( loop_body_statement is other_node_try_except or loop_body_statement.parent_of(other_node_try_except) for loop_body_statement in loop.body ) if not _try_in_loop_body(other_node_try_except, closest_loop): for ancestor in closest_loop.node_ancestors(): if isinstance(ancestor, (nodes.For, nodes.While)): if _try_in_loop_body(other_node_try_except, ancestor): break else: # `other_node_try_except` didn't have a shared ancestor loop return _False for loop_stmt in closest_loop.body: if NamesConsumer._recursive_search_for_continue_before_break( loop_stmt, break_stmt ): break else: # No continue found, so we arrived at our special case! return _True return _False @staticmethod def _recursive_search_for_continue_before_break( stmt: nodes.Statement, break_stmt: nodes.Break ) -> bool: """Return _True if any Continue node can be found in descendants of `stmt` before encountering `break_stmt`, ignoring any nested loops. """ if stmt is break_stmt: return _False if isinstance(stmt, nodes.Continue): return _True for child in stmt.get_children(): if isinstance(stmt, (nodes.For, nodes.While)): continue if NamesConsumer._recursive_search_for_continue_before_break( child, break_stmt ): return _True return _False @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in a try block and the ``node_statement`` being evaluated is in one of its except handlers. """ uncertain_nodes: List[nodes.NodeNG] = [] closest_except_handler = utils.get_node_first_ancestor_of_type( node_statement, nodes.ExceptHandler ) if closest_except_handler is None: return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # If the other statement is the except handler guarding `node`, it executes if other_node_statement is closest_except_handler: continue # Ensure other_node is in a try block ( other_node_try_ancestor, other_node_try_ancestor_visited_child, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryExcept ) if other_node_try_ancestor is None: continue if ( other_node_try_ancestor_visited_child not in other_node_try_ancestor.body ): continue # Make sure nesting is correct -- there should be at least one # except handler that is a sibling attached to the try ancestor, # or is an ancestor of the try ancestor. if not any( closest_except_handler in other_node_try_ancestor.handlers or other_node_try_ancestor_except_handler in closest_except_handler.node_ancestors() for other_node_try_ancestor_except_handler in other_node_try_ancestor.handlers ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: uncertain_nodes: List[nodes.NodeNG] = [] ( closest_try_finally_ancestor, child_of_closest_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( node_statement, nodes.TryFinally ) if closest_try_finally_ancestor is None: return uncertain_nodes if ( child_of_closest_try_finally_ancestor not in closest_try_finally_ancestor.finalbody ): return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) ( other_node_try_finally_ancestor, child_of_other_node_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryFinally ) if other_node_try_finally_ancestor is None: continue # other_node needs to descend from the try of a try/finally. if ( child_of_other_node_try_finally_ancestor not in other_node_try_finally_ancestor.body ): continue # If the two try/finally ancestors are not the same, then # node_statement's closest try/finally ancestor needs to be in # the final body of other_node's try/finally ancestor, or # descend from one of the statements in that final body. if ( other_node_try_finally_ancestor is not closest_try_finally_ancestor and not any( other_node_final_statement is closest_try_finally_ancestor or other_node_final_statement.parent_of( closest_try_finally_ancestor ) for other_node_final_statement in other_node_try_finally_ancestor.finalbody ) ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
708
statement
ref
function
other_node_statement = other_node.statement(future=True)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
710
get_node_first_ancestor_of_type
ref
function
closest_except_handler = utils.get_node_first_ancestor_of_type(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
716
parent_of
ref
function
if closest_except_handler.parent_of(node):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
730
parent_of
ref
function
and closest_try_except.parent.parent_of(node_statement)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
736
_defines_name_raises_or_returns
ref
function
NamesConsumer._defines_name_raises_or_returns(node.name, handler)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
741
_check_loop_finishes_via_except
ref
function
if NamesConsumer._check_loop_finishes_via_except(node, closest_try_except):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
749
_defines_name_raises_or_returns
def
function
def _defines_name_raises_or_returns( name: str, handler: nodes.ExceptHandler ) -> bool: """Return _True if some child of `handler` defines the name `name`, raises, or returns. """ def _define_raise_or_return(stmt: nodes.NodeNG) -> bool: if isinstance(stmt, (nodes.Raise, nodes.Return)): return _True if isinstance(stmt, nodes.Assign): for target in stmt.targets: for elt in utils.get_all_elements(target): if isinstance(elt, nodes.AssignName) and elt.name == name: return _True if isinstance(stmt, nodes.If): # Check for assignments inside the test if ( isinstance(stmt.test, nodes.NamedExpr) and stmt.test.target.name == name ): return _True if isinstance(stmt.test, nodes.Call): for arg_or_kwarg in stmt.test.args + [ kw.value for kw in stmt.test.keywords ]: if ( isinstance(arg_or_kwarg, nodes.NamedExpr) and arg_or_kwarg.target.name == name ): return _True return _False for stmt in handler.get_children(): if _define_raise_or_return(stmt): return _True if isinstance(stmt, (nodes.If, nodes.With)): if any( _define_raise_or_return(nested_stmt) for nested_stmt in stmt.get_children() ): return _True return _False @staticmethod def _check_loop_finishes_via_except( node: nodes.NodeNG, other_node_try_except: nodes.TryExcept ) -> bool: """Check for a case described in https://github.com/PyCQA/pylint/issues/5683. It consists of a specific control flow scenario where the only non-break exit from a loop consists of the very except handler we are examining, such that code in the `else` branch of the loop can depend on it being assigned. Example: for _ in range(3): try: do_something() except: name = 1 <-- only non-break exit from loop else: break else: print(name) """ if not other_node_try_except.orelse: return _False closest_loop: Optional[ Union[nodes.For, nodes.While] ] = utils.get_node_first_ancestor_of_type(node, (nodes.For, nodes.While)) if closest_loop is None: return _False if not any( else_statement is node or else_statement.parent_of(node) for else_statement in closest_loop.orelse ): # `node` not guarded by `else` return _False for inner_else_statement in other_node_try_except.orelse: if isinstance(inner_else_statement, nodes.Break): break_stmt = inner_else_statement break else: # No break statement return _False def _try_in_loop_body( other_node_try_except: nodes.TryExcept, loop: Union[nodes.For, nodes.While] ) -> bool: """Return _True if `other_node_try_except` is a descendant of `loop`.""" return any( loop_body_statement is other_node_try_except or loop_body_statement.parent_of(other_node_try_except) for loop_body_statement in loop.body ) if not _try_in_loop_body(other_node_try_except, closest_loop): for ancestor in closest_loop.node_ancestors(): if isinstance(ancestor, (nodes.For, nodes.While)): if _try_in_loop_body(other_node_try_except, ancestor): break else: # `other_node_try_except` didn't have a shared ancestor loop return _False for loop_stmt in closest_loop.body: if NamesConsumer._recursive_search_for_continue_before_break( loop_stmt, break_stmt ): break else: # No continue found, so we arrived at our special case! return _True return _False @staticmethod def _recursive_search_for_continue_before_break( stmt: nodes.Statement, break_stmt: nodes.Break ) -> bool: """Return _True if any Continue node can be found in descendants of `stmt` before encountering `break_stmt`, ignoring any nested loops. """ if stmt is break_stmt: return _False if isinstance(stmt, nodes.Continue): return _True for child in stmt.get_children(): if isinstance(stmt, (nodes.For, nodes.While)): continue if NamesConsumer._recursive_search_for_continue_before_break( child, break_stmt ): return _True return _False @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in a try block and the ``node_statement`` being evaluated is in one of its except handlers. """ uncertain_nodes: List[nodes.NodeNG] = [] closest_except_handler = utils.get_node_first_ancestor_of_type( node_statement, nodes.ExceptHandler ) if closest_except_handler is None: return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # If the other statement is the except handler guarding `node`, it executes if other_node_statement is closest_except_handler: continue # Ensure other_node is in a try block ( other_node_try_ancestor, other_node_try_ancestor_visited_child, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryExcept ) if other_node_try_ancestor is None: continue if ( other_node_try_ancestor_visited_child not in other_node_try_ancestor.body ): continue # Make sure nesting is correct -- there should be at least one # except handler that is a sibling attached to the try ancestor, # or is an ancestor of the try ancestor. if not any( closest_except_handler in other_node_try_ancestor.handlers or other_node_try_ancestor_except_handler in closest_except_handler.node_ancestors() for other_node_try_ancestor_except_handler in other_node_try_ancestor.handlers ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: uncertain_nodes: List[nodes.NodeNG] = [] ( closest_try_finally_ancestor, child_of_closest_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( node_statement, nodes.TryFinally ) if closest_try_finally_ancestor is None: return uncertain_nodes if ( child_of_closest_try_finally_ancestor not in closest_try_finally_ancestor.finalbody ): return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) ( other_node_try_finally_ancestor, child_of_other_node_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryFinally ) if other_node_try_finally_ancestor is None: continue # other_node needs to descend from the try of a try/finally. if ( child_of_other_node_try_finally_ancestor not in other_node_try_finally_ancestor.body ): continue # If the two try/finally ancestors are not the same, then # node_statement's closest try/finally ancestor needs to be in # the final body of other_node's try/finally ancestor, or # descend from one of the statements in that final body. if ( other_node_try_finally_ancestor is not closest_try_finally_ancestor and not any( other_node_final_statement is closest_try_finally_ancestor or other_node_final_statement.parent_of( closest_try_finally_ancestor ) for other_node_final_statement in other_node_try_finally_ancestor.finalbody ) ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
756
_define_raise_or_return
def
function
def _define_raise_or_return(stmt: nodes.NodeNG) -> bool: if isinstance(stmt, (nodes.Raise, nodes.Return)): return _True if isinstance(stmt, nodes.Assign): for target in stmt.targets: for elt in utils.get_all_elements(target): if isinstance(elt, nodes.AssignName) and elt.name == name: return _True if isinstance(stmt, nodes.If): # Check for assignments inside the test if ( isinstance(stmt.test, nodes.NamedExpr) and stmt.test.target.name == name ): return _True if isinstance(stmt.test, nodes.Call): for arg_or_kwarg in stmt.test.args + [ kw.value for kw in stmt.test.keywords ]: if ( isinstance(arg_or_kwarg, nodes.NamedExpr) and arg_or_kwarg.target.name == name ): return _True return _False for stmt in handler.get_children(): if _define_raise_or_return(stmt): return _True if isinstance(stmt, (nodes.If, nodes.With)): if any( _define_raise_or_return(nested_stmt) for nested_stmt in stmt.get_children() ): return _True return _False @staticmethod def _check_loop_finishes_via_except( node: nodes.NodeNG, other_node_try_except: nodes.TryExcept ) -> bool: """Check for a case described in https://github.com/PyCQA/pylint/issues/5683. It consists of a specific control flow scenario where the only non-break exit from a loop consists of the very except handler we are examining, such that code in the `else` branch of the loop can depend on it being assigned. Example: for _ in range(3): try: do_something() except: name = 1 <-- only non-break exit from loop else: break else: print(name) """ if not other_node_try_except.orelse: return _False closest_loop: Optional[ Union[nodes.For, nodes.While] ] = utils.get_node_first_ancestor_of_type(node, (nodes.For, nodes.While)) if closest_loop is None: return _False if not any( else_statement is node or else_statement.parent_of(node) for else_statement in closest_loop.orelse ): # `node` not guarded by `else` return _False for inner_else_statement in other_node_try_except.orelse: if isinstance(inner_else_statement, nodes.Break): break_stmt = inner_else_statement break else: # No break statement return _False def _try_in_loop_body( other_node_try_except: nodes.TryExcept, loop: Union[nodes.For, nodes.While] ) -> bool: """Return _True if `other_node_try_except` is a descendant of `loop`.""" return any( loop_body_statement is other_node_try_except or loop_body_statement.parent_of(other_node_try_except) for loop_body_statement in loop.body ) if not _try_in_loop_body(other_node_try_except, closest_loop): for ancestor in closest_loop.node_ancestors(): if isinstance(ancestor, (nodes.For, nodes.While)): if _try_in_loop_body(other_node_try_except, ancestor): break else: # `other_node_try_except` didn't have a shared ancestor loop return _False for loop_stmt in closest_loop.body: if NamesConsumer._recursive_search_for_continue_before_break( loop_stmt, break_stmt ): break else: # No continue found, so we arrived at our special case! return _True return _False @staticmethod def _recursive_search_for_continue_before_break( stmt: nodes.Statement, break_stmt: nodes.Break ) -> bool: """Return _True if any Continue node can be found in descendants of `stmt` before encountering `break_stmt`, ignoring any nested loops. """ if stmt is break_stmt: return _False if isinstance(stmt, nodes.Continue): return _True for child in stmt.get_children(): if isinstance(stmt, (nodes.For, nodes.While)): continue if NamesConsumer._recursive_search_for_continue_before_break( child, break_stmt ): return _True return _False @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in a try block and the ``node_statement`` being evaluated is in one of its except handlers. """ uncertain_nodes: List[nodes.NodeNG] = [] closest_except_handler = utils.get_node_first_ancestor_of_type( node_statement, nodes.ExceptHandler ) if closest_except_handler is None: return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # If the other statement is the except handler guarding `node`, it executes if other_node_statement is closest_except_handler: continue # Ensure other_node is in a try block ( other_node_try_ancestor, other_node_try_ancestor_visited_child, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryExcept ) if other_node_try_ancestor is None: continue if ( other_node_try_ancestor_visited_child not in other_node_try_ancestor.body ): continue # Make sure nesting is correct -- there should be at least one # except handler that is a sibling attached to the try ancestor, # or is an ancestor of the try ancestor. if not any( closest_except_handler in other_node_try_ancestor.handlers or other_node_try_ancestor_except_handler in closest_except_handler.node_ancestors() for other_node_try_ancestor_except_handler in other_node_try_ancestor.handlers ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: uncertain_nodes: List[nodes.NodeNG] = [] ( closest_try_finally_ancestor, child_of_closest_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( node_statement, nodes.TryFinally ) if closest_try_finally_ancestor is None: return uncertain_nodes if ( child_of_closest_try_finally_ancestor not in closest_try_finally_ancestor.finalbody ): return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) ( other_node_try_finally_ancestor, child_of_other_node_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryFinally ) if other_node_try_finally_ancestor is None: continue # other_node needs to descend from the try of a try/finally. if ( child_of_other_node_try_finally_ancestor not in other_node_try_finally_ancestor.body ): continue # If the two try/finally ancestors are not the same, then # node_statement's closest try/finally ancestor needs to be in # the final body of other_node's try/finally ancestor, or # descend from one of the statements in that final body. if ( other_node_try_finally_ancestor is not closest_try_finally_ancestor and not any( other_node_final_statement is closest_try_finally_ancestor or other_node_final_statement.parent_of( closest_try_finally_ancestor ) for other_node_final_statement in other_node_try_finally_ancestor.finalbody ) ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
761
get_all_elements
ref
function
for elt in utils.get_all_elements(target):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
782
get_children
ref
function
for stmt in handler.get_children():
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
783
_define_raise_or_return
ref
function
if _define_raise_or_return(stmt):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
787
_define_raise_or_return
ref
function
_define_raise_or_return(nested_stmt)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
788
get_children
ref
function
for nested_stmt in stmt.get_children()
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
794
_check_loop_finishes_via_except
def
function
def _check_loop_finishes_via_except( node: nodes.NodeNG, other_node_try_except: nodes.TryExcept ) -> bool: """Check for a case described in https://github.com/PyCQA/pylint/issues/5683. It consists of a specific control flow scenario where the only non-break exit from a loop consists of the very except handler we are examining, such that code in the `else` branch of the loop can depend on it being assigned. Example: for _ in range(3): try: do_something() except: name = 1 <-- only non-break exit from loop else: break else: print(name) """ if not other_node_try_except.orelse: return _False closest_loop: Optional[ Union[nodes.For, nodes.While] ] = utils.get_node_first_ancestor_of_type(node, (nodes.For, nodes.While)) if closest_loop is None: return _False if not any( else_statement is node or else_statement.parent_of(node) for else_statement in closest_loop.orelse ): # `node` not guarded by `else` return _False for inner_else_statement in other_node_try_except.orelse: if isinstance(inner_else_statement, nodes.Break): break_stmt = inner_else_statement break else: # No break statement return _False def _try_in_loop_body( other_node_try_except: nodes.TryExcept, loop: Union[nodes.For, nodes.While] ) -> bool: """Return _True if `other_node_try_except` is a descendant of `loop`.""" return any( loop_body_statement is other_node_try_except or loop_body_statement.parent_of(other_node_try_except) for loop_body_statement in loop.body ) if not _try_in_loop_body(other_node_try_except, closest_loop): for ancestor in closest_loop.node_ancestors(): if isinstance(ancestor, (nodes.For, nodes.While)): if _try_in_loop_body(other_node_try_except, ancestor): break else: # `other_node_try_except` didn't have a shared ancestor loop return _False for loop_stmt in closest_loop.body: if NamesConsumer._recursive_search_for_continue_before_break( loop_stmt, break_stmt ): break else: # No continue found, so we arrived at our special case! return _True return _False @staticmethod def _recursive_search_for_continue_before_break( stmt: nodes.Statement, break_stmt: nodes.Break ) -> bool: """Return _True if any Continue node can be found in descendants of `stmt` before encountering `break_stmt`, ignoring any nested loops. """ if stmt is break_stmt: return _False if isinstance(stmt, nodes.Continue): return _True for child in stmt.get_children(): if isinstance(stmt, (nodes.For, nodes.While)): continue if NamesConsumer._recursive_search_for_continue_before_break( child, break_stmt ): return _True return _False @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in a try block and the ``node_statement`` being evaluated is in one of its except handlers. """ uncertain_nodes: List[nodes.NodeNG] = [] closest_except_handler = utils.get_node_first_ancestor_of_type( node_statement, nodes.ExceptHandler ) if closest_except_handler is None: return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # If the other statement is the except handler guarding `node`, it executes if other_node_statement is closest_except_handler: continue # Ensure other_node is in a try block ( other_node_try_ancestor, other_node_try_ancestor_visited_child, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryExcept ) if other_node_try_ancestor is None: continue if ( other_node_try_ancestor_visited_child not in other_node_try_ancestor.body ): continue # Make sure nesting is correct -- there should be at least one # except handler that is a sibling attached to the try ancestor, # or is an ancestor of the try ancestor. if not any( closest_except_handler in other_node_try_ancestor.handlers or other_node_try_ancestor_except_handler in closest_except_handler.node_ancestors() for other_node_try_ancestor_except_handler in other_node_try_ancestor.handlers ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: uncertain_nodes: List[nodes.NodeNG] = [] ( closest_try_finally_ancestor, child_of_closest_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( node_statement, nodes.TryFinally ) if closest_try_finally_ancestor is None: return uncertain_nodes if ( child_of_closest_try_finally_ancestor not in closest_try_finally_ancestor.finalbody ): return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) ( other_node_try_finally_ancestor, child_of_other_node_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryFinally ) if other_node_try_finally_ancestor is None: continue # other_node needs to descend from the try of a try/finally. if ( child_of_other_node_try_finally_ancestor not in other_node_try_finally_ancestor.body ): continue # If the two try/finally ancestors are not the same, then # node_statement's closest try/finally ancestor needs to be in # the final body of other_node's try/finally ancestor, or # descend from one of the statements in that final body. if ( other_node_try_finally_ancestor is not closest_try_finally_ancestor and not any( other_node_final_statement is closest_try_finally_ancestor or other_node_final_statement.parent_of( closest_try_finally_ancestor ) for other_node_final_statement in other_node_try_finally_ancestor.finalbody ) ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
819
get_node_first_ancestor_of_type
ref
function
] = utils.get_node_first_ancestor_of_type(node, (nodes.For, nodes.While))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
823
parent_of
ref
function
else_statement is node or else_statement.parent_of(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
836
_try_in_loop_body
def
function
def _try_in_loop_body( other_node_try_except: nodes.TryExcept, loop: Union[nodes.For, nodes.While] ) -> bool: """Return _True if `other_node_try_except` is a descendant of `loop`.""" return any( loop_body_statement is other_node_try_except or loop_body_statement.parent_of(other_node_try_except) for loop_body_statement in loop.body ) if not _try_in_loop_body(other_node_try_except, closest_loop): for ancestor in closest_loop.node_ancestors(): if isinstance(ancestor, (nodes.For, nodes.While)): if _try_in_loop_body(other_node_try_except, ancestor): break else: # `other_node_try_except` didn't have a shared ancestor loop return _False for loop_stmt in closest_loop.body: if NamesConsumer._recursive_search_for_continue_before_break( loop_stmt, break_stmt ): break else: # No continue found, so we arrived at our special case! return _True return _False @staticmethod def _recursive_search_for_continue_before_break( stmt: nodes.Statement, break_stmt: nodes.Break ) -> bool: """Return _True if any Continue node can be found in descendants of `stmt` before encountering `break_stmt`, ignoring any nested loops. """ if stmt is break_stmt: return _False if isinstance(stmt, nodes.Continue): return _True for child in stmt.get_children(): if isinstance(stmt, (nodes.For, nodes.While)): continue if NamesConsumer._recursive_search_for_continue_before_break( child, break_stmt ): return _True return _False @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in a try block and the ``node_statement`` being evaluated is in one of its except handlers. """ uncertain_nodes: List[nodes.NodeNG] = [] closest_except_handler = utils.get_node_first_ancestor_of_type( node_statement, nodes.ExceptHandler ) if closest_except_handler is None: return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # If the other statement is the except handler guarding `node`, it executes if other_node_statement is closest_except_handler: continue # Ensure other_node is in a try block ( other_node_try_ancestor, other_node_try_ancestor_visited_child, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryExcept ) if other_node_try_ancestor is None: continue if ( other_node_try_ancestor_visited_child not in other_node_try_ancestor.body ): continue # Make sure nesting is correct -- there should be at least one # except handler that is a sibling attached to the try ancestor, # or is an ancestor of the try ancestor. if not any( closest_except_handler in other_node_try_ancestor.handlers or other_node_try_ancestor_except_handler in closest_except_handler.node_ancestors() for other_node_try_ancestor_except_handler in other_node_try_ancestor.handlers ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: uncertain_nodes: List[nodes.NodeNG] = [] ( closest_try_finally_ancestor, child_of_closest_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( node_statement, nodes.TryFinally ) if closest_try_finally_ancestor is None: return uncertain_nodes if ( child_of_closest_try_finally_ancestor not in closest_try_finally_ancestor.finalbody ): return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) ( other_node_try_finally_ancestor, child_of_other_node_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryFinally ) if other_node_try_finally_ancestor is None: continue # other_node needs to descend from the try of a try/finally. if ( child_of_other_node_try_finally_ancestor not in other_node_try_finally_ancestor.body ): continue # If the two try/finally ancestors are not the same, then # node_statement's closest try/finally ancestor needs to be in # the final body of other_node's try/finally ancestor, or # descend from one of the statements in that final body. if ( other_node_try_finally_ancestor is not closest_try_finally_ancestor and not any( other_node_final_statement is closest_try_finally_ancestor or other_node_final_statement.parent_of( closest_try_finally_ancestor ) for other_node_final_statement in other_node_try_finally_ancestor.finalbody ) ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
842
parent_of
ref
function
or loop_body_statement.parent_of(other_node_try_except)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
846
_try_in_loop_body
ref
function
if not _try_in_loop_body(other_node_try_except, closest_loop):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
847
node_ancestors
ref
function
for ancestor in closest_loop.node_ancestors():
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
849
_try_in_loop_body
ref
function
if _try_in_loop_body(other_node_try_except, ancestor):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
856
_recursive_search_for_continue_before_break
ref
function
if NamesConsumer._recursive_search_for_continue_before_break(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
866
_recursive_search_for_continue_before_break
def
function
def _recursive_search_for_continue_before_break( stmt: nodes.Statement, break_stmt: nodes.Break ) -> bool: """Return _True if any Continue node can be found in descendants of `stmt` before encountering `break_stmt`, ignoring any nested loops. """ if stmt is break_stmt: return _False if isinstance(stmt, nodes.Continue): return _True for child in stmt.get_children(): if isinstance(stmt, (nodes.For, nodes.While)): continue if NamesConsumer._recursive_search_for_continue_before_break( child, break_stmt ): return _True return _False @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in a try block and the ``node_statement`` being evaluated is in one of its except handlers. """ uncertain_nodes: List[nodes.NodeNG] = [] closest_except_handler = utils.get_node_first_ancestor_of_type( node_statement, nodes.ExceptHandler ) if closest_except_handler is None: return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # If the other statement is the except handler guarding `node`, it executes if other_node_statement is closest_except_handler: continue # Ensure other_node is in a try block ( other_node_try_ancestor, other_node_try_ancestor_visited_child, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryExcept ) if other_node_try_ancestor is None: continue if ( other_node_try_ancestor_visited_child not in other_node_try_ancestor.body ): continue # Make sure nesting is correct -- there should be at least one # except handler that is a sibling attached to the try ancestor, # or is an ancestor of the try ancestor. if not any( closest_except_handler in other_node_try_ancestor.handlers or other_node_try_ancestor_except_handler in closest_except_handler.node_ancestors() for other_node_try_ancestor_except_handler in other_node_try_ancestor.handlers ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: uncertain_nodes: List[nodes.NodeNG] = [] ( closest_try_finally_ancestor, child_of_closest_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( node_statement, nodes.TryFinally ) if closest_try_finally_ancestor is None: return uncertain_nodes if ( child_of_closest_try_finally_ancestor not in closest_try_finally_ancestor.finalbody ): return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) ( other_node_try_finally_ancestor, child_of_other_node_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryFinally ) if other_node_try_finally_ancestor is None: continue # other_node needs to descend from the try of a try/finally. if ( child_of_other_node_try_finally_ancestor not in other_node_try_finally_ancestor.body ): continue # If the two try/finally ancestors are not the same, then # node_statement's closest try/finally ancestor needs to be in # the final body of other_node's try/finally ancestor, or # descend from one of the statements in that final body. if ( other_node_try_finally_ancestor is not closest_try_finally_ancestor and not any( other_node_final_statement is closest_try_finally_ancestor or other_node_final_statement.parent_of( closest_try_finally_ancestor ) for other_node_final_statement in other_node_try_finally_ancestor.finalbody ) ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
876
get_children
ref
function
for child in stmt.get_children():
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
879
_recursive_search_for_continue_before_break
ref
function
if NamesConsumer._recursive_search_for_continue_before_break(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
886
_uncertain_nodes_in_try_blocks_when_evaluating_except_blocks
def
function
def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: """Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in a try block and the ``node_statement`` being evaluated is in one of its except handlers. """ uncertain_nodes: List[nodes.NodeNG] = [] closest_except_handler = utils.get_node_first_ancestor_of_type( node_statement, nodes.ExceptHandler ) if closest_except_handler is None: return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) # If the other statement is the except handler guarding `node`, it executes if other_node_statement is closest_except_handler: continue # Ensure other_node is in a try block ( other_node_try_ancestor, other_node_try_ancestor_visited_child, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryExcept ) if other_node_try_ancestor is None: continue if ( other_node_try_ancestor_visited_child not in other_node_try_ancestor.body ): continue # Make sure nesting is correct -- there should be at least one # except handler that is a sibling attached to the try ancestor, # or is an ancestor of the try ancestor. if not any( closest_except_handler in other_node_try_ancestor.handlers or other_node_try_ancestor_except_handler in closest_except_handler.node_ancestors() for other_node_try_ancestor_except_handler in other_node_try_ancestor.handlers ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes @staticmethod def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: uncertain_nodes: List[nodes.NodeNG] = [] ( closest_try_finally_ancestor, child_of_closest_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( node_statement, nodes.TryFinally ) if closest_try_finally_ancestor is None: return uncertain_nodes if ( child_of_closest_try_finally_ancestor not in closest_try_finally_ancestor.finalbody ): return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) ( other_node_try_finally_ancestor, child_of_other_node_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryFinally ) if other_node_try_finally_ancestor is None: continue # other_node needs to descend from the try of a try/finally. if ( child_of_other_node_try_finally_ancestor not in other_node_try_finally_ancestor.body ): continue # If the two try/finally ancestors are not the same, then # node_statement's closest try/finally ancestor needs to be in # the final body of other_node's try/finally ancestor, or # descend from one of the statements in that final body. if ( other_node_try_finally_ancestor is not closest_try_finally_ancestor and not any( other_node_final_statement is closest_try_finally_ancestor or other_node_final_statement.parent_of( closest_try_finally_ancestor ) for other_node_final_statement in other_node_try_finally_ancestor.finalbody ) ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
894
get_node_first_ancestor_of_type
ref
function
closest_except_handler = utils.get_node_first_ancestor_of_type(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
900
statement
ref
function
other_node_statement = other_node.statement(future=True)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
908
get_node_first_ancestor_of_type_and_its_child
ref
function
) = utils.get_node_first_ancestor_of_type_and_its_child(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
924
node_ancestors
ref
function
in closest_except_handler.node_ancestors()
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
933
_uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks
def
function
def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks( found_nodes: List[nodes.NodeNG], node_statement: nodes.Statement ) -> List[nodes.NodeNG]: uncertain_nodes: List[nodes.NodeNG] = [] ( closest_try_finally_ancestor, child_of_closest_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( node_statement, nodes.TryFinally ) if closest_try_finally_ancestor is None: return uncertain_nodes if ( child_of_closest_try_finally_ancestor not in closest_try_finally_ancestor.finalbody ): return uncertain_nodes for other_node in found_nodes: other_node_statement = other_node.statement(future=_True) ( other_node_try_finally_ancestor, child_of_other_node_try_finally_ancestor, ) = utils.get_node_first_ancestor_of_type_and_its_child( other_node_statement, nodes.TryFinally ) if other_node_try_finally_ancestor is None: continue # other_node needs to descend from the try of a try/finally. if ( child_of_other_node_try_finally_ancestor not in other_node_try_finally_ancestor.body ): continue # If the two try/finally ancestors are not the same, then # node_statement's closest try/finally ancestor needs to be in # the final body of other_node's try/finally ancestor, or # descend from one of the statements in that final body. if ( other_node_try_finally_ancestor is not closest_try_finally_ancestor and not any( other_node_final_statement is closest_try_finally_ancestor or other_node_final_statement.parent_of( closest_try_finally_ancestor ) for other_node_final_statement in other_node_try_finally_ancestor.finalbody ) ): continue # Passed all tests for uncertain execution uncertain_nodes.append(other_node) return uncertain_nodes
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
940
get_node_first_ancestor_of_type_and_its_child
ref
function
) = utils.get_node_first_ancestor_of_type_and_its_child(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
951
statement
ref
function
other_node_statement = other_node.statement(future=True)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
955
get_node_first_ancestor_of_type_and_its_child
ref
function
) = utils.get_node_first_ancestor_of_type_and_its_child(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
974
parent_of
ref
function
or other_node_final_statement.parent_of(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
987
VariablesChecker
def
class
__init__ open visit_for leave_for visit_module leave_module visit_classdef leave_classdef visit_lambda leave_lambda visit_generatorexp leave_generatorexp visit_dictcomp leave_dictcomp visit_setcomp leave_setcomp visit_functiondef leave_functiondef visit_global visit_assignname visit_delname visit_name visit_excepthandler leave_excepthandler _undefined_and_used_before_checker _should_node_be_skipped _check_consumer visit_import visit_importfrom visit_assign visit_listcomp leave_listcomp leave_assign leave_with visit_arguments _analyse_fallback_blocks _ignored_modules _allow_global_unused_variables _defined_in_function_definition _in_lambda_or_comprehension_body _is_variable_violation _maybe_used_and_assigned_at_once _is_only_type_assignment _is_first_level_self_reference _is_never_evaluated _ignore_class_scope _loopvar_name _check_is_unused _is_name_ignored _check_unused_arguments _check_late_binding_closure _should_ignore_redefined_builtin _allowed_redefined_builtin _has_homonym_in_upper_function_scope _store_type_annotation_node _store_type_annotation_names _check_self_cls_assign _check_unpacking _nodes_to_unpack _check_module_attrs _check_all _check_globals _check_imports _check_metaclasses _check_classdef_metaclasses
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,105
is_message_enabled
ref
function
self._is_undefined_variable_enabled = self.linter.is_message_enabled(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,108
is_message_enabled
ref
function
self._is_used_before_assignment_enabled = self.linter.is_message_enabled(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,111
is_message_enabled
ref
function
self._is_undefined_loop_variable_enabled = self.linter.is_message_enabled(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,115
check_messages
ref
function
@utils.check_messages("redefined-outer-name")
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,116
visit_for
def
function
def visit_for(self, node: nodes.For) -> None: assigned_to = [a.name for a in node.target.nodes_of_class(nodes.AssignName)] # Only check variables that are used dummy_rgx = self.config.dummy_variables_rgx assigned_to = [var for var in assigned_to if not dummy_rgx.match(var)] for variable in assigned_to: for outer_for, outer_variables in self._loop_variables: if variable in outer_variables and not in_for_else_branch( outer_for, node ): self.add_message( "redefined-outer-name", args=(variable, outer_for.fromlineno), node=node, ) break self._loop_variables.append((node, assigned_to)) @utils.check_messages("redefined-outer-name") def leave_for(self, node: nodes.For) -> None: self._loop_variables.pop() self._store_type_annotation_names(node) def visit_module(self, node: nodes.Module) -> None: """Visit module : update consumption analysis variable checks globals doesn't overrides builtins """ self._to_consume = [NamesConsumer(node, "module")] self._postponed_evaluation_enabled = is_postponed_evaluation_enabled(node) for name, stmts in node.locals.items(): if utils.is_builtin(name): if self._should_ignore_redefined_builtin(stmts[0]) or name == "__doc__": continue self.add_message("redefined-builtin", args=name, node=stmts[0]) @utils.check_messages( "unused-import", "unused-wildcard-import", "redefined-builtin", "undefined-all-variable", "invalid-all-object", "invalid-all-format", "unused-variable", ) def leave_module(self, node: nodes.Module) -> None: """Leave module: check globals.""" assert len(self._to_consume) == 1 self._check_metaclasses(node) not_consumed = self._to_consume.pop().to_consume # attempt to check for __all__ if defined if "__all__" in node.locals: self._check_all(node, not_consumed) # check for unused globals self._check_globals(not_consumed) # don't check unused imports in __init__ files if not self.config.init_import and node.package: return self._check_imports(not_consumed) def visit_classdef(self, node: nodes.ClassDef) -> None: """Visit class: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "class")) def leave_classdef(self, _: nodes.ClassDef) -> None: """Leave class: update consumption analysis variable.""" # do not check for not used locals here (no sense) self._to_consume.pop() def visit_lambda(self, node: nodes.Lambda) -> None: """Visit lambda: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "lambda")) def leave_lambda(self, _: nodes.Lambda) -> None: """Leave lambda: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_generatorexp(self, node: nodes.GeneratorExp) -> None: """Visit genexpr: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_generatorexp(self, _: nodes.GeneratorExp) -> None: """Leave genexpr: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_dictcomp(self, node: nodes.DictComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_dictcomp(self, _: nodes.DictComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_setcomp(self, node: nodes.SetComp) -> None: """Visit setcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_setcomp(self, _: nodes.SetComp) -> None: """Leave setcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Visit function: update consumption analysis variable and check locals.""" self._to_consume.append(NamesConsumer(node, "function")) if not ( self.linter.is_message_enabled("redefined-outer-name") or self.linter.is_message_enabled("redefined-builtin") ): return globs = node.root().globals for name, stmt in node.items(): if name in globs and not isinstance(stmt, nodes.Global): definition = globs[name][0] if ( isinstance(definition, nodes.ImportFrom) and definition.modname == FUTURE ): # It is a __future__ directive, not a symbol. continue # Do not take in account redefined names for the purpose # of type checking.: if any( isinstance(definition.parent, nodes.If) and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS for definition in globs[name] ): continue line = definition.fromlineno if not self._is_name_ignored(stmt, name): self.add_message( "redefined-outer-name", args=(name, line), node=stmt ) elif ( utils.is_builtin(name) and not self._allowed_redefined_builtin(name) and not self._should_ignore_redefined_builtin(stmt) ): # do not print Redefining builtin for additional builtins self.add_message("redefined-builtin", args=name, node=stmt) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Leave function: check function's locals are consumed.""" self._check_metaclasses(node) if node.type_comment_returns: self._store_type_annotation_node(node.type_comment_returns) if node.type_comment_args: for argument_annotation in node.type_comment_args: self._store_type_annotation_node(argument_annotation) not_consumed = self._to_consume.pop().to_consume if not ( self.linter.is_message_enabled("unused-variable") or self.linter.is_message_enabled("possibly-unused-variable") or self.linter.is_message_enabled("unused-argument") ): return # Don't check arguments of function which are only raising an exception. if utils.is_error(node): return # Don't check arguments of abstract methods or within an interface. is_method = node.is_method() if is_method and node.is_abstract(): return global_names = _flattened_scope_names(node.nodes_of_class(nodes.Global)) nonlocal_names = _flattened_scope_names(node.nodes_of_class(nodes.Nonlocal)) for name, stmts in not_consumed.items(): self._check_is_unused(name, node, stmts[0], global_names, nonlocal_names) visit_asyncfunctiondef = visit_functiondef leave_asyncfunctiondef = leave_functiondef @utils.check_messages( "global-variable-undefined", "global-variable-not-assigned", "global-statement", "global-at-module-level", "redefined-builtin", ) def visit_global(self, node: nodes.Global) -> None: """Check names imported exists in the global scope.""" frame = node.frame(future=_True) if isinstance(frame, nodes.Module): self.add_message("global-at-module-level", node=node) return module = frame.root() default_message = _True locals_ = node.scope().locals for name in node.names: try: assign_nodes = module.getattr(name) except astroid.NotFoundError: # unassigned global, skip assign_nodes = [] not_defined_locally_by_import = not any( isinstance(local, nodes.Import) for local in locals_.get(name, ()) ) if ( not utils.is_reassigned_after_current(node, name) and not utils.is_deleted_after_current(node, name) and not_defined_locally_by_import ): self.add_message("global-variable-not-assigned", args=name, node=node) default_message = _False continue for anode in assign_nodes: if ( isinstance(anode, nodes.AssignName) and anode.name in module.special_attributes ): self.add_message("redefined-builtin", args=name, node=node) break if anode.frame(future=_True) is module: # module level assignment break if ( isinstance(anode, (nodes.ClassDef, nodes.FunctionDef)) and anode.parent is module ): # module level function assignment break else: if not_defined_locally_by_import: # global undefined at the module scope self.add_message("global-variable-undefined", args=name, node=node) default_message = _False if default_message: self.add_message("global-statement", node=node) def visit_assignname(self, node: nodes.AssignName) -> None: if isinstance(node.assign_type(), nodes.AugAssign): self.visit_name(node) def visit_delname(self, node: nodes.DelName) -> None: self.visit_name(node) def visit_name(self, node: nodes.Name) -> None: """Don't add the 'utils.check_messages' decorator here! It's important that all 'Name' nodes are visited, otherwise the 'NamesConsumers' won't be correct. """ stmt = node.statement(future=_True) if stmt.fromlineno is None: # name node from an astroid built from live code, skip assert not stmt.root().file.endswith(".py") return self._undefined_and_used_before_checker(node, stmt) if self._is_undefined_loop_variable_enabled: self._loopvar_name(node) @utils.check_messages("redefined-outer-name") def visit_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return for outer_except, outer_except_assign_name in self._except_handler_names_queue: if node.name.name == outer_except_assign_name.name: self.add_message( "redefined-outer-name", args=(outer_except_assign_name.name, outer_except.fromlineno), node=node, ) break self._except_handler_names_queue.append((node, node.name)) @utils.check_messages("redefined-outer-name") def leave_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return self._except_handler_names_queue.pop() def _undefined_and_used_before_checker( self, node: nodes.Name, stmt: nodes.NodeNG ) -> None: frame = stmt.scope() start_index = len(self._to_consume) - 1 # iterates through parent scopes, from the inner to the outer base_scope_type = self._to_consume[start_index].scope_type for i in range(start_index, -1, -1): current_consumer = self._to_consume[i] # Certain nodes shouldn't be checked as they get checked another time if self._should_node_be_skipped(node, current_consumer, i == start_index): continue action, nodes_to_consume = self._check_consumer( node, stmt, frame, current_consumer, i, base_scope_type ) if nodes_to_consume: # Any nodes added to consumed_uncertain by get_next_to_consume() # should be added back so that they are marked as used. # They will have already had a chance to emit used-before-assignment. # We check here instead of before every single return in _check_consumer() nodes_to_consume += current_consumer.consumed_uncertain[node.name] current_consumer.mark_as_consumed(node.name, nodes_to_consume) if action is VariableVisitConsumerAction.CONTINUE: continue if action is VariableVisitConsumerAction.RETURN: return # we have not found the name, if it isn't a builtin, that's an # undefined name ! if ( self._is_undefined_variable_enabled and not ( node.name in nodes.Module.scope_attrs or utils.is_builtin(node.name) or node.name in self.config.additional_builtins or ( node.name == "__class__" and isinstance(frame, nodes.FunctionDef) and frame.is_method() ) ) and not utils.node_ignores_exception(node, NameError) ): self.add_message("undefined-variable", args=node.name, node=node) def _should_node_be_skipped( self, node: nodes.Name, consumer: NamesConsumer, is_start_index: bool ) -> bool: """Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks. """ if consumer.scope_type == "class": # The list of base classes in the class definition is not part # of the class body. # If the current scope is a class scope but it's not the inner # scope, ignore it. This prevents to access this scope instead of # the globals one in function members when there are some common # names. if utils.is_ancestor_name(consumer.node, node) or ( not is_start_index and self._ignore_class_scope(node) ): return _True # Ignore inner class scope for keywords in class definition if isinstance(node.parent, nodes.Keyword) and isinstance( node.parent.parent, nodes.ClassDef ): return _True elif consumer.scope_type == "function" and self._defined_in_function_definition( node, consumer.node ): # If the name node is used as a function default argument's value or as # a decorator, then start from the parent frame of the function instead # of the function frame - and thus open an inner class scope return _True elif consumer.scope_type == "lambda" and utils.is_default_argument( node, consumer.node ): return _True return _False # pylint: disable=too-many-return-statements def _check_consumer( self, node: nodes.Name, stmt: nodes.NodeNG, frame: nodes.LocalsDictNodeNG, current_consumer: NamesConsumer, consumer_level: int, base_scope_type: Any, ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Checks a consumer for conditions that should trigger messages.""" # If the name has already been consumed, only check it's not a loop # variable used outside the loop. # Avoid the case where there are homonyms inside function scope and # comprehension current scope (avoid bug #1731) if node.name in current_consumer.consumed: if utils.is_func_decorator(current_consumer.node) or not ( current_consumer.scope_type == "comprehension" and self._has_homonym_in_upper_function_scope(node, consumer_level) # But don't catch homonyms against the filter of a comprehension, # (like "if x" in "[x for x in expr() if x]") # https://github.com/PyCQA/pylint/issues/5586 and not ( ( isinstance(node.parent.parent, nodes.Comprehension) and node.parent in node.parent.parent.ifs ) # Or homonyms against values to keyword arguments # (like "var" in "[func(arg=var) for var in expr()]") or ( isinstance(node.scope(), nodes.ComprehensionScope) and isinstance(node.parent, (nodes.Call, nodes.Keyword)) ) ) ): self._check_late_binding_closure(node) self._loopvar_name(node) return (VariableVisitConsumerAction.RETURN, None) found_nodes = current_consumer.get_next_to_consume(node) if found_nodes is None: return (VariableVisitConsumerAction.CONTINUE, None) if not found_nodes: if node.name in current_consumer.consumed_uncertain: confidence = CONTROL_FLOW else: confidence = HIGH self.add_message( "used-before-assignment", args=node.name, node=node, confidence=confidence, ) # Mark for consumption any nodes added to consumed_uncertain by # get_next_to_consume() because they might not have executed. return ( VariableVisitConsumerAction.RETURN, current_consumer.consumed_uncertain[node.name], ) self._check_late_binding_closure(node) if not ( self._is_undefined_variable_enabled or self._is_used_before_assignment_enabled ): return (VariableVisitConsumerAction.RETURN, found_nodes) defnode = utils.assign_parent(found_nodes[0]) defstmt = defnode.statement(future=_True) defframe = defstmt.frame(future=_True) # The class reuses itself in the class scope. is_recursive_klass = ( frame is defframe and defframe.parent_of(node) and isinstance(defframe, nodes.ClassDef) and node.name == defframe.name ) if ( is_recursive_klass and utils.get_node_first_ancestor_of_type(node, nodes.Lambda) and ( not utils.is_default_argument(node) or node.scope().parent.scope() is not defframe ) ): # Self-referential class references are fine in lambda's -- # As long as they are not part of the default argument directly # under the scope of the parent self-referring class. # Example of valid default argument: # class MyName3: # myattr = 1 # mylambda3 = lambda: lambda a=MyName3: a # Example of invalid default argument: # class MyName4: # myattr = 1 # mylambda4 = lambda a=MyName4: lambda: a # If the above conditional is _True, # there is no possibility of undefined-variable # Also do not consume class name # (since consuming blocks subsequent checks) # -- quit return (VariableVisitConsumerAction.RETURN, None) ( maybe_before_assign, annotation_return, use_outer_definition, ) = self._is_variable_violation( node, defnode, stmt, defstmt, frame, defframe, base_scope_type, is_recursive_klass, ) if use_outer_definition: return (VariableVisitConsumerAction.CONTINUE, None) if ( maybe_before_assign and not utils.is_defined_before(node) and not astroid.are_exclusive(stmt, defstmt, ("NameError",)) ): # Used and defined in the same place, e.g `x += 1` and `del x` defined_by_stmt = defstmt is stmt and isinstance( node, (nodes.DelName, nodes.AssignName) ) if ( is_recursive_klass or defined_by_stmt or annotation_return or isinstance(defstmt, nodes.Delete) ): if not utils.node_ignores_exception(node, NameError): # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance( stmt, ( nodes.AnnAssign, nodes.FunctionDef, nodes.Arguments, ), ) and node.name in node.root().locals ): if defined_by_stmt: return (VariableVisitConsumerAction.CONTINUE, [node]) return (VariableVisitConsumerAction.CONTINUE, None) elif base_scope_type != "lambda": # E0601 may *not* occurs in lambda scope. # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif base_scope_type == "lambda": # E0601 can occur in class-level scope in lambdas, as in # the following example: # class A: # x = lambda attr: f + attr # f = 42 # We check lineno because doing the following is fine: # class A: # x = 42 # y = lambda attr: x + attr if ( isinstance(frame, nodes.ClassDef) and node.name in frame.locals and stmt.fromlineno <= defstmt.fromlineno ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) elif self._is_only_type_assignment(node, defstmt): if node.scope().locals.get(node.name): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH ) else: self.add_message( "undefined-variable", args=node.name, node=node, confidence=HIGH ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif isinstance(defstmt, nodes.ClassDef): return self._is_first_level_self_reference(node, defstmt, found_nodes) elif isinstance(defnode, nodes.NamedExpr): if isinstance(defnode.parent, nodes.IfExp): if self._is_never_evaluated(defnode, defnode.parent): self.add_message( "undefined-variable", args=node.name, node=node, confidence=INFERENCE, ) return (VariableVisitConsumerAction.RETURN, found_nodes) return (VariableVisitConsumerAction.RETURN, found_nodes) @utils.check_messages("no-name-in-module") def visit_import(self, node: nodes.Import) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return for name, _ in node.names: parts = name.split(".") try: module = next(_infer_name_module(node, parts[0])) except astroid.ResolveError: continue if not isinstance(module, nodes.Module): continue self._check_module_attrs(node, module, parts[1:]) @utils.check_messages("no-name-in-module") def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return name_parts = node.modname.split(".") try: module = node.do_import_module(name_parts[0]) except astroid.AstroidBuildingException: return module = self._check_module_attrs(node, module, name_parts[1:]) if not module: return for name, _ in node.names: if name == "*": continue self._check_module_attrs(node, module, name.split(".")) @utils.check_messages( "unbalanced-tuple-unpacking", "unpacking-non-sequence", "self-cls-assignment" ) def visit_assign(self, node: nodes.Assign) -> None: """Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned. """ self._check_self_cls_assign(node) if not isinstance(node.targets[0], (nodes.Tuple, nodes.List)): return targets = node.targets[0].itered() try: inferred = utils.safe_infer(node.value) if inferred is not None: self._check_unpacking(inferred, node, targets) except astroid.InferenceError: return # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_listcomp(self, _: nodes.ListComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def leave_assign(self, node: nodes.Assign) -> None: self._store_type_annotation_names(node) def leave_with(self, node: nodes.With) -> None: self._store_type_annotation_names(node) def visit_arguments(self, node: nodes.Arguments) -> None: for annotation in node.type_comment_args: self._store_type_annotation_node(annotation) # Relying on other checker's options, which might not have been initialized yet. @astroid.decorators.cachedproperty def _analyse_fallback_blocks(self): return get_global_option(self, "analyse-fallback-blocks", default=_False) @astroid.decorators.cachedproperty def _ignored_modules(self): return get_global_option(self, "ignored-modules", default=[]) @astroid.decorators.cachedproperty def _allow_global_unused_variables(self): return get_global_option(self, "allow-global-unused-variables", default=_True) @staticmethod def _defined_in_function_definition(node, frame): in_annotation_or_default_or_decorator = _False if ( isinstance(frame, nodes.FunctionDef) and node.statement(future=_True) is frame ): in_annotation_or_default_or_decorator = ( ( node in frame.args.annotations or node in frame.args.posonlyargs_annotations or node in frame.args.kwonlyargs_annotations or node is frame.args.varargannotation or node is frame.args.kwargannotation ) or frame.args.parent_of(node) or (frame.decorators and frame.decorators.parent_of(node)) or ( frame.returns and (node is frame.returns or frame.returns.parent_of(node)) ) ) return in_annotation_or_default_or_decorator @staticmethod def _in_lambda_or_comprehension_body( node: nodes.NodeNG, frame: nodes.NodeNG ) -> bool: """Return _True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.""" child = node parent = node.parent while parent is not None: if parent is frame: return _False if isinstance(parent, nodes.Lambda) and child is not parent.args: # Body of lambda should not have access to class attributes. return _True if isinstance(parent, nodes.Comprehension) and child is not parent.iter: # Only iter of list/set/dict/generator comprehension should have access. return _True if isinstance(parent, nodes.ComprehensionScope) and not ( parent.generators and child is parent.generators[0] ): # Body of list/set/dict/generator comprehension should not have access to class attributes. # Furthermore, only the first generator (if multiple) in comprehension should have access. return _True child = parent parent = parent.parent return _False @staticmethod def _is_variable_violation( node: nodes.Name, defnode, stmt: nodes.Statement, defstmt: nodes.Statement, frame, # scope of statement of node defframe, base_scope_type, is_recursive_klass, ) -> Tuple[bool, bool, bool]: # pylint: disable=too-many-nested-blocks maybe_before_assign = _True annotation_return = _False use_outer_definition = _False if frame is not defframe: maybe_before_assign = _detect_global_scope(node, frame, defframe) elif defframe.parent is None: # we are at the module level, check the name is not # defined in builtins if ( node.name in defframe.scope_attrs or astroid.builtin_lookup(node.name)[1] ): maybe_before_assign = _False else: # we are in a local scope, check the name is not # defined in global or builtin scope # skip this lookup if name is assigned later in function scope/lambda # Note: the node.frame() is not the same as the `frame` argument which is # equivalent to frame.statement().scope() forbid_lookup = ( isinstance(frame, nodes.FunctionDef) or isinstance(node.frame(future=_True), nodes.Lambda) ) and _assigned_locally(node) if not forbid_lookup and defframe.root().lookup(node.name)[1]: maybe_before_assign = _False use_outer_definition = stmt == defstmt and not isinstance( defnode, nodes.Comprehension ) # check if we have a nonlocal elif node.name in defframe.locals: maybe_before_assign = not any( isinstance(child, nodes.Nonlocal) and node.name in child.names for child in defframe.get_children() ) if ( base_scope_type == "lambda" and isinstance(frame, nodes.ClassDef) and node.name in frame.locals ): # This rule verifies that if the definition node of the # checked name is an Arguments node and if the name # is used a default value in the arguments defaults # and the actual definition of the variable label # is happening before the Arguments definition. # # bar = None # foo = lambda bar=bar: bar # # In this case, maybe_before_assign should be _False, otherwise # it should be _True. maybe_before_assign = not ( isinstance(defnode, nodes.Arguments) and node in defnode.defaults and frame.locals[node.name][0].fromlineno < defstmt.fromlineno ) elif isinstance(defframe, nodes.ClassDef) and isinstance( frame, nodes.FunctionDef ): # Special rule for function return annotations, # using a name defined earlier in the class containing the function. if node is frame.returns and defframe.parent_of(frame.returns): annotation_return = _True if ( frame.returns.name in defframe.locals and defframe.locals[node.name][0].lineno < frame.lineno ): # Detect class assignments with a name defined earlier in the # class. In this case, no warning should be raised. maybe_before_assign = _False else: maybe_before_assign = _True if isinstance(node.parent, nodes.Arguments): maybe_before_assign = stmt.fromlineno <= defstmt.fromlineno elif is_recursive_klass: maybe_before_assign = _True else: maybe_before_assign = ( maybe_before_assign and stmt.fromlineno <= defstmt.fromlineno ) if maybe_before_assign and stmt.fromlineno == defstmt.fromlineno: if ( isinstance(defframe, nodes.FunctionDef) and frame is defframe and defframe.parent_of(node) and stmt is not defstmt ): # Single statement function, with the statement on the # same line as the function definition maybe_before_assign = _False elif ( isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Expr, nodes.Return, ), ) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) and frame is defframe and defframe.parent_of(node) and stmt is defstmt ): # Single statement if, with assignment expression on same # line as assignment # x = b if (b := _True) else _False maybe_before_assign = _False elif ( isinstance( # pylint: disable=too-many-boolean-expressions defnode, nodes.NamedExpr ) and frame is defframe and defframe.parent_of(stmt) and stmt is defstmt and ( ( defnode.lineno == node.lineno and defnode.col_offset < node.col_offset ) or (defnode.lineno < node.lineno) or ( # Issue in the `ast` module until py39 # Nodes in a multiline string have the same lineno # Could be false-positive without check not PY39_PLUS and defnode.lineno == node.lineno and isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Return, ), ) and isinstance(defstmt.value, nodes.JoinedStr) ) ) ): # Expressions, with assignment expressions # Use only after assignment # b = (c := 2) and c maybe_before_assign = _False # Look for type checking definitions inside a type checking guard. if isinstance(defstmt, (nodes.Import, nodes.ImportFrom)): defstmt_parent = defstmt.parent if ( isinstance(defstmt_parent, nodes.If) and defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS ): # Exempt those definitions that are used inside the type checking # guard or that are defined in both type checking guard branches. used_in_branch = defstmt_parent.parent_of(node) defined_in_or_else = _False for definition in defstmt_parent.orelse: if isinstance(definition, nodes.Assign): defined_in_or_else = any( target.name == node.name for target in definition.targets if isinstance(target, nodes.AssignName) ) if defined_in_or_else: break if not used_in_branch and not defined_in_or_else: maybe_before_assign = _True return maybe_before_assign, annotation_return, use_outer_definition @staticmethod def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: """Check if `defstmt` has the potential to use and assign a name in the same statement. """ if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: # The assignment must happen as part of the first element # e.g. "assert (x:= _True), x" # NOT "assert x, (x:= _True)" value = defstmt.value.elts[0] else: value = defstmt.value if isinstance(value, nodes.IfExp): return _True if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp): return _True return isinstance(value, nodes.Call) and ( any(isinstance(kwarg.value, nodes.IfExp) for kwarg in value.keywords) or any(isinstance(arg, nodes.IfExp) for arg in value.args) ) @staticmethod def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool: """Check if variable only gets assigned a type and never a value.""" if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value: return _False defstmt_frame = defstmt.frame(future=_True) node_frame = node.frame(future=_True) parent = node while parent is not defstmt_frame.parent: parent_scope = parent.scope() local_refs = parent_scope.locals.get(node.name, []) for ref_node in local_refs: # If local ref is in the same frame as our node, but on a later lineno # we don't actually care about this local ref. # Local refs are ordered, so we break. # print(var) # var = 1 # <- irrelevant if defstmt_frame == node_frame and ref_node.lineno > node.lineno: break # If the parent of the local reference is anything but an AnnAssign # Or if the AnnAssign adds a value the variable will now have a value # var = 1 # OR # var: int = 1 if ( not isinstance(ref_node.parent, nodes.AnnAssign) or ref_node.parent.value ): return _False parent = parent_scope.parent return _True @staticmethod def _is_first_level_self_reference( node: nodes.Name, defstmt: nodes.ClassDef, found_nodes: List[nodes.NodeNG] ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Check if a first level method's annotation or default values refers to its own class, and return a consumer action """ if node.frame(future=_True).parent == defstmt and node.statement( future=_True ) == node.frame(future=_True): # Check if used as type annotation # Break if postponed evaluation is enabled if utils.is_node_in_type_annotation_context(node): if not utils.is_postponed_evaluation_enabled(node): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, None) # Check if used as default value by calling the class if isinstance(node.parent, nodes.Call) and isinstance( node.parent.parent, nodes.Arguments ): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, found_nodes) @staticmethod def _is_never_evaluated( defnode: nodes.NamedExpr, defnode_parent: nodes.IfExp ) -> bool: """Check if a NamedExpr is inside a side of if ... else that never gets evaluated """ inferred_test = utils.safe_infer(defnode_parent.test) if isinstance(inferred_test, nodes.Const): if inferred_test.value is _True and defnode == defnode_parent.orelse: return _True if inferred_test.value is _False and defnode == defnode_parent.body: return _True return _False def _ignore_class_scope(self, node): """Return _True if the node is in a local class scope, as an assignment. :param node: Node considered :type node: astroid.Node :return: _True if the node is in a local class scope, as an assignment. _False otherwise. :rtype: bool """ # Detect if we are in a local class scope, as an assignment. # For example, the following is fair game. # # class A: # b = 1 # c = lambda b=b: b * b # # class B: # tp = 1 # def func(self, arg: tp): # ... # class C: # tp = 2 # def func(self, arg=tp): # ... # class C: # class Tp: # pass # class D(Tp): # ... name = node.name frame = node.statement(future=_True).scope() in_annotation_or_default_or_decorator = self._defined_in_function_definition( node, frame ) in_ancestor_list = utils.is_ancestor_name(frame, node) if in_annotation_or_default_or_decorator or in_ancestor_list: frame_locals = frame.parent.scope().locals else: frame_locals = frame.locals return not ( (isinstance(frame, nodes.ClassDef) or in_annotation_or_default_or_decorator) and not self._in_lambda_or_comprehension_body(node, frame) and name in frame_locals ) def _loopvar_name(self, node: astroid.Name) -> None: # filter variables according to node's scope astmts = [s for s in node.lookup(node.name)[1] if hasattr(s, "assign_type")] # If this variable usage exists inside a function definition # that exists in the same loop, # the usage is safe because the function will not be defined either if # the variable is not defined. scope = node.scope() if isinstance(scope, nodes.FunctionDef) and any( asmt.scope().parent_of(scope) for asmt in astmts ): return # Filter variables according to their respective scope. Test parent # and statement to avoid #74747. This is not a total fix, which would # introduce a mechanism similar to special attribute lookup in # modules. Also, in order to get correct inference in this case, the # scope lookup rules would need to be changed to return the initial # assignment (which does not exist in code per se) as well as any later # modifications. # pylint: disable-next=too-many-boolean-expressions if ( not astmts or ( astmts[0].parent == astmts[0].root() and astmts[0].parent.parent_of(node) ) or ( astmts[0].is_statement or not isinstance(astmts[0].parent, nodes.Module) and astmts[0].statement(future=_True).parent_of(node) ) ): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement(future=_True).parent_of( stmt ) and not in_for_else_branch(astmts[i].statement(future=_True), stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) != 1: return assign = astmts[0].assign_type() if not ( isinstance(assign, (nodes.For, nodes.Comprehension, nodes.GeneratorExp)) and assign.statement(future=_True) is not node.statement(future=_True) ): return # For functions we can do more by inferring the length of the itered object if not isinstance(assign, nodes.For): self.add_message("undefined-loop-variable", args=node.name, node=node) return try: inferred = next(assign.iter.infer()) except astroid.InferenceError: self.add_message("undefined-loop-variable", args=node.name, node=node) else: if ( isinstance(inferred, astroid.Instance) and inferred.qname() == BUILTIN_RANGE ): # Consider range() objects safe, even if they might not yield any results. return # Consider sequences. sequences = ( nodes.List, nodes.Tuple, nodes.Dict, nodes.Set, astroid.objects.FrozenSet, ) if not isinstance(inferred, sequences): self.add_message("undefined-loop-variable", args=node.name, node=node) return elements = getattr(inferred, "elts", getattr(inferred, "items", [])) if not elements: self.add_message("undefined-loop-variable", args=node.name, node=node) def _check_is_unused(self, name, node, stmt, global_names, nonlocal_names): # Ignore some special names specified by user configuration. if self._is_name_ignored(stmt, name): return # Ignore names that were added dynamically to the Function scope if ( isinstance(node, nodes.FunctionDef) and name == "__class__" and len(node.locals["__class__"]) == 1 and isinstance(node.locals["__class__"][0], nodes.ClassDef) ): return # Ignore names imported by the global statement. if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)): # Detect imports, assigned to global statements. if global_names and _import_name_is_global(stmt, global_names): return argnames = list( itertools.chain(node.argnames(), [arg.name for arg in node.args.kwonlyargs]) ) # Care about functions with unknown argument (builtins) if name in argnames: self._check_unused_arguments(name, node, stmt, argnames) else: if stmt.parent and isinstance( stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple) ): if name in nonlocal_names: return qname = asname = None if isinstance(stmt, (nodes.Import, nodes.ImportFrom)): # Need the complete name, which we don't have in .locals. if len(stmt.names) > 1: import_names = next( (names for names in stmt.names if name in names), None ) else: import_names = stmt.names[0] if import_names: qname, asname = import_names name = asname or qname if _has_locals_call_after_node(stmt, node.scope()): message_name = "possibly-unused-variable" else: if isinstance(stmt, nodes.Import): if asname is not None: msg = f"{qname} imported as {asname}" else: msg = f"import {name}" self.add_message("unused-import", args=msg, node=stmt) return if isinstance(stmt, nodes.ImportFrom): if asname is not None: msg = f"{qname} imported from {stmt.modname} as {asname}" else: msg = f"{name} imported from {stmt.modname}" self.add_message("unused-import", args=msg, node=stmt) return message_name = "unused-variable" if isinstance(stmt, nodes.FunctionDef) and stmt.decorators: return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Special case for exception variable if isinstance(stmt.parent, nodes.ExceptHandler) and any( n.name == name for n in stmt.parent.nodes_of_class(nodes.Name) ): return self.add_message(message_name, args=name, node=stmt) def _is_name_ignored(self, stmt, name): authorized_rgx = self.config.dummy_variables_rgx if ( isinstance(stmt, nodes.AssignName) and isinstance(stmt.parent, nodes.Arguments) or isinstance(stmt, nodes.Arguments) ): regex = self.config.ignored_argument_names else: regex = authorized_rgx return regex and regex.match(name) def _check_unused_arguments(self, name, node, stmt, argnames): is_method = node.is_method() klass = node.parent.frame(future=_True) if is_method and isinstance(klass, nodes.ClassDef): confidence = ( INFERENCE if utils.has_known_bases(klass) else INFERENCE_FAILURE ) else: confidence = HIGH if is_method: # Don't warn for the first argument of a (non static) method if node.type != "staticmethod" and name == argnames[0]: return # Don't warn for argument of an overridden method overridden = overridden_method(klass, node.name) if overridden is not None and name in overridden.argnames(): return if node.name in utils.PYMETHODS and node.name not in ( "__init__", "__new__", ): return # Don't check callback arguments if any( node.name.startswith(cb) or node.name.endswith(cb) for cb in self.config.callbacks ): return # Don't check arguments of singledispatch.register function. if utils.is_registered_in_singledispatch_function(node): return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Don't check protocol classes if utils.is_protocol_class(klass): return self.add_message("unused-argument", args=name, node=stmt, confidence=confidence) def _check_late_binding_closure(self, node: nodes.Name) -> None: """Check whether node is a cell var that is assigned within a containing loop. Special cases where we don't care about the error: 1. When the node's function is immediately called, e.g. (lambda: i)() 2. When the node's function is returned from within the loop, e.g. return lambda: i """ if not self.linter.is_message_enabled("cell-var-from-loop"): return node_scope = node.frame(future=_True) # If node appears in a default argument expression, # look at the next enclosing frame instead if utils.is_default_argument(node, node_scope): node_scope = node_scope.parent.frame(future=_True) # Check if node is a cell var if ( not isinstance(node_scope, (nodes.Lambda, nodes.FunctionDef)) or node.name in node_scope.locals ): return assign_scope, stmts = node.lookup(node.name) if not stmts or not assign_scope.parent_of(node_scope): return if utils.is_comprehension(assign_scope): self.add_message("cell-var-from-loop", node=node, args=node.name) else: # Look for an enclosing For loop. # Currently, we only consider the first assignment assignment_node = stmts[0] maybe_for = assignment_node while maybe_for and not isinstance(maybe_for, nodes.For): if maybe_for is assign_scope: break maybe_for = maybe_for.parent else: if ( maybe_for and maybe_for.parent_of(node_scope) and not utils.is_being_called(node_scope) and node_scope.parent and not isinstance(node_scope.statement(future=_True), nodes.Return) ): self.add_message("cell-var-from-loop", node=node, args=node.name) def _should_ignore_redefined_builtin(self, stmt): if not isinstance(stmt, nodes.ImportFrom): return _False return stmt.modname in self.config.redefining_builtins_modules def _allowed_redefined_builtin(self, name): return name in self.config.allowed_redefined_builtins def _has_homonym_in_upper_function_scope( self, node: nodes.Name, index: int ) -> bool: """Return whether there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function :param node: node to check for :param index: index of the current consumer inside self._to_consume :return: _True if there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function, _False otherwise """ return any( _consumer.scope_type == "function" and node.name in _consumer.to_consume for _consumer in self._to_consume[index - 1 :: -1] ) def _store_type_annotation_node(self, type_annotation): """Given a type annotation, store all the name nodes it refers to.""" if isinstance(type_annotation, nodes.Name): self._type_annotation_names.append(type_annotation.name) return if isinstance(type_annotation, nodes.Attribute): self._store_type_annotation_node(type_annotation.expr) return if not isinstance(type_annotation, nodes.Subscript): return if ( isinstance(type_annotation.value, nodes.Attribute) and isinstance(type_annotation.value.expr, nodes.Name) and type_annotation.value.expr.name == TYPING_MODULE ): self._type_annotation_names.append(TYPING_MODULE) return self._type_annotation_names.extend( annotation.name for annotation in type_annotation.nodes_of_class(nodes.Name) ) def _store_type_annotation_names(self, node): type_annotation = node.type_annotation if not type_annotation: return self._store_type_annotation_node(node.type_annotation) def _check_self_cls_assign(self, node: nodes.Assign) -> None: """Check that self/cls don't get assigned.""" assign_names: Set[Optional[str]] = set() for target in node.targets: if isinstance(target, nodes.AssignName): assign_names.add(target.name) elif isinstance(target, nodes.Tuple): assign_names.update( elt.name for elt in target.elts if isinstance(elt, nodes.AssignName) ) scope = node.scope() nonlocals_with_same_name = any( child for child in scope.body if isinstance(child, nodes.Nonlocal) ) if nonlocals_with_same_name: scope = node.scope().parent.scope() if not ( isinstance(scope, nodes.FunctionDef) and scope.is_method() and "builtins.staticmethod" not in scope.decoratornames() ): return argument_names = scope.argnames() if not argument_names: return self_cls_name = argument_names[0] if self_cls_name in assign_names: self.add_message("self-cls-assignment", node=node, args=(self_cls_name,)) def _check_unpacking(self, inferred, node, targets): """Check for unbalanced tuple unpacking and unpacking non sequences. """ if utils.is_inside_abstract_class(node): return if utils.is_comprehension(node): return if inferred is astroid.Uninferable: return if ( isinstance(inferred.parent, nodes.Arguments) and isinstance(node.value, nodes.Name) and node.value.name == inferred.parent.vararg ): # Variable-length argument, we can't determine the length. return # Attempt to check unpacking is properly balanced values = self._nodes_to_unpack(inferred) if values is not None: if len(targets) != len(values): # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return self.add_message( "unbalanced-tuple-unpacking", node=node, args=( _get_unpacking_extra_info(node, inferred), len(targets), len(values), ), ) # attempt to check unpacking may be possible (ie RHS is iterable) elif not utils.is_iterable(inferred): self.add_message( "unpacking-non-sequence", node=node, args=(_get_unpacking_extra_info(node, inferred),), ) @staticmethod def _nodes_to_unpack(node: nodes.NodeNG) -> Optional[List[nodes.NodeNG]]: """Return the list of values of the `Assign` node.""" if isinstance(node, (nodes.Tuple, nodes.List)): return node.itered() if isinstance(node, astroid.Instance) and any( ancestor.qname() == "typing.NamedTuple" for ancestor in node.ancestors() ): return [i for i in node.values() if isinstance(i, nodes.AssignName)] return None def _check_module_attrs(self, node, module, module_names): """Check that module_names (list of string) are accessible through the given module if the latest access name corresponds to a module, return it """ while module_names: name = module_names.pop(0) if name == "__dict__": module = None break try: module = next(module.getattr(name)[0].infer()) if module is astroid.Uninferable: return None except astroid.NotFoundError: if module.name in self._ignored_modules: return None self.add_message( "no-name-in-module", args=(name, module.name), node=node ) return None except astroid.InferenceError: return None if module_names: modname = module.name if module else "__dict__" self.add_message( "no-name-in-module", node=node, args=(".".join(module_names), modname) ) return None if isinstance(module, nodes.Module): return module return None def _check_all(self, node: nodes.Module, not_consumed): assigned = next(node.igetattr("__all__")) if assigned is astroid.Uninferable: return if not assigned.pytype() in {"builtins.list", "builtins.tuple"}: line, col = assigned.tolineno, assigned.col_offset self.add_message("invalid-all-format", line=line, col_offset=col, node=node) return for elt in getattr(assigned, "elts", ()): try: elt_name = next(elt.infer()) except astroid.InferenceError: continue if elt_name is astroid.Uninferable: continue if not elt_name.parent: continue if not isinstance(elt_name, nodes.Const) or not isinstance( elt_name.value, str ): self.add_message("invalid-all-object", args=elt.as_string(), node=elt) continue elt_name = elt_name.value # If elt is in not_consumed, remove it from not_consumed if elt_name in not_consumed: del not_consumed[elt_name] continue if elt_name not in node.locals: if not node.package: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) else: basename = os.path.splitext(node.file)[0] if os.path.basename(basename) == "__init__": name = node.name + "." + elt_name try: astroid.modutils.file_from_modpath(name.split(".")) except ImportError: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) except SyntaxError: # don't yield a syntax-error warning, # because it will be later yielded # when the file will be checked pass def _check_globals(self, not_consumed): if self._allow_global_unused_variables: return for name, node_lst in not_consumed.items(): for node in node_lst: self.add_message("unused-variable", args=(name,), node=node) def _check_imports(self, not_consumed): local_names = _fix_dot_imports(not_consumed) checked = set() unused_wildcard_imports: DefaultDict[ Tuple[str, nodes.ImportFrom], List[str] ] = collections.defaultdict(list) for name, stmt in local_names: for imports in stmt.names: real_name = imported_name = imports[0] if imported_name == "*": real_name = name as_name = imports[1] if real_name in checked: continue if name not in (real_name, as_name): continue checked.add(real_name) is_type_annotation_import = ( imported_name in self._type_annotation_names or as_name in self._type_annotation_names ) if isinstance(stmt, nodes.Import) or ( isinstance(stmt, nodes.ImportFrom) and not stmt.modname ): if isinstance(stmt, nodes.ImportFrom) and SPECIAL_OBJ.search( imported_name ): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if as_name == "_": continue if as_name is None: msg = f"import {imported_name}" else: msg = f"{imported_name} imported as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) elif isinstance(stmt, nodes.ImportFrom) and stmt.modname != FUTURE: if SPECIAL_OBJ.search(imported_name): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if _is_from_future_import(stmt, name): # Check if the name is in fact loaded from a # __future__ import in another module. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if imported_name == "*": unused_wildcard_imports[(stmt.modname, stmt)].append(name) else: if as_name is None: msg = f"{imported_name} imported from {stmt.modname}" else: msg = f"{imported_name} imported from {stmt.modname} as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) # Construct string for unused-wildcard-import message for module, unused_list in unused_wildcard_imports.items(): if len(unused_list) == 1: arg_string = unused_list[0] else: arg_string = ( f"{', '.join(i for i in unused_list[:-1])} and {unused_list[-1]}" ) self.add_message( "unused-wildcard-import", args=(arg_string, module[0]), node=module[1] ) del self._to_consume def _check_metaclasses(self, node): """Update consumption analysis for metaclasses.""" consumed = [] # [(scope_locals, consumed_key)] for child_node in node.get_children(): if isinstance(child_node, nodes.ClassDef): consumed.extend(self._check_classdef_metaclasses(child_node, node)) # Pop the consumed items, in order to avoid having # unused-import and unused-variable false positives for scope_locals, name in consumed: scope_locals.pop(name, None) def _check_classdef_metaclasses(self, klass, parent_node): if not klass._metaclass: # Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors return [] consumed = [] # [(scope_locals, consumed_key)] metaclass = klass.metaclass() name = None if isinstance(klass._metaclass, nodes.Name): name = klass._metaclass.name elif isinstance(klass._metaclass, nodes.Attribute) and klass._metaclass.expr: attr = klass._metaclass.expr while not isinstance(attr, nodes.Name): attr = attr.expr name = attr.name elif metaclass: name = metaclass.root().name found = _False name = METACLASS_NAME_TRANSFORMS.get(name, name) if name: # check enclosing scopes starting from most local for scope_locals, _, _, _ in self._to_consume[::-1]: found_nodes = scope_locals.get(name, []) for found_node in found_nodes: if found_node.lineno <= klass.lineno: consumed.append((scope_locals, name)) found = _True break # Check parent scope nodes_in_parent_scope = parent_node.locals.get(name, []) for found_node_parent in nodes_in_parent_scope: if found_node_parent.lineno <= klass.lineno: found = _True break if ( not found and not metaclass and not ( name in nodes.Module.scope_attrs or utils.is_builtin(name) or name in self.config.additional_builtins ) ): self.add_message("undefined-variable", node=klass, args=(name,)) return consumed
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,117
nodes_of_class
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,125
in_for_else_branch
ref
function
if variable in outer_variables and not in_for_else_branch(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,128
add_message
ref
function
self.add_message(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,137
check_messages
ref
function
@utils.check_messages("redefined-outer-name")
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,138
leave_for
def
function
def leave_for(self, node: nodes.For) -> None: self._loop_variables.pop() self._store_type_annotation_names(node) def visit_module(self, node: nodes.Module) -> None: """Visit module : update consumption analysis variable checks globals doesn't overrides builtins """ self._to_consume = [NamesConsumer(node, "module")] self._postponed_evaluation_enabled = is_postponed_evaluation_enabled(node) for name, stmts in node.locals.items(): if utils.is_builtin(name): if self._should_ignore_redefined_builtin(stmts[0]) or name == "__doc__": continue self.add_message("redefined-builtin", args=name, node=stmts[0]) @utils.check_messages( "unused-import", "unused-wildcard-import", "redefined-builtin", "undefined-all-variable", "invalid-all-object", "invalid-all-format", "unused-variable", ) def leave_module(self, node: nodes.Module) -> None: """Leave module: check globals.""" assert len(self._to_consume) == 1 self._check_metaclasses(node) not_consumed = self._to_consume.pop().to_consume # attempt to check for __all__ if defined if "__all__" in node.locals: self._check_all(node, not_consumed) # check for unused globals self._check_globals(not_consumed) # don't check unused imports in __init__ files if not self.config.init_import and node.package: return self._check_imports(not_consumed) def visit_classdef(self, node: nodes.ClassDef) -> None: """Visit class: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "class")) def leave_classdef(self, _: nodes.ClassDef) -> None: """Leave class: update consumption analysis variable.""" # do not check for not used locals here (no sense) self._to_consume.pop() def visit_lambda(self, node: nodes.Lambda) -> None: """Visit lambda: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "lambda")) def leave_lambda(self, _: nodes.Lambda) -> None: """Leave lambda: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_generatorexp(self, node: nodes.GeneratorExp) -> None: """Visit genexpr: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_generatorexp(self, _: nodes.GeneratorExp) -> None: """Leave genexpr: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_dictcomp(self, node: nodes.DictComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_dictcomp(self, _: nodes.DictComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_setcomp(self, node: nodes.SetComp) -> None: """Visit setcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_setcomp(self, _: nodes.SetComp) -> None: """Leave setcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Visit function: update consumption analysis variable and check locals.""" self._to_consume.append(NamesConsumer(node, "function")) if not ( self.linter.is_message_enabled("redefined-outer-name") or self.linter.is_message_enabled("redefined-builtin") ): return globs = node.root().globals for name, stmt in node.items(): if name in globs and not isinstance(stmt, nodes.Global): definition = globs[name][0] if ( isinstance(definition, nodes.ImportFrom) and definition.modname == FUTURE ): # It is a __future__ directive, not a symbol. continue # Do not take in account redefined names for the purpose # of type checking.: if any( isinstance(definition.parent, nodes.If) and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS for definition in globs[name] ): continue line = definition.fromlineno if not self._is_name_ignored(stmt, name): self.add_message( "redefined-outer-name", args=(name, line), node=stmt ) elif ( utils.is_builtin(name) and not self._allowed_redefined_builtin(name) and not self._should_ignore_redefined_builtin(stmt) ): # do not print Redefining builtin for additional builtins self.add_message("redefined-builtin", args=name, node=stmt) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Leave function: check function's locals are consumed.""" self._check_metaclasses(node) if node.type_comment_returns: self._store_type_annotation_node(node.type_comment_returns) if node.type_comment_args: for argument_annotation in node.type_comment_args: self._store_type_annotation_node(argument_annotation) not_consumed = self._to_consume.pop().to_consume if not ( self.linter.is_message_enabled("unused-variable") or self.linter.is_message_enabled("possibly-unused-variable") or self.linter.is_message_enabled("unused-argument") ): return # Don't check arguments of function which are only raising an exception. if utils.is_error(node): return # Don't check arguments of abstract methods or within an interface. is_method = node.is_method() if is_method and node.is_abstract(): return global_names = _flattened_scope_names(node.nodes_of_class(nodes.Global)) nonlocal_names = _flattened_scope_names(node.nodes_of_class(nodes.Nonlocal)) for name, stmts in not_consumed.items(): self._check_is_unused(name, node, stmts[0], global_names, nonlocal_names) visit_asyncfunctiondef = visit_functiondef leave_asyncfunctiondef = leave_functiondef @utils.check_messages( "global-variable-undefined", "global-variable-not-assigned", "global-statement", "global-at-module-level", "redefined-builtin", ) def visit_global(self, node: nodes.Global) -> None: """Check names imported exists in the global scope.""" frame = node.frame(future=_True) if isinstance(frame, nodes.Module): self.add_message("global-at-module-level", node=node) return module = frame.root() default_message = _True locals_ = node.scope().locals for name in node.names: try: assign_nodes = module.getattr(name) except astroid.NotFoundError: # unassigned global, skip assign_nodes = [] not_defined_locally_by_import = not any( isinstance(local, nodes.Import) for local in locals_.get(name, ()) ) if ( not utils.is_reassigned_after_current(node, name) and not utils.is_deleted_after_current(node, name) and not_defined_locally_by_import ): self.add_message("global-variable-not-assigned", args=name, node=node) default_message = _False continue for anode in assign_nodes: if ( isinstance(anode, nodes.AssignName) and anode.name in module.special_attributes ): self.add_message("redefined-builtin", args=name, node=node) break if anode.frame(future=_True) is module: # module level assignment break if ( isinstance(anode, (nodes.ClassDef, nodes.FunctionDef)) and anode.parent is module ): # module level function assignment break else: if not_defined_locally_by_import: # global undefined at the module scope self.add_message("global-variable-undefined", args=name, node=node) default_message = _False if default_message: self.add_message("global-statement", node=node) def visit_assignname(self, node: nodes.AssignName) -> None: if isinstance(node.assign_type(), nodes.AugAssign): self.visit_name(node) def visit_delname(self, node: nodes.DelName) -> None: self.visit_name(node) def visit_name(self, node: nodes.Name) -> None: """Don't add the 'utils.check_messages' decorator here! It's important that all 'Name' nodes are visited, otherwise the 'NamesConsumers' won't be correct. """ stmt = node.statement(future=_True) if stmt.fromlineno is None: # name node from an astroid built from live code, skip assert not stmt.root().file.endswith(".py") return self._undefined_and_used_before_checker(node, stmt) if self._is_undefined_loop_variable_enabled: self._loopvar_name(node) @utils.check_messages("redefined-outer-name") def visit_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return for outer_except, outer_except_assign_name in self._except_handler_names_queue: if node.name.name == outer_except_assign_name.name: self.add_message( "redefined-outer-name", args=(outer_except_assign_name.name, outer_except.fromlineno), node=node, ) break self._except_handler_names_queue.append((node, node.name)) @utils.check_messages("redefined-outer-name") def leave_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return self._except_handler_names_queue.pop() def _undefined_and_used_before_checker( self, node: nodes.Name, stmt: nodes.NodeNG ) -> None: frame = stmt.scope() start_index = len(self._to_consume) - 1 # iterates through parent scopes, from the inner to the outer base_scope_type = self._to_consume[start_index].scope_type for i in range(start_index, -1, -1): current_consumer = self._to_consume[i] # Certain nodes shouldn't be checked as they get checked another time if self._should_node_be_skipped(node, current_consumer, i == start_index): continue action, nodes_to_consume = self._check_consumer( node, stmt, frame, current_consumer, i, base_scope_type ) if nodes_to_consume: # Any nodes added to consumed_uncertain by get_next_to_consume() # should be added back so that they are marked as used. # They will have already had a chance to emit used-before-assignment. # We check here instead of before every single return in _check_consumer() nodes_to_consume += current_consumer.consumed_uncertain[node.name] current_consumer.mark_as_consumed(node.name, nodes_to_consume) if action is VariableVisitConsumerAction.CONTINUE: continue if action is VariableVisitConsumerAction.RETURN: return # we have not found the name, if it isn't a builtin, that's an # undefined name ! if ( self._is_undefined_variable_enabled and not ( node.name in nodes.Module.scope_attrs or utils.is_builtin(node.name) or node.name in self.config.additional_builtins or ( node.name == "__class__" and isinstance(frame, nodes.FunctionDef) and frame.is_method() ) ) and not utils.node_ignores_exception(node, NameError) ): self.add_message("undefined-variable", args=node.name, node=node) def _should_node_be_skipped( self, node: nodes.Name, consumer: NamesConsumer, is_start_index: bool ) -> bool: """Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks. """ if consumer.scope_type == "class": # The list of base classes in the class definition is not part # of the class body. # If the current scope is a class scope but it's not the inner # scope, ignore it. This prevents to access this scope instead of # the globals one in function members when there are some common # names. if utils.is_ancestor_name(consumer.node, node) or ( not is_start_index and self._ignore_class_scope(node) ): return _True # Ignore inner class scope for keywords in class definition if isinstance(node.parent, nodes.Keyword) and isinstance( node.parent.parent, nodes.ClassDef ): return _True elif consumer.scope_type == "function" and self._defined_in_function_definition( node, consumer.node ): # If the name node is used as a function default argument's value or as # a decorator, then start from the parent frame of the function instead # of the function frame - and thus open an inner class scope return _True elif consumer.scope_type == "lambda" and utils.is_default_argument( node, consumer.node ): return _True return _False # pylint: disable=too-many-return-statements def _check_consumer( self, node: nodes.Name, stmt: nodes.NodeNG, frame: nodes.LocalsDictNodeNG, current_consumer: NamesConsumer, consumer_level: int, base_scope_type: Any, ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Checks a consumer for conditions that should trigger messages.""" # If the name has already been consumed, only check it's not a loop # variable used outside the loop. # Avoid the case where there are homonyms inside function scope and # comprehension current scope (avoid bug #1731) if node.name in current_consumer.consumed: if utils.is_func_decorator(current_consumer.node) or not ( current_consumer.scope_type == "comprehension" and self._has_homonym_in_upper_function_scope(node, consumer_level) # But don't catch homonyms against the filter of a comprehension, # (like "if x" in "[x for x in expr() if x]") # https://github.com/PyCQA/pylint/issues/5586 and not ( ( isinstance(node.parent.parent, nodes.Comprehension) and node.parent in node.parent.parent.ifs ) # Or homonyms against values to keyword arguments # (like "var" in "[func(arg=var) for var in expr()]") or ( isinstance(node.scope(), nodes.ComprehensionScope) and isinstance(node.parent, (nodes.Call, nodes.Keyword)) ) ) ): self._check_late_binding_closure(node) self._loopvar_name(node) return (VariableVisitConsumerAction.RETURN, None) found_nodes = current_consumer.get_next_to_consume(node) if found_nodes is None: return (VariableVisitConsumerAction.CONTINUE, None) if not found_nodes: if node.name in current_consumer.consumed_uncertain: confidence = CONTROL_FLOW else: confidence = HIGH self.add_message( "used-before-assignment", args=node.name, node=node, confidence=confidence, ) # Mark for consumption any nodes added to consumed_uncertain by # get_next_to_consume() because they might not have executed. return ( VariableVisitConsumerAction.RETURN, current_consumer.consumed_uncertain[node.name], ) self._check_late_binding_closure(node) if not ( self._is_undefined_variable_enabled or self._is_used_before_assignment_enabled ): return (VariableVisitConsumerAction.RETURN, found_nodes) defnode = utils.assign_parent(found_nodes[0]) defstmt = defnode.statement(future=_True) defframe = defstmt.frame(future=_True) # The class reuses itself in the class scope. is_recursive_klass = ( frame is defframe and defframe.parent_of(node) and isinstance(defframe, nodes.ClassDef) and node.name == defframe.name ) if ( is_recursive_klass and utils.get_node_first_ancestor_of_type(node, nodes.Lambda) and ( not utils.is_default_argument(node) or node.scope().parent.scope() is not defframe ) ): # Self-referential class references are fine in lambda's -- # As long as they are not part of the default argument directly # under the scope of the parent self-referring class. # Example of valid default argument: # class MyName3: # myattr = 1 # mylambda3 = lambda: lambda a=MyName3: a # Example of invalid default argument: # class MyName4: # myattr = 1 # mylambda4 = lambda a=MyName4: lambda: a # If the above conditional is _True, # there is no possibility of undefined-variable # Also do not consume class name # (since consuming blocks subsequent checks) # -- quit return (VariableVisitConsumerAction.RETURN, None) ( maybe_before_assign, annotation_return, use_outer_definition, ) = self._is_variable_violation( node, defnode, stmt, defstmt, frame, defframe, base_scope_type, is_recursive_klass, ) if use_outer_definition: return (VariableVisitConsumerAction.CONTINUE, None) if ( maybe_before_assign and not utils.is_defined_before(node) and not astroid.are_exclusive(stmt, defstmt, ("NameError",)) ): # Used and defined in the same place, e.g `x += 1` and `del x` defined_by_stmt = defstmt is stmt and isinstance( node, (nodes.DelName, nodes.AssignName) ) if ( is_recursive_klass or defined_by_stmt or annotation_return or isinstance(defstmt, nodes.Delete) ): if not utils.node_ignores_exception(node, NameError): # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance( stmt, ( nodes.AnnAssign, nodes.FunctionDef, nodes.Arguments, ), ) and node.name in node.root().locals ): if defined_by_stmt: return (VariableVisitConsumerAction.CONTINUE, [node]) return (VariableVisitConsumerAction.CONTINUE, None) elif base_scope_type != "lambda": # E0601 may *not* occurs in lambda scope. # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif base_scope_type == "lambda": # E0601 can occur in class-level scope in lambdas, as in # the following example: # class A: # x = lambda attr: f + attr # f = 42 # We check lineno because doing the following is fine: # class A: # x = 42 # y = lambda attr: x + attr if ( isinstance(frame, nodes.ClassDef) and node.name in frame.locals and stmt.fromlineno <= defstmt.fromlineno ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) elif self._is_only_type_assignment(node, defstmt): if node.scope().locals.get(node.name): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH ) else: self.add_message( "undefined-variable", args=node.name, node=node, confidence=HIGH ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif isinstance(defstmt, nodes.ClassDef): return self._is_first_level_self_reference(node, defstmt, found_nodes) elif isinstance(defnode, nodes.NamedExpr): if isinstance(defnode.parent, nodes.IfExp): if self._is_never_evaluated(defnode, defnode.parent): self.add_message( "undefined-variable", args=node.name, node=node, confidence=INFERENCE, ) return (VariableVisitConsumerAction.RETURN, found_nodes) return (VariableVisitConsumerAction.RETURN, found_nodes) @utils.check_messages("no-name-in-module") def visit_import(self, node: nodes.Import) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return for name, _ in node.names: parts = name.split(".") try: module = next(_infer_name_module(node, parts[0])) except astroid.ResolveError: continue if not isinstance(module, nodes.Module): continue self._check_module_attrs(node, module, parts[1:]) @utils.check_messages("no-name-in-module") def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return name_parts = node.modname.split(".") try: module = node.do_import_module(name_parts[0]) except astroid.AstroidBuildingException: return module = self._check_module_attrs(node, module, name_parts[1:]) if not module: return for name, _ in node.names: if name == "*": continue self._check_module_attrs(node, module, name.split(".")) @utils.check_messages( "unbalanced-tuple-unpacking", "unpacking-non-sequence", "self-cls-assignment" ) def visit_assign(self, node: nodes.Assign) -> None: """Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned. """ self._check_self_cls_assign(node) if not isinstance(node.targets[0], (nodes.Tuple, nodes.List)): return targets = node.targets[0].itered() try: inferred = utils.safe_infer(node.value) if inferred is not None: self._check_unpacking(inferred, node, targets) except astroid.InferenceError: return # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_listcomp(self, _: nodes.ListComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def leave_assign(self, node: nodes.Assign) -> None: self._store_type_annotation_names(node) def leave_with(self, node: nodes.With) -> None: self._store_type_annotation_names(node) def visit_arguments(self, node: nodes.Arguments) -> None: for annotation in node.type_comment_args: self._store_type_annotation_node(annotation) # Relying on other checker's options, which might not have been initialized yet. @astroid.decorators.cachedproperty def _analyse_fallback_blocks(self): return get_global_option(self, "analyse-fallback-blocks", default=_False) @astroid.decorators.cachedproperty def _ignored_modules(self): return get_global_option(self, "ignored-modules", default=[]) @astroid.decorators.cachedproperty def _allow_global_unused_variables(self): return get_global_option(self, "allow-global-unused-variables", default=_True) @staticmethod def _defined_in_function_definition(node, frame): in_annotation_or_default_or_decorator = _False if ( isinstance(frame, nodes.FunctionDef) and node.statement(future=_True) is frame ): in_annotation_or_default_or_decorator = ( ( node in frame.args.annotations or node in frame.args.posonlyargs_annotations or node in frame.args.kwonlyargs_annotations or node is frame.args.varargannotation or node is frame.args.kwargannotation ) or frame.args.parent_of(node) or (frame.decorators and frame.decorators.parent_of(node)) or ( frame.returns and (node is frame.returns or frame.returns.parent_of(node)) ) ) return in_annotation_or_default_or_decorator @staticmethod def _in_lambda_or_comprehension_body( node: nodes.NodeNG, frame: nodes.NodeNG ) -> bool: """Return _True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.""" child = node parent = node.parent while parent is not None: if parent is frame: return _False if isinstance(parent, nodes.Lambda) and child is not parent.args: # Body of lambda should not have access to class attributes. return _True if isinstance(parent, nodes.Comprehension) and child is not parent.iter: # Only iter of list/set/dict/generator comprehension should have access. return _True if isinstance(parent, nodes.ComprehensionScope) and not ( parent.generators and child is parent.generators[0] ): # Body of list/set/dict/generator comprehension should not have access to class attributes. # Furthermore, only the first generator (if multiple) in comprehension should have access. return _True child = parent parent = parent.parent return _False @staticmethod def _is_variable_violation( node: nodes.Name, defnode, stmt: nodes.Statement, defstmt: nodes.Statement, frame, # scope of statement of node defframe, base_scope_type, is_recursive_klass, ) -> Tuple[bool, bool, bool]: # pylint: disable=too-many-nested-blocks maybe_before_assign = _True annotation_return = _False use_outer_definition = _False if frame is not defframe: maybe_before_assign = _detect_global_scope(node, frame, defframe) elif defframe.parent is None: # we are at the module level, check the name is not # defined in builtins if ( node.name in defframe.scope_attrs or astroid.builtin_lookup(node.name)[1] ): maybe_before_assign = _False else: # we are in a local scope, check the name is not # defined in global or builtin scope # skip this lookup if name is assigned later in function scope/lambda # Note: the node.frame() is not the same as the `frame` argument which is # equivalent to frame.statement().scope() forbid_lookup = ( isinstance(frame, nodes.FunctionDef) or isinstance(node.frame(future=_True), nodes.Lambda) ) and _assigned_locally(node) if not forbid_lookup and defframe.root().lookup(node.name)[1]: maybe_before_assign = _False use_outer_definition = stmt == defstmt and not isinstance( defnode, nodes.Comprehension ) # check if we have a nonlocal elif node.name in defframe.locals: maybe_before_assign = not any( isinstance(child, nodes.Nonlocal) and node.name in child.names for child in defframe.get_children() ) if ( base_scope_type == "lambda" and isinstance(frame, nodes.ClassDef) and node.name in frame.locals ): # This rule verifies that if the definition node of the # checked name is an Arguments node and if the name # is used a default value in the arguments defaults # and the actual definition of the variable label # is happening before the Arguments definition. # # bar = None # foo = lambda bar=bar: bar # # In this case, maybe_before_assign should be _False, otherwise # it should be _True. maybe_before_assign = not ( isinstance(defnode, nodes.Arguments) and node in defnode.defaults and frame.locals[node.name][0].fromlineno < defstmt.fromlineno ) elif isinstance(defframe, nodes.ClassDef) and isinstance( frame, nodes.FunctionDef ): # Special rule for function return annotations, # using a name defined earlier in the class containing the function. if node is frame.returns and defframe.parent_of(frame.returns): annotation_return = _True if ( frame.returns.name in defframe.locals and defframe.locals[node.name][0].lineno < frame.lineno ): # Detect class assignments with a name defined earlier in the # class. In this case, no warning should be raised. maybe_before_assign = _False else: maybe_before_assign = _True if isinstance(node.parent, nodes.Arguments): maybe_before_assign = stmt.fromlineno <= defstmt.fromlineno elif is_recursive_klass: maybe_before_assign = _True else: maybe_before_assign = ( maybe_before_assign and stmt.fromlineno <= defstmt.fromlineno ) if maybe_before_assign and stmt.fromlineno == defstmt.fromlineno: if ( isinstance(defframe, nodes.FunctionDef) and frame is defframe and defframe.parent_of(node) and stmt is not defstmt ): # Single statement function, with the statement on the # same line as the function definition maybe_before_assign = _False elif ( isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Expr, nodes.Return, ), ) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) and frame is defframe and defframe.parent_of(node) and stmt is defstmt ): # Single statement if, with assignment expression on same # line as assignment # x = b if (b := _True) else _False maybe_before_assign = _False elif ( isinstance( # pylint: disable=too-many-boolean-expressions defnode, nodes.NamedExpr ) and frame is defframe and defframe.parent_of(stmt) and stmt is defstmt and ( ( defnode.lineno == node.lineno and defnode.col_offset < node.col_offset ) or (defnode.lineno < node.lineno) or ( # Issue in the `ast` module until py39 # Nodes in a multiline string have the same lineno # Could be false-positive without check not PY39_PLUS and defnode.lineno == node.lineno and isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Return, ), ) and isinstance(defstmt.value, nodes.JoinedStr) ) ) ): # Expressions, with assignment expressions # Use only after assignment # b = (c := 2) and c maybe_before_assign = _False # Look for type checking definitions inside a type checking guard. if isinstance(defstmt, (nodes.Import, nodes.ImportFrom)): defstmt_parent = defstmt.parent if ( isinstance(defstmt_parent, nodes.If) and defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS ): # Exempt those definitions that are used inside the type checking # guard or that are defined in both type checking guard branches. used_in_branch = defstmt_parent.parent_of(node) defined_in_or_else = _False for definition in defstmt_parent.orelse: if isinstance(definition, nodes.Assign): defined_in_or_else = any( target.name == node.name for target in definition.targets if isinstance(target, nodes.AssignName) ) if defined_in_or_else: break if not used_in_branch and not defined_in_or_else: maybe_before_assign = _True return maybe_before_assign, annotation_return, use_outer_definition @staticmethod def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: """Check if `defstmt` has the potential to use and assign a name in the same statement. """ if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: # The assignment must happen as part of the first element # e.g. "assert (x:= _True), x" # NOT "assert x, (x:= _True)" value = defstmt.value.elts[0] else: value = defstmt.value if isinstance(value, nodes.IfExp): return _True if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp): return _True return isinstance(value, nodes.Call) and ( any(isinstance(kwarg.value, nodes.IfExp) for kwarg in value.keywords) or any(isinstance(arg, nodes.IfExp) for arg in value.args) ) @staticmethod def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool: """Check if variable only gets assigned a type and never a value.""" if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value: return _False defstmt_frame = defstmt.frame(future=_True) node_frame = node.frame(future=_True) parent = node while parent is not defstmt_frame.parent: parent_scope = parent.scope() local_refs = parent_scope.locals.get(node.name, []) for ref_node in local_refs: # If local ref is in the same frame as our node, but on a later lineno # we don't actually care about this local ref. # Local refs are ordered, so we break. # print(var) # var = 1 # <- irrelevant if defstmt_frame == node_frame and ref_node.lineno > node.lineno: break # If the parent of the local reference is anything but an AnnAssign # Or if the AnnAssign adds a value the variable will now have a value # var = 1 # OR # var: int = 1 if ( not isinstance(ref_node.parent, nodes.AnnAssign) or ref_node.parent.value ): return _False parent = parent_scope.parent return _True @staticmethod def _is_first_level_self_reference( node: nodes.Name, defstmt: nodes.ClassDef, found_nodes: List[nodes.NodeNG] ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Check if a first level method's annotation or default values refers to its own class, and return a consumer action """ if node.frame(future=_True).parent == defstmt and node.statement( future=_True ) == node.frame(future=_True): # Check if used as type annotation # Break if postponed evaluation is enabled if utils.is_node_in_type_annotation_context(node): if not utils.is_postponed_evaluation_enabled(node): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, None) # Check if used as default value by calling the class if isinstance(node.parent, nodes.Call) and isinstance( node.parent.parent, nodes.Arguments ): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, found_nodes) @staticmethod def _is_never_evaluated( defnode: nodes.NamedExpr, defnode_parent: nodes.IfExp ) -> bool: """Check if a NamedExpr is inside a side of if ... else that never gets evaluated """ inferred_test = utils.safe_infer(defnode_parent.test) if isinstance(inferred_test, nodes.Const): if inferred_test.value is _True and defnode == defnode_parent.orelse: return _True if inferred_test.value is _False and defnode == defnode_parent.body: return _True return _False def _ignore_class_scope(self, node): """Return _True if the node is in a local class scope, as an assignment. :param node: Node considered :type node: astroid.Node :return: _True if the node is in a local class scope, as an assignment. _False otherwise. :rtype: bool """ # Detect if we are in a local class scope, as an assignment. # For example, the following is fair game. # # class A: # b = 1 # c = lambda b=b: b * b # # class B: # tp = 1 # def func(self, arg: tp): # ... # class C: # tp = 2 # def func(self, arg=tp): # ... # class C: # class Tp: # pass # class D(Tp): # ... name = node.name frame = node.statement(future=_True).scope() in_annotation_or_default_or_decorator = self._defined_in_function_definition( node, frame ) in_ancestor_list = utils.is_ancestor_name(frame, node) if in_annotation_or_default_or_decorator or in_ancestor_list: frame_locals = frame.parent.scope().locals else: frame_locals = frame.locals return not ( (isinstance(frame, nodes.ClassDef) or in_annotation_or_default_or_decorator) and not self._in_lambda_or_comprehension_body(node, frame) and name in frame_locals ) def _loopvar_name(self, node: astroid.Name) -> None: # filter variables according to node's scope astmts = [s for s in node.lookup(node.name)[1] if hasattr(s, "assign_type")] # If this variable usage exists inside a function definition # that exists in the same loop, # the usage is safe because the function will not be defined either if # the variable is not defined. scope = node.scope() if isinstance(scope, nodes.FunctionDef) and any( asmt.scope().parent_of(scope) for asmt in astmts ): return # Filter variables according to their respective scope. Test parent # and statement to avoid #74747. This is not a total fix, which would # introduce a mechanism similar to special attribute lookup in # modules. Also, in order to get correct inference in this case, the # scope lookup rules would need to be changed to return the initial # assignment (which does not exist in code per se) as well as any later # modifications. # pylint: disable-next=too-many-boolean-expressions if ( not astmts or ( astmts[0].parent == astmts[0].root() and astmts[0].parent.parent_of(node) ) or ( astmts[0].is_statement or not isinstance(astmts[0].parent, nodes.Module) and astmts[0].statement(future=_True).parent_of(node) ) ): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement(future=_True).parent_of( stmt ) and not in_for_else_branch(astmts[i].statement(future=_True), stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) != 1: return assign = astmts[0].assign_type() if not ( isinstance(assign, (nodes.For, nodes.Comprehension, nodes.GeneratorExp)) and assign.statement(future=_True) is not node.statement(future=_True) ): return # For functions we can do more by inferring the length of the itered object if not isinstance(assign, nodes.For): self.add_message("undefined-loop-variable", args=node.name, node=node) return try: inferred = next(assign.iter.infer()) except astroid.InferenceError: self.add_message("undefined-loop-variable", args=node.name, node=node) else: if ( isinstance(inferred, astroid.Instance) and inferred.qname() == BUILTIN_RANGE ): # Consider range() objects safe, even if they might not yield any results. return # Consider sequences. sequences = ( nodes.List, nodes.Tuple, nodes.Dict, nodes.Set, astroid.objects.FrozenSet, ) if not isinstance(inferred, sequences): self.add_message("undefined-loop-variable", args=node.name, node=node) return elements = getattr(inferred, "elts", getattr(inferred, "items", [])) if not elements: self.add_message("undefined-loop-variable", args=node.name, node=node) def _check_is_unused(self, name, node, stmt, global_names, nonlocal_names): # Ignore some special names specified by user configuration. if self._is_name_ignored(stmt, name): return # Ignore names that were added dynamically to the Function scope if ( isinstance(node, nodes.FunctionDef) and name == "__class__" and len(node.locals["__class__"]) == 1 and isinstance(node.locals["__class__"][0], nodes.ClassDef) ): return # Ignore names imported by the global statement. if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)): # Detect imports, assigned to global statements. if global_names and _import_name_is_global(stmt, global_names): return argnames = list( itertools.chain(node.argnames(), [arg.name for arg in node.args.kwonlyargs]) ) # Care about functions with unknown argument (builtins) if name in argnames: self._check_unused_arguments(name, node, stmt, argnames) else: if stmt.parent and isinstance( stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple) ): if name in nonlocal_names: return qname = asname = None if isinstance(stmt, (nodes.Import, nodes.ImportFrom)): # Need the complete name, which we don't have in .locals. if len(stmt.names) > 1: import_names = next( (names for names in stmt.names if name in names), None ) else: import_names = stmt.names[0] if import_names: qname, asname = import_names name = asname or qname if _has_locals_call_after_node(stmt, node.scope()): message_name = "possibly-unused-variable" else: if isinstance(stmt, nodes.Import): if asname is not None: msg = f"{qname} imported as {asname}" else: msg = f"import {name}" self.add_message("unused-import", args=msg, node=stmt) return if isinstance(stmt, nodes.ImportFrom): if asname is not None: msg = f"{qname} imported from {stmt.modname} as {asname}" else: msg = f"{name} imported from {stmt.modname}" self.add_message("unused-import", args=msg, node=stmt) return message_name = "unused-variable" if isinstance(stmt, nodes.FunctionDef) and stmt.decorators: return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Special case for exception variable if isinstance(stmt.parent, nodes.ExceptHandler) and any( n.name == name for n in stmt.parent.nodes_of_class(nodes.Name) ): return self.add_message(message_name, args=name, node=stmt) def _is_name_ignored(self, stmt, name): authorized_rgx = self.config.dummy_variables_rgx if ( isinstance(stmt, nodes.AssignName) and isinstance(stmt.parent, nodes.Arguments) or isinstance(stmt, nodes.Arguments) ): regex = self.config.ignored_argument_names else: regex = authorized_rgx return regex and regex.match(name) def _check_unused_arguments(self, name, node, stmt, argnames): is_method = node.is_method() klass = node.parent.frame(future=_True) if is_method and isinstance(klass, nodes.ClassDef): confidence = ( INFERENCE if utils.has_known_bases(klass) else INFERENCE_FAILURE ) else: confidence = HIGH if is_method: # Don't warn for the first argument of a (non static) method if node.type != "staticmethod" and name == argnames[0]: return # Don't warn for argument of an overridden method overridden = overridden_method(klass, node.name) if overridden is not None and name in overridden.argnames(): return if node.name in utils.PYMETHODS and node.name not in ( "__init__", "__new__", ): return # Don't check callback arguments if any( node.name.startswith(cb) or node.name.endswith(cb) for cb in self.config.callbacks ): return # Don't check arguments of singledispatch.register function. if utils.is_registered_in_singledispatch_function(node): return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Don't check protocol classes if utils.is_protocol_class(klass): return self.add_message("unused-argument", args=name, node=stmt, confidence=confidence) def _check_late_binding_closure(self, node: nodes.Name) -> None: """Check whether node is a cell var that is assigned within a containing loop. Special cases where we don't care about the error: 1. When the node's function is immediately called, e.g. (lambda: i)() 2. When the node's function is returned from within the loop, e.g. return lambda: i """ if not self.linter.is_message_enabled("cell-var-from-loop"): return node_scope = node.frame(future=_True) # If node appears in a default argument expression, # look at the next enclosing frame instead if utils.is_default_argument(node, node_scope): node_scope = node_scope.parent.frame(future=_True) # Check if node is a cell var if ( not isinstance(node_scope, (nodes.Lambda, nodes.FunctionDef)) or node.name in node_scope.locals ): return assign_scope, stmts = node.lookup(node.name) if not stmts or not assign_scope.parent_of(node_scope): return if utils.is_comprehension(assign_scope): self.add_message("cell-var-from-loop", node=node, args=node.name) else: # Look for an enclosing For loop. # Currently, we only consider the first assignment assignment_node = stmts[0] maybe_for = assignment_node while maybe_for and not isinstance(maybe_for, nodes.For): if maybe_for is assign_scope: break maybe_for = maybe_for.parent else: if ( maybe_for and maybe_for.parent_of(node_scope) and not utils.is_being_called(node_scope) and node_scope.parent and not isinstance(node_scope.statement(future=_True), nodes.Return) ): self.add_message("cell-var-from-loop", node=node, args=node.name) def _should_ignore_redefined_builtin(self, stmt): if not isinstance(stmt, nodes.ImportFrom): return _False return stmt.modname in self.config.redefining_builtins_modules def _allowed_redefined_builtin(self, name): return name in self.config.allowed_redefined_builtins def _has_homonym_in_upper_function_scope( self, node: nodes.Name, index: int ) -> bool: """Return whether there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function :param node: node to check for :param index: index of the current consumer inside self._to_consume :return: _True if there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function, _False otherwise """ return any( _consumer.scope_type == "function" and node.name in _consumer.to_consume for _consumer in self._to_consume[index - 1 :: -1] ) def _store_type_annotation_node(self, type_annotation): """Given a type annotation, store all the name nodes it refers to.""" if isinstance(type_annotation, nodes.Name): self._type_annotation_names.append(type_annotation.name) return if isinstance(type_annotation, nodes.Attribute): self._store_type_annotation_node(type_annotation.expr) return if not isinstance(type_annotation, nodes.Subscript): return if ( isinstance(type_annotation.value, nodes.Attribute) and isinstance(type_annotation.value.expr, nodes.Name) and type_annotation.value.expr.name == TYPING_MODULE ): self._type_annotation_names.append(TYPING_MODULE) return self._type_annotation_names.extend( annotation.name for annotation in type_annotation.nodes_of_class(nodes.Name) ) def _store_type_annotation_names(self, node): type_annotation = node.type_annotation if not type_annotation: return self._store_type_annotation_node(node.type_annotation) def _check_self_cls_assign(self, node: nodes.Assign) -> None: """Check that self/cls don't get assigned.""" assign_names: Set[Optional[str]] = set() for target in node.targets: if isinstance(target, nodes.AssignName): assign_names.add(target.name) elif isinstance(target, nodes.Tuple): assign_names.update( elt.name for elt in target.elts if isinstance(elt, nodes.AssignName) ) scope = node.scope() nonlocals_with_same_name = any( child for child in scope.body if isinstance(child, nodes.Nonlocal) ) if nonlocals_with_same_name: scope = node.scope().parent.scope() if not ( isinstance(scope, nodes.FunctionDef) and scope.is_method() and "builtins.staticmethod" not in scope.decoratornames() ): return argument_names = scope.argnames() if not argument_names: return self_cls_name = argument_names[0] if self_cls_name in assign_names: self.add_message("self-cls-assignment", node=node, args=(self_cls_name,)) def _check_unpacking(self, inferred, node, targets): """Check for unbalanced tuple unpacking and unpacking non sequences. """ if utils.is_inside_abstract_class(node): return if utils.is_comprehension(node): return if inferred is astroid.Uninferable: return if ( isinstance(inferred.parent, nodes.Arguments) and isinstance(node.value, nodes.Name) and node.value.name == inferred.parent.vararg ): # Variable-length argument, we can't determine the length. return # Attempt to check unpacking is properly balanced values = self._nodes_to_unpack(inferred) if values is not None: if len(targets) != len(values): # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return self.add_message( "unbalanced-tuple-unpacking", node=node, args=( _get_unpacking_extra_info(node, inferred), len(targets), len(values), ), ) # attempt to check unpacking may be possible (ie RHS is iterable) elif not utils.is_iterable(inferred): self.add_message( "unpacking-non-sequence", node=node, args=(_get_unpacking_extra_info(node, inferred),), ) @staticmethod def _nodes_to_unpack(node: nodes.NodeNG) -> Optional[List[nodes.NodeNG]]: """Return the list of values of the `Assign` node.""" if isinstance(node, (nodes.Tuple, nodes.List)): return node.itered() if isinstance(node, astroid.Instance) and any( ancestor.qname() == "typing.NamedTuple" for ancestor in node.ancestors() ): return [i for i in node.values() if isinstance(i, nodes.AssignName)] return None def _check_module_attrs(self, node, module, module_names): """Check that module_names (list of string) are accessible through the given module if the latest access name corresponds to a module, return it """ while module_names: name = module_names.pop(0) if name == "__dict__": module = None break try: module = next(module.getattr(name)[0].infer()) if module is astroid.Uninferable: return None except astroid.NotFoundError: if module.name in self._ignored_modules: return None self.add_message( "no-name-in-module", args=(name, module.name), node=node ) return None except astroid.InferenceError: return None if module_names: modname = module.name if module else "__dict__" self.add_message( "no-name-in-module", node=node, args=(".".join(module_names), modname) ) return None if isinstance(module, nodes.Module): return module return None def _check_all(self, node: nodes.Module, not_consumed): assigned = next(node.igetattr("__all__")) if assigned is astroid.Uninferable: return if not assigned.pytype() in {"builtins.list", "builtins.tuple"}: line, col = assigned.tolineno, assigned.col_offset self.add_message("invalid-all-format", line=line, col_offset=col, node=node) return for elt in getattr(assigned, "elts", ()): try: elt_name = next(elt.infer()) except astroid.InferenceError: continue if elt_name is astroid.Uninferable: continue if not elt_name.parent: continue if not isinstance(elt_name, nodes.Const) or not isinstance( elt_name.value, str ): self.add_message("invalid-all-object", args=elt.as_string(), node=elt) continue elt_name = elt_name.value # If elt is in not_consumed, remove it from not_consumed if elt_name in not_consumed: del not_consumed[elt_name] continue if elt_name not in node.locals: if not node.package: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) else: basename = os.path.splitext(node.file)[0] if os.path.basename(basename) == "__init__": name = node.name + "." + elt_name try: astroid.modutils.file_from_modpath(name.split(".")) except ImportError: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) except SyntaxError: # don't yield a syntax-error warning, # because it will be later yielded # when the file will be checked pass def _check_globals(self, not_consumed): if self._allow_global_unused_variables: return for name, node_lst in not_consumed.items(): for node in node_lst: self.add_message("unused-variable", args=(name,), node=node) def _check_imports(self, not_consumed): local_names = _fix_dot_imports(not_consumed) checked = set() unused_wildcard_imports: DefaultDict[ Tuple[str, nodes.ImportFrom], List[str] ] = collections.defaultdict(list) for name, stmt in local_names: for imports in stmt.names: real_name = imported_name = imports[0] if imported_name == "*": real_name = name as_name = imports[1] if real_name in checked: continue if name not in (real_name, as_name): continue checked.add(real_name) is_type_annotation_import = ( imported_name in self._type_annotation_names or as_name in self._type_annotation_names ) if isinstance(stmt, nodes.Import) or ( isinstance(stmt, nodes.ImportFrom) and not stmt.modname ): if isinstance(stmt, nodes.ImportFrom) and SPECIAL_OBJ.search( imported_name ): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if as_name == "_": continue if as_name is None: msg = f"import {imported_name}" else: msg = f"{imported_name} imported as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) elif isinstance(stmt, nodes.ImportFrom) and stmt.modname != FUTURE: if SPECIAL_OBJ.search(imported_name): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if _is_from_future_import(stmt, name): # Check if the name is in fact loaded from a # __future__ import in another module. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if imported_name == "*": unused_wildcard_imports[(stmt.modname, stmt)].append(name) else: if as_name is None: msg = f"{imported_name} imported from {stmt.modname}" else: msg = f"{imported_name} imported from {stmt.modname} as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) # Construct string for unused-wildcard-import message for module, unused_list in unused_wildcard_imports.items(): if len(unused_list) == 1: arg_string = unused_list[0] else: arg_string = ( f"{', '.join(i for i in unused_list[:-1])} and {unused_list[-1]}" ) self.add_message( "unused-wildcard-import", args=(arg_string, module[0]), node=module[1] ) del self._to_consume def _check_metaclasses(self, node): """Update consumption analysis for metaclasses.""" consumed = [] # [(scope_locals, consumed_key)] for child_node in node.get_children(): if isinstance(child_node, nodes.ClassDef): consumed.extend(self._check_classdef_metaclasses(child_node, node)) # Pop the consumed items, in order to avoid having # unused-import and unused-variable false positives for scope_locals, name in consumed: scope_locals.pop(name, None) def _check_classdef_metaclasses(self, klass, parent_node): if not klass._metaclass: # Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors return [] consumed = [] # [(scope_locals, consumed_key)] metaclass = klass.metaclass() name = None if isinstance(klass._metaclass, nodes.Name): name = klass._metaclass.name elif isinstance(klass._metaclass, nodes.Attribute) and klass._metaclass.expr: attr = klass._metaclass.expr while not isinstance(attr, nodes.Name): attr = attr.expr name = attr.name elif metaclass: name = metaclass.root().name found = _False name = METACLASS_NAME_TRANSFORMS.get(name, name) if name: # check enclosing scopes starting from most local for scope_locals, _, _, _ in self._to_consume[::-1]: found_nodes = scope_locals.get(name, []) for found_node in found_nodes: if found_node.lineno <= klass.lineno: consumed.append((scope_locals, name)) found = _True break # Check parent scope nodes_in_parent_scope = parent_node.locals.get(name, []) for found_node_parent in nodes_in_parent_scope: if found_node_parent.lineno <= klass.lineno: found = _True break if ( not found and not metaclass and not ( name in nodes.Module.scope_attrs or utils.is_builtin(name) or name in self.config.additional_builtins ) ): self.add_message("undefined-variable", node=klass, args=(name,)) return consumed
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,140
_store_type_annotation_names
ref
function
self._store_type_annotation_names(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,142
visit_module
def
function
def visit_module(self, node: nodes.Module) -> None: """Visit module : update consumption analysis variable checks globals doesn't overrides builtins """ self._to_consume = [NamesConsumer(node, "module")] self._postponed_evaluation_enabled = is_postponed_evaluation_enabled(node) for name, stmts in node.locals.items(): if utils.is_builtin(name): if self._should_ignore_redefined_builtin(stmts[0]) or name == "__doc__": continue self.add_message("redefined-builtin", args=name, node=stmts[0]) @utils.check_messages( "unused-import", "unused-wildcard-import", "redefined-builtin", "undefined-all-variable", "invalid-all-object", "invalid-all-format", "unused-variable", ) def leave_module(self, node: nodes.Module) -> None: """Leave module: check globals.""" assert len(self._to_consume) == 1 self._check_metaclasses(node) not_consumed = self._to_consume.pop().to_consume # attempt to check for __all__ if defined if "__all__" in node.locals: self._check_all(node, not_consumed) # check for unused globals self._check_globals(not_consumed) # don't check unused imports in __init__ files if not self.config.init_import and node.package: return self._check_imports(not_consumed) def visit_classdef(self, node: nodes.ClassDef) -> None: """Visit class: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "class")) def leave_classdef(self, _: nodes.ClassDef) -> None: """Leave class: update consumption analysis variable.""" # do not check for not used locals here (no sense) self._to_consume.pop() def visit_lambda(self, node: nodes.Lambda) -> None: """Visit lambda: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "lambda")) def leave_lambda(self, _: nodes.Lambda) -> None: """Leave lambda: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_generatorexp(self, node: nodes.GeneratorExp) -> None: """Visit genexpr: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_generatorexp(self, _: nodes.GeneratorExp) -> None: """Leave genexpr: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_dictcomp(self, node: nodes.DictComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_dictcomp(self, _: nodes.DictComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_setcomp(self, node: nodes.SetComp) -> None: """Visit setcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_setcomp(self, _: nodes.SetComp) -> None: """Leave setcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Visit function: update consumption analysis variable and check locals.""" self._to_consume.append(NamesConsumer(node, "function")) if not ( self.linter.is_message_enabled("redefined-outer-name") or self.linter.is_message_enabled("redefined-builtin") ): return globs = node.root().globals for name, stmt in node.items(): if name in globs and not isinstance(stmt, nodes.Global): definition = globs[name][0] if ( isinstance(definition, nodes.ImportFrom) and definition.modname == FUTURE ): # It is a __future__ directive, not a symbol. continue # Do not take in account redefined names for the purpose # of type checking.: if any( isinstance(definition.parent, nodes.If) and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS for definition in globs[name] ): continue line = definition.fromlineno if not self._is_name_ignored(stmt, name): self.add_message( "redefined-outer-name", args=(name, line), node=stmt ) elif ( utils.is_builtin(name) and not self._allowed_redefined_builtin(name) and not self._should_ignore_redefined_builtin(stmt) ): # do not print Redefining builtin for additional builtins self.add_message("redefined-builtin", args=name, node=stmt) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Leave function: check function's locals are consumed.""" self._check_metaclasses(node) if node.type_comment_returns: self._store_type_annotation_node(node.type_comment_returns) if node.type_comment_args: for argument_annotation in node.type_comment_args: self._store_type_annotation_node(argument_annotation) not_consumed = self._to_consume.pop().to_consume if not ( self.linter.is_message_enabled("unused-variable") or self.linter.is_message_enabled("possibly-unused-variable") or self.linter.is_message_enabled("unused-argument") ): return # Don't check arguments of function which are only raising an exception. if utils.is_error(node): return # Don't check arguments of abstract methods or within an interface. is_method = node.is_method() if is_method and node.is_abstract(): return global_names = _flattened_scope_names(node.nodes_of_class(nodes.Global)) nonlocal_names = _flattened_scope_names(node.nodes_of_class(nodes.Nonlocal)) for name, stmts in not_consumed.items(): self._check_is_unused(name, node, stmts[0], global_names, nonlocal_names) visit_asyncfunctiondef = visit_functiondef leave_asyncfunctiondef = leave_functiondef @utils.check_messages( "global-variable-undefined", "global-variable-not-assigned", "global-statement", "global-at-module-level", "redefined-builtin", ) def visit_global(self, node: nodes.Global) -> None: """Check names imported exists in the global scope.""" frame = node.frame(future=_True) if isinstance(frame, nodes.Module): self.add_message("global-at-module-level", node=node) return module = frame.root() default_message = _True locals_ = node.scope().locals for name in node.names: try: assign_nodes = module.getattr(name) except astroid.NotFoundError: # unassigned global, skip assign_nodes = [] not_defined_locally_by_import = not any( isinstance(local, nodes.Import) for local in locals_.get(name, ()) ) if ( not utils.is_reassigned_after_current(node, name) and not utils.is_deleted_after_current(node, name) and not_defined_locally_by_import ): self.add_message("global-variable-not-assigned", args=name, node=node) default_message = _False continue for anode in assign_nodes: if ( isinstance(anode, nodes.AssignName) and anode.name in module.special_attributes ): self.add_message("redefined-builtin", args=name, node=node) break if anode.frame(future=_True) is module: # module level assignment break if ( isinstance(anode, (nodes.ClassDef, nodes.FunctionDef)) and anode.parent is module ): # module level function assignment break else: if not_defined_locally_by_import: # global undefined at the module scope self.add_message("global-variable-undefined", args=name, node=node) default_message = _False if default_message: self.add_message("global-statement", node=node) def visit_assignname(self, node: nodes.AssignName) -> None: if isinstance(node.assign_type(), nodes.AugAssign): self.visit_name(node) def visit_delname(self, node: nodes.DelName) -> None: self.visit_name(node) def visit_name(self, node: nodes.Name) -> None: """Don't add the 'utils.check_messages' decorator here! It's important that all 'Name' nodes are visited, otherwise the 'NamesConsumers' won't be correct. """ stmt = node.statement(future=_True) if stmt.fromlineno is None: # name node from an astroid built from live code, skip assert not stmt.root().file.endswith(".py") return self._undefined_and_used_before_checker(node, stmt) if self._is_undefined_loop_variable_enabled: self._loopvar_name(node) @utils.check_messages("redefined-outer-name") def visit_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return for outer_except, outer_except_assign_name in self._except_handler_names_queue: if node.name.name == outer_except_assign_name.name: self.add_message( "redefined-outer-name", args=(outer_except_assign_name.name, outer_except.fromlineno), node=node, ) break self._except_handler_names_queue.append((node, node.name)) @utils.check_messages("redefined-outer-name") def leave_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return self._except_handler_names_queue.pop() def _undefined_and_used_before_checker( self, node: nodes.Name, stmt: nodes.NodeNG ) -> None: frame = stmt.scope() start_index = len(self._to_consume) - 1 # iterates through parent scopes, from the inner to the outer base_scope_type = self._to_consume[start_index].scope_type for i in range(start_index, -1, -1): current_consumer = self._to_consume[i] # Certain nodes shouldn't be checked as they get checked another time if self._should_node_be_skipped(node, current_consumer, i == start_index): continue action, nodes_to_consume = self._check_consumer( node, stmt, frame, current_consumer, i, base_scope_type ) if nodes_to_consume: # Any nodes added to consumed_uncertain by get_next_to_consume() # should be added back so that they are marked as used. # They will have already had a chance to emit used-before-assignment. # We check here instead of before every single return in _check_consumer() nodes_to_consume += current_consumer.consumed_uncertain[node.name] current_consumer.mark_as_consumed(node.name, nodes_to_consume) if action is VariableVisitConsumerAction.CONTINUE: continue if action is VariableVisitConsumerAction.RETURN: return # we have not found the name, if it isn't a builtin, that's an # undefined name ! if ( self._is_undefined_variable_enabled and not ( node.name in nodes.Module.scope_attrs or utils.is_builtin(node.name) or node.name in self.config.additional_builtins or ( node.name == "__class__" and isinstance(frame, nodes.FunctionDef) and frame.is_method() ) ) and not utils.node_ignores_exception(node, NameError) ): self.add_message("undefined-variable", args=node.name, node=node) def _should_node_be_skipped( self, node: nodes.Name, consumer: NamesConsumer, is_start_index: bool ) -> bool: """Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks. """ if consumer.scope_type == "class": # The list of base classes in the class definition is not part # of the class body. # If the current scope is a class scope but it's not the inner # scope, ignore it. This prevents to access this scope instead of # the globals one in function members when there are some common # names. if utils.is_ancestor_name(consumer.node, node) or ( not is_start_index and self._ignore_class_scope(node) ): return _True # Ignore inner class scope for keywords in class definition if isinstance(node.parent, nodes.Keyword) and isinstance( node.parent.parent, nodes.ClassDef ): return _True elif consumer.scope_type == "function" and self._defined_in_function_definition( node, consumer.node ): # If the name node is used as a function default argument's value or as # a decorator, then start from the parent frame of the function instead # of the function frame - and thus open an inner class scope return _True elif consumer.scope_type == "lambda" and utils.is_default_argument( node, consumer.node ): return _True return _False # pylint: disable=too-many-return-statements def _check_consumer( self, node: nodes.Name, stmt: nodes.NodeNG, frame: nodes.LocalsDictNodeNG, current_consumer: NamesConsumer, consumer_level: int, base_scope_type: Any, ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Checks a consumer for conditions that should trigger messages.""" # If the name has already been consumed, only check it's not a loop # variable used outside the loop. # Avoid the case where there are homonyms inside function scope and # comprehension current scope (avoid bug #1731) if node.name in current_consumer.consumed: if utils.is_func_decorator(current_consumer.node) or not ( current_consumer.scope_type == "comprehension" and self._has_homonym_in_upper_function_scope(node, consumer_level) # But don't catch homonyms against the filter of a comprehension, # (like "if x" in "[x for x in expr() if x]") # https://github.com/PyCQA/pylint/issues/5586 and not ( ( isinstance(node.parent.parent, nodes.Comprehension) and node.parent in node.parent.parent.ifs ) # Or homonyms against values to keyword arguments # (like "var" in "[func(arg=var) for var in expr()]") or ( isinstance(node.scope(), nodes.ComprehensionScope) and isinstance(node.parent, (nodes.Call, nodes.Keyword)) ) ) ): self._check_late_binding_closure(node) self._loopvar_name(node) return (VariableVisitConsumerAction.RETURN, None) found_nodes = current_consumer.get_next_to_consume(node) if found_nodes is None: return (VariableVisitConsumerAction.CONTINUE, None) if not found_nodes: if node.name in current_consumer.consumed_uncertain: confidence = CONTROL_FLOW else: confidence = HIGH self.add_message( "used-before-assignment", args=node.name, node=node, confidence=confidence, ) # Mark for consumption any nodes added to consumed_uncertain by # get_next_to_consume() because they might not have executed. return ( VariableVisitConsumerAction.RETURN, current_consumer.consumed_uncertain[node.name], ) self._check_late_binding_closure(node) if not ( self._is_undefined_variable_enabled or self._is_used_before_assignment_enabled ): return (VariableVisitConsumerAction.RETURN, found_nodes) defnode = utils.assign_parent(found_nodes[0]) defstmt = defnode.statement(future=_True) defframe = defstmt.frame(future=_True) # The class reuses itself in the class scope. is_recursive_klass = ( frame is defframe and defframe.parent_of(node) and isinstance(defframe, nodes.ClassDef) and node.name == defframe.name ) if ( is_recursive_klass and utils.get_node_first_ancestor_of_type(node, nodes.Lambda) and ( not utils.is_default_argument(node) or node.scope().parent.scope() is not defframe ) ): # Self-referential class references are fine in lambda's -- # As long as they are not part of the default argument directly # under the scope of the parent self-referring class. # Example of valid default argument: # class MyName3: # myattr = 1 # mylambda3 = lambda: lambda a=MyName3: a # Example of invalid default argument: # class MyName4: # myattr = 1 # mylambda4 = lambda a=MyName4: lambda: a # If the above conditional is _True, # there is no possibility of undefined-variable # Also do not consume class name # (since consuming blocks subsequent checks) # -- quit return (VariableVisitConsumerAction.RETURN, None) ( maybe_before_assign, annotation_return, use_outer_definition, ) = self._is_variable_violation( node, defnode, stmt, defstmt, frame, defframe, base_scope_type, is_recursive_klass, ) if use_outer_definition: return (VariableVisitConsumerAction.CONTINUE, None) if ( maybe_before_assign and not utils.is_defined_before(node) and not astroid.are_exclusive(stmt, defstmt, ("NameError",)) ): # Used and defined in the same place, e.g `x += 1` and `del x` defined_by_stmt = defstmt is stmt and isinstance( node, (nodes.DelName, nodes.AssignName) ) if ( is_recursive_klass or defined_by_stmt or annotation_return or isinstance(defstmt, nodes.Delete) ): if not utils.node_ignores_exception(node, NameError): # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance( stmt, ( nodes.AnnAssign, nodes.FunctionDef, nodes.Arguments, ), ) and node.name in node.root().locals ): if defined_by_stmt: return (VariableVisitConsumerAction.CONTINUE, [node]) return (VariableVisitConsumerAction.CONTINUE, None) elif base_scope_type != "lambda": # E0601 may *not* occurs in lambda scope. # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif base_scope_type == "lambda": # E0601 can occur in class-level scope in lambdas, as in # the following example: # class A: # x = lambda attr: f + attr # f = 42 # We check lineno because doing the following is fine: # class A: # x = 42 # y = lambda attr: x + attr if ( isinstance(frame, nodes.ClassDef) and node.name in frame.locals and stmt.fromlineno <= defstmt.fromlineno ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) elif self._is_only_type_assignment(node, defstmt): if node.scope().locals.get(node.name): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH ) else: self.add_message( "undefined-variable", args=node.name, node=node, confidence=HIGH ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif isinstance(defstmt, nodes.ClassDef): return self._is_first_level_self_reference(node, defstmt, found_nodes) elif isinstance(defnode, nodes.NamedExpr): if isinstance(defnode.parent, nodes.IfExp): if self._is_never_evaluated(defnode, defnode.parent): self.add_message( "undefined-variable", args=node.name, node=node, confidence=INFERENCE, ) return (VariableVisitConsumerAction.RETURN, found_nodes) return (VariableVisitConsumerAction.RETURN, found_nodes) @utils.check_messages("no-name-in-module") def visit_import(self, node: nodes.Import) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return for name, _ in node.names: parts = name.split(".") try: module = next(_infer_name_module(node, parts[0])) except astroid.ResolveError: continue if not isinstance(module, nodes.Module): continue self._check_module_attrs(node, module, parts[1:]) @utils.check_messages("no-name-in-module") def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return name_parts = node.modname.split(".") try: module = node.do_import_module(name_parts[0]) except astroid.AstroidBuildingException: return module = self._check_module_attrs(node, module, name_parts[1:]) if not module: return for name, _ in node.names: if name == "*": continue self._check_module_attrs(node, module, name.split(".")) @utils.check_messages( "unbalanced-tuple-unpacking", "unpacking-non-sequence", "self-cls-assignment" ) def visit_assign(self, node: nodes.Assign) -> None: """Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned. """ self._check_self_cls_assign(node) if not isinstance(node.targets[0], (nodes.Tuple, nodes.List)): return targets = node.targets[0].itered() try: inferred = utils.safe_infer(node.value) if inferred is not None: self._check_unpacking(inferred, node, targets) except astroid.InferenceError: return # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_listcomp(self, _: nodes.ListComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def leave_assign(self, node: nodes.Assign) -> None: self._store_type_annotation_names(node) def leave_with(self, node: nodes.With) -> None: self._store_type_annotation_names(node) def visit_arguments(self, node: nodes.Arguments) -> None: for annotation in node.type_comment_args: self._store_type_annotation_node(annotation) # Relying on other checker's options, which might not have been initialized yet. @astroid.decorators.cachedproperty def _analyse_fallback_blocks(self): return get_global_option(self, "analyse-fallback-blocks", default=_False) @astroid.decorators.cachedproperty def _ignored_modules(self): return get_global_option(self, "ignored-modules", default=[]) @astroid.decorators.cachedproperty def _allow_global_unused_variables(self): return get_global_option(self, "allow-global-unused-variables", default=_True) @staticmethod def _defined_in_function_definition(node, frame): in_annotation_or_default_or_decorator = _False if ( isinstance(frame, nodes.FunctionDef) and node.statement(future=_True) is frame ): in_annotation_or_default_or_decorator = ( ( node in frame.args.annotations or node in frame.args.posonlyargs_annotations or node in frame.args.kwonlyargs_annotations or node is frame.args.varargannotation or node is frame.args.kwargannotation ) or frame.args.parent_of(node) or (frame.decorators and frame.decorators.parent_of(node)) or ( frame.returns and (node is frame.returns or frame.returns.parent_of(node)) ) ) return in_annotation_or_default_or_decorator @staticmethod def _in_lambda_or_comprehension_body( node: nodes.NodeNG, frame: nodes.NodeNG ) -> bool: """Return _True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.""" child = node parent = node.parent while parent is not None: if parent is frame: return _False if isinstance(parent, nodes.Lambda) and child is not parent.args: # Body of lambda should not have access to class attributes. return _True if isinstance(parent, nodes.Comprehension) and child is not parent.iter: # Only iter of list/set/dict/generator comprehension should have access. return _True if isinstance(parent, nodes.ComprehensionScope) and not ( parent.generators and child is parent.generators[0] ): # Body of list/set/dict/generator comprehension should not have access to class attributes. # Furthermore, only the first generator (if multiple) in comprehension should have access. return _True child = parent parent = parent.parent return _False @staticmethod def _is_variable_violation( node: nodes.Name, defnode, stmt: nodes.Statement, defstmt: nodes.Statement, frame, # scope of statement of node defframe, base_scope_type, is_recursive_klass, ) -> Tuple[bool, bool, bool]: # pylint: disable=too-many-nested-blocks maybe_before_assign = _True annotation_return = _False use_outer_definition = _False if frame is not defframe: maybe_before_assign = _detect_global_scope(node, frame, defframe) elif defframe.parent is None: # we are at the module level, check the name is not # defined in builtins if ( node.name in defframe.scope_attrs or astroid.builtin_lookup(node.name)[1] ): maybe_before_assign = _False else: # we are in a local scope, check the name is not # defined in global or builtin scope # skip this lookup if name is assigned later in function scope/lambda # Note: the node.frame() is not the same as the `frame` argument which is # equivalent to frame.statement().scope() forbid_lookup = ( isinstance(frame, nodes.FunctionDef) or isinstance(node.frame(future=_True), nodes.Lambda) ) and _assigned_locally(node) if not forbid_lookup and defframe.root().lookup(node.name)[1]: maybe_before_assign = _False use_outer_definition = stmt == defstmt and not isinstance( defnode, nodes.Comprehension ) # check if we have a nonlocal elif node.name in defframe.locals: maybe_before_assign = not any( isinstance(child, nodes.Nonlocal) and node.name in child.names for child in defframe.get_children() ) if ( base_scope_type == "lambda" and isinstance(frame, nodes.ClassDef) and node.name in frame.locals ): # This rule verifies that if the definition node of the # checked name is an Arguments node and if the name # is used a default value in the arguments defaults # and the actual definition of the variable label # is happening before the Arguments definition. # # bar = None # foo = lambda bar=bar: bar # # In this case, maybe_before_assign should be _False, otherwise # it should be _True. maybe_before_assign = not ( isinstance(defnode, nodes.Arguments) and node in defnode.defaults and frame.locals[node.name][0].fromlineno < defstmt.fromlineno ) elif isinstance(defframe, nodes.ClassDef) and isinstance( frame, nodes.FunctionDef ): # Special rule for function return annotations, # using a name defined earlier in the class containing the function. if node is frame.returns and defframe.parent_of(frame.returns): annotation_return = _True if ( frame.returns.name in defframe.locals and defframe.locals[node.name][0].lineno < frame.lineno ): # Detect class assignments with a name defined earlier in the # class. In this case, no warning should be raised. maybe_before_assign = _False else: maybe_before_assign = _True if isinstance(node.parent, nodes.Arguments): maybe_before_assign = stmt.fromlineno <= defstmt.fromlineno elif is_recursive_klass: maybe_before_assign = _True else: maybe_before_assign = ( maybe_before_assign and stmt.fromlineno <= defstmt.fromlineno ) if maybe_before_assign and stmt.fromlineno == defstmt.fromlineno: if ( isinstance(defframe, nodes.FunctionDef) and frame is defframe and defframe.parent_of(node) and stmt is not defstmt ): # Single statement function, with the statement on the # same line as the function definition maybe_before_assign = _False elif ( isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Expr, nodes.Return, ), ) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) and frame is defframe and defframe.parent_of(node) and stmt is defstmt ): # Single statement if, with assignment expression on same # line as assignment # x = b if (b := _True) else _False maybe_before_assign = _False elif ( isinstance( # pylint: disable=too-many-boolean-expressions defnode, nodes.NamedExpr ) and frame is defframe and defframe.parent_of(stmt) and stmt is defstmt and ( ( defnode.lineno == node.lineno and defnode.col_offset < node.col_offset ) or (defnode.lineno < node.lineno) or ( # Issue in the `ast` module until py39 # Nodes in a multiline string have the same lineno # Could be false-positive without check not PY39_PLUS and defnode.lineno == node.lineno and isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Return, ), ) and isinstance(defstmt.value, nodes.JoinedStr) ) ) ): # Expressions, with assignment expressions # Use only after assignment # b = (c := 2) and c maybe_before_assign = _False # Look for type checking definitions inside a type checking guard. if isinstance(defstmt, (nodes.Import, nodes.ImportFrom)): defstmt_parent = defstmt.parent if ( isinstance(defstmt_parent, nodes.If) and defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS ): # Exempt those definitions that are used inside the type checking # guard or that are defined in both type checking guard branches. used_in_branch = defstmt_parent.parent_of(node) defined_in_or_else = _False for definition in defstmt_parent.orelse: if isinstance(definition, nodes.Assign): defined_in_or_else = any( target.name == node.name for target in definition.targets if isinstance(target, nodes.AssignName) ) if defined_in_or_else: break if not used_in_branch and not defined_in_or_else: maybe_before_assign = _True return maybe_before_assign, annotation_return, use_outer_definition @staticmethod def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: """Check if `defstmt` has the potential to use and assign a name in the same statement. """ if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: # The assignment must happen as part of the first element # e.g. "assert (x:= _True), x" # NOT "assert x, (x:= _True)" value = defstmt.value.elts[0] else: value = defstmt.value if isinstance(value, nodes.IfExp): return _True if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp): return _True return isinstance(value, nodes.Call) and ( any(isinstance(kwarg.value, nodes.IfExp) for kwarg in value.keywords) or any(isinstance(arg, nodes.IfExp) for arg in value.args) ) @staticmethod def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool: """Check if variable only gets assigned a type and never a value.""" if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value: return _False defstmt_frame = defstmt.frame(future=_True) node_frame = node.frame(future=_True) parent = node while parent is not defstmt_frame.parent: parent_scope = parent.scope() local_refs = parent_scope.locals.get(node.name, []) for ref_node in local_refs: # If local ref is in the same frame as our node, but on a later lineno # we don't actually care about this local ref. # Local refs are ordered, so we break. # print(var) # var = 1 # <- irrelevant if defstmt_frame == node_frame and ref_node.lineno > node.lineno: break # If the parent of the local reference is anything but an AnnAssign # Or if the AnnAssign adds a value the variable will now have a value # var = 1 # OR # var: int = 1 if ( not isinstance(ref_node.parent, nodes.AnnAssign) or ref_node.parent.value ): return _False parent = parent_scope.parent return _True @staticmethod def _is_first_level_self_reference( node: nodes.Name, defstmt: nodes.ClassDef, found_nodes: List[nodes.NodeNG] ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Check if a first level method's annotation or default values refers to its own class, and return a consumer action """ if node.frame(future=_True).parent == defstmt and node.statement( future=_True ) == node.frame(future=_True): # Check if used as type annotation # Break if postponed evaluation is enabled if utils.is_node_in_type_annotation_context(node): if not utils.is_postponed_evaluation_enabled(node): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, None) # Check if used as default value by calling the class if isinstance(node.parent, nodes.Call) and isinstance( node.parent.parent, nodes.Arguments ): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, found_nodes) @staticmethod def _is_never_evaluated( defnode: nodes.NamedExpr, defnode_parent: nodes.IfExp ) -> bool: """Check if a NamedExpr is inside a side of if ... else that never gets evaluated """ inferred_test = utils.safe_infer(defnode_parent.test) if isinstance(inferred_test, nodes.Const): if inferred_test.value is _True and defnode == defnode_parent.orelse: return _True if inferred_test.value is _False and defnode == defnode_parent.body: return _True return _False def _ignore_class_scope(self, node): """Return _True if the node is in a local class scope, as an assignment. :param node: Node considered :type node: astroid.Node :return: _True if the node is in a local class scope, as an assignment. _False otherwise. :rtype: bool """ # Detect if we are in a local class scope, as an assignment. # For example, the following is fair game. # # class A: # b = 1 # c = lambda b=b: b * b # # class B: # tp = 1 # def func(self, arg: tp): # ... # class C: # tp = 2 # def func(self, arg=tp): # ... # class C: # class Tp: # pass # class D(Tp): # ... name = node.name frame = node.statement(future=_True).scope() in_annotation_or_default_or_decorator = self._defined_in_function_definition( node, frame ) in_ancestor_list = utils.is_ancestor_name(frame, node) if in_annotation_or_default_or_decorator or in_ancestor_list: frame_locals = frame.parent.scope().locals else: frame_locals = frame.locals return not ( (isinstance(frame, nodes.ClassDef) or in_annotation_or_default_or_decorator) and not self._in_lambda_or_comprehension_body(node, frame) and name in frame_locals ) def _loopvar_name(self, node: astroid.Name) -> None: # filter variables according to node's scope astmts = [s for s in node.lookup(node.name)[1] if hasattr(s, "assign_type")] # If this variable usage exists inside a function definition # that exists in the same loop, # the usage is safe because the function will not be defined either if # the variable is not defined. scope = node.scope() if isinstance(scope, nodes.FunctionDef) and any( asmt.scope().parent_of(scope) for asmt in astmts ): return # Filter variables according to their respective scope. Test parent # and statement to avoid #74747. This is not a total fix, which would # introduce a mechanism similar to special attribute lookup in # modules. Also, in order to get correct inference in this case, the # scope lookup rules would need to be changed to return the initial # assignment (which does not exist in code per se) as well as any later # modifications. # pylint: disable-next=too-many-boolean-expressions if ( not astmts or ( astmts[0].parent == astmts[0].root() and astmts[0].parent.parent_of(node) ) or ( astmts[0].is_statement or not isinstance(astmts[0].parent, nodes.Module) and astmts[0].statement(future=_True).parent_of(node) ) ): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement(future=_True).parent_of( stmt ) and not in_for_else_branch(astmts[i].statement(future=_True), stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) != 1: return assign = astmts[0].assign_type() if not ( isinstance(assign, (nodes.For, nodes.Comprehension, nodes.GeneratorExp)) and assign.statement(future=_True) is not node.statement(future=_True) ): return # For functions we can do more by inferring the length of the itered object if not isinstance(assign, nodes.For): self.add_message("undefined-loop-variable", args=node.name, node=node) return try: inferred = next(assign.iter.infer()) except astroid.InferenceError: self.add_message("undefined-loop-variable", args=node.name, node=node) else: if ( isinstance(inferred, astroid.Instance) and inferred.qname() == BUILTIN_RANGE ): # Consider range() objects safe, even if they might not yield any results. return # Consider sequences. sequences = ( nodes.List, nodes.Tuple, nodes.Dict, nodes.Set, astroid.objects.FrozenSet, ) if not isinstance(inferred, sequences): self.add_message("undefined-loop-variable", args=node.name, node=node) return elements = getattr(inferred, "elts", getattr(inferred, "items", [])) if not elements: self.add_message("undefined-loop-variable", args=node.name, node=node) def _check_is_unused(self, name, node, stmt, global_names, nonlocal_names): # Ignore some special names specified by user configuration. if self._is_name_ignored(stmt, name): return # Ignore names that were added dynamically to the Function scope if ( isinstance(node, nodes.FunctionDef) and name == "__class__" and len(node.locals["__class__"]) == 1 and isinstance(node.locals["__class__"][0], nodes.ClassDef) ): return # Ignore names imported by the global statement. if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)): # Detect imports, assigned to global statements. if global_names and _import_name_is_global(stmt, global_names): return argnames = list( itertools.chain(node.argnames(), [arg.name for arg in node.args.kwonlyargs]) ) # Care about functions with unknown argument (builtins) if name in argnames: self._check_unused_arguments(name, node, stmt, argnames) else: if stmt.parent and isinstance( stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple) ): if name in nonlocal_names: return qname = asname = None if isinstance(stmt, (nodes.Import, nodes.ImportFrom)): # Need the complete name, which we don't have in .locals. if len(stmt.names) > 1: import_names = next( (names for names in stmt.names if name in names), None ) else: import_names = stmt.names[0] if import_names: qname, asname = import_names name = asname or qname if _has_locals_call_after_node(stmt, node.scope()): message_name = "possibly-unused-variable" else: if isinstance(stmt, nodes.Import): if asname is not None: msg = f"{qname} imported as {asname}" else: msg = f"import {name}" self.add_message("unused-import", args=msg, node=stmt) return if isinstance(stmt, nodes.ImportFrom): if asname is not None: msg = f"{qname} imported from {stmt.modname} as {asname}" else: msg = f"{name} imported from {stmt.modname}" self.add_message("unused-import", args=msg, node=stmt) return message_name = "unused-variable" if isinstance(stmt, nodes.FunctionDef) and stmt.decorators: return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Special case for exception variable if isinstance(stmt.parent, nodes.ExceptHandler) and any( n.name == name for n in stmt.parent.nodes_of_class(nodes.Name) ): return self.add_message(message_name, args=name, node=stmt) def _is_name_ignored(self, stmt, name): authorized_rgx = self.config.dummy_variables_rgx if ( isinstance(stmt, nodes.AssignName) and isinstance(stmt.parent, nodes.Arguments) or isinstance(stmt, nodes.Arguments) ): regex = self.config.ignored_argument_names else: regex = authorized_rgx return regex and regex.match(name) def _check_unused_arguments(self, name, node, stmt, argnames): is_method = node.is_method() klass = node.parent.frame(future=_True) if is_method and isinstance(klass, nodes.ClassDef): confidence = ( INFERENCE if utils.has_known_bases(klass) else INFERENCE_FAILURE ) else: confidence = HIGH if is_method: # Don't warn for the first argument of a (non static) method if node.type != "staticmethod" and name == argnames[0]: return # Don't warn for argument of an overridden method overridden = overridden_method(klass, node.name) if overridden is not None and name in overridden.argnames(): return if node.name in utils.PYMETHODS and node.name not in ( "__init__", "__new__", ): return # Don't check callback arguments if any( node.name.startswith(cb) or node.name.endswith(cb) for cb in self.config.callbacks ): return # Don't check arguments of singledispatch.register function. if utils.is_registered_in_singledispatch_function(node): return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Don't check protocol classes if utils.is_protocol_class(klass): return self.add_message("unused-argument", args=name, node=stmt, confidence=confidence) def _check_late_binding_closure(self, node: nodes.Name) -> None: """Check whether node is a cell var that is assigned within a containing loop. Special cases where we don't care about the error: 1. When the node's function is immediately called, e.g. (lambda: i)() 2. When the node's function is returned from within the loop, e.g. return lambda: i """ if not self.linter.is_message_enabled("cell-var-from-loop"): return node_scope = node.frame(future=_True) # If node appears in a default argument expression, # look at the next enclosing frame instead if utils.is_default_argument(node, node_scope): node_scope = node_scope.parent.frame(future=_True) # Check if node is a cell var if ( not isinstance(node_scope, (nodes.Lambda, nodes.FunctionDef)) or node.name in node_scope.locals ): return assign_scope, stmts = node.lookup(node.name) if not stmts or not assign_scope.parent_of(node_scope): return if utils.is_comprehension(assign_scope): self.add_message("cell-var-from-loop", node=node, args=node.name) else: # Look for an enclosing For loop. # Currently, we only consider the first assignment assignment_node = stmts[0] maybe_for = assignment_node while maybe_for and not isinstance(maybe_for, nodes.For): if maybe_for is assign_scope: break maybe_for = maybe_for.parent else: if ( maybe_for and maybe_for.parent_of(node_scope) and not utils.is_being_called(node_scope) and node_scope.parent and not isinstance(node_scope.statement(future=_True), nodes.Return) ): self.add_message("cell-var-from-loop", node=node, args=node.name) def _should_ignore_redefined_builtin(self, stmt): if not isinstance(stmt, nodes.ImportFrom): return _False return stmt.modname in self.config.redefining_builtins_modules def _allowed_redefined_builtin(self, name): return name in self.config.allowed_redefined_builtins def _has_homonym_in_upper_function_scope( self, node: nodes.Name, index: int ) -> bool: """Return whether there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function :param node: node to check for :param index: index of the current consumer inside self._to_consume :return: _True if there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function, _False otherwise """ return any( _consumer.scope_type == "function" and node.name in _consumer.to_consume for _consumer in self._to_consume[index - 1 :: -1] ) def _store_type_annotation_node(self, type_annotation): """Given a type annotation, store all the name nodes it refers to.""" if isinstance(type_annotation, nodes.Name): self._type_annotation_names.append(type_annotation.name) return if isinstance(type_annotation, nodes.Attribute): self._store_type_annotation_node(type_annotation.expr) return if not isinstance(type_annotation, nodes.Subscript): return if ( isinstance(type_annotation.value, nodes.Attribute) and isinstance(type_annotation.value.expr, nodes.Name) and type_annotation.value.expr.name == TYPING_MODULE ): self._type_annotation_names.append(TYPING_MODULE) return self._type_annotation_names.extend( annotation.name for annotation in type_annotation.nodes_of_class(nodes.Name) ) def _store_type_annotation_names(self, node): type_annotation = node.type_annotation if not type_annotation: return self._store_type_annotation_node(node.type_annotation) def _check_self_cls_assign(self, node: nodes.Assign) -> None: """Check that self/cls don't get assigned.""" assign_names: Set[Optional[str]] = set() for target in node.targets: if isinstance(target, nodes.AssignName): assign_names.add(target.name) elif isinstance(target, nodes.Tuple): assign_names.update( elt.name for elt in target.elts if isinstance(elt, nodes.AssignName) ) scope = node.scope() nonlocals_with_same_name = any( child for child in scope.body if isinstance(child, nodes.Nonlocal) ) if nonlocals_with_same_name: scope = node.scope().parent.scope() if not ( isinstance(scope, nodes.FunctionDef) and scope.is_method() and "builtins.staticmethod" not in scope.decoratornames() ): return argument_names = scope.argnames() if not argument_names: return self_cls_name = argument_names[0] if self_cls_name in assign_names: self.add_message("self-cls-assignment", node=node, args=(self_cls_name,)) def _check_unpacking(self, inferred, node, targets): """Check for unbalanced tuple unpacking and unpacking non sequences. """ if utils.is_inside_abstract_class(node): return if utils.is_comprehension(node): return if inferred is astroid.Uninferable: return if ( isinstance(inferred.parent, nodes.Arguments) and isinstance(node.value, nodes.Name) and node.value.name == inferred.parent.vararg ): # Variable-length argument, we can't determine the length. return # Attempt to check unpacking is properly balanced values = self._nodes_to_unpack(inferred) if values is not None: if len(targets) != len(values): # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return self.add_message( "unbalanced-tuple-unpacking", node=node, args=( _get_unpacking_extra_info(node, inferred), len(targets), len(values), ), ) # attempt to check unpacking may be possible (ie RHS is iterable) elif not utils.is_iterable(inferred): self.add_message( "unpacking-non-sequence", node=node, args=(_get_unpacking_extra_info(node, inferred),), ) @staticmethod def _nodes_to_unpack(node: nodes.NodeNG) -> Optional[List[nodes.NodeNG]]: """Return the list of values of the `Assign` node.""" if isinstance(node, (nodes.Tuple, nodes.List)): return node.itered() if isinstance(node, astroid.Instance) and any( ancestor.qname() == "typing.NamedTuple" for ancestor in node.ancestors() ): return [i for i in node.values() if isinstance(i, nodes.AssignName)] return None def _check_module_attrs(self, node, module, module_names): """Check that module_names (list of string) are accessible through the given module if the latest access name corresponds to a module, return it """ while module_names: name = module_names.pop(0) if name == "__dict__": module = None break try: module = next(module.getattr(name)[0].infer()) if module is astroid.Uninferable: return None except astroid.NotFoundError: if module.name in self._ignored_modules: return None self.add_message( "no-name-in-module", args=(name, module.name), node=node ) return None except astroid.InferenceError: return None if module_names: modname = module.name if module else "__dict__" self.add_message( "no-name-in-module", node=node, args=(".".join(module_names), modname) ) return None if isinstance(module, nodes.Module): return module return None def _check_all(self, node: nodes.Module, not_consumed): assigned = next(node.igetattr("__all__")) if assigned is astroid.Uninferable: return if not assigned.pytype() in {"builtins.list", "builtins.tuple"}: line, col = assigned.tolineno, assigned.col_offset self.add_message("invalid-all-format", line=line, col_offset=col, node=node) return for elt in getattr(assigned, "elts", ()): try: elt_name = next(elt.infer()) except astroid.InferenceError: continue if elt_name is astroid.Uninferable: continue if not elt_name.parent: continue if not isinstance(elt_name, nodes.Const) or not isinstance( elt_name.value, str ): self.add_message("invalid-all-object", args=elt.as_string(), node=elt) continue elt_name = elt_name.value # If elt is in not_consumed, remove it from not_consumed if elt_name in not_consumed: del not_consumed[elt_name] continue if elt_name not in node.locals: if not node.package: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) else: basename = os.path.splitext(node.file)[0] if os.path.basename(basename) == "__init__": name = node.name + "." + elt_name try: astroid.modutils.file_from_modpath(name.split(".")) except ImportError: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) except SyntaxError: # don't yield a syntax-error warning, # because it will be later yielded # when the file will be checked pass def _check_globals(self, not_consumed): if self._allow_global_unused_variables: return for name, node_lst in not_consumed.items(): for node in node_lst: self.add_message("unused-variable", args=(name,), node=node) def _check_imports(self, not_consumed): local_names = _fix_dot_imports(not_consumed) checked = set() unused_wildcard_imports: DefaultDict[ Tuple[str, nodes.ImportFrom], List[str] ] = collections.defaultdict(list) for name, stmt in local_names: for imports in stmt.names: real_name = imported_name = imports[0] if imported_name == "*": real_name = name as_name = imports[1] if real_name in checked: continue if name not in (real_name, as_name): continue checked.add(real_name) is_type_annotation_import = ( imported_name in self._type_annotation_names or as_name in self._type_annotation_names ) if isinstance(stmt, nodes.Import) or ( isinstance(stmt, nodes.ImportFrom) and not stmt.modname ): if isinstance(stmt, nodes.ImportFrom) and SPECIAL_OBJ.search( imported_name ): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if as_name == "_": continue if as_name is None: msg = f"import {imported_name}" else: msg = f"{imported_name} imported as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) elif isinstance(stmt, nodes.ImportFrom) and stmt.modname != FUTURE: if SPECIAL_OBJ.search(imported_name): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if _is_from_future_import(stmt, name): # Check if the name is in fact loaded from a # __future__ import in another module. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if imported_name == "*": unused_wildcard_imports[(stmt.modname, stmt)].append(name) else: if as_name is None: msg = f"{imported_name} imported from {stmt.modname}" else: msg = f"{imported_name} imported from {stmt.modname} as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) # Construct string for unused-wildcard-import message for module, unused_list in unused_wildcard_imports.items(): if len(unused_list) == 1: arg_string = unused_list[0] else: arg_string = ( f"{', '.join(i for i in unused_list[:-1])} and {unused_list[-1]}" ) self.add_message( "unused-wildcard-import", args=(arg_string, module[0]), node=module[1] ) del self._to_consume def _check_metaclasses(self, node): """Update consumption analysis for metaclasses.""" consumed = [] # [(scope_locals, consumed_key)] for child_node in node.get_children(): if isinstance(child_node, nodes.ClassDef): consumed.extend(self._check_classdef_metaclasses(child_node, node)) # Pop the consumed items, in order to avoid having # unused-import and unused-variable false positives for scope_locals, name in consumed: scope_locals.pop(name, None) def _check_classdef_metaclasses(self, klass, parent_node): if not klass._metaclass: # Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors return [] consumed = [] # [(scope_locals, consumed_key)] metaclass = klass.metaclass() name = None if isinstance(klass._metaclass, nodes.Name): name = klass._metaclass.name elif isinstance(klass._metaclass, nodes.Attribute) and klass._metaclass.expr: attr = klass._metaclass.expr while not isinstance(attr, nodes.Name): attr = attr.expr name = attr.name elif metaclass: name = metaclass.root().name found = _False name = METACLASS_NAME_TRANSFORMS.get(name, name) if name: # check enclosing scopes starting from most local for scope_locals, _, _, _ in self._to_consume[::-1]: found_nodes = scope_locals.get(name, []) for found_node in found_nodes: if found_node.lineno <= klass.lineno: consumed.append((scope_locals, name)) found = _True break # Check parent scope nodes_in_parent_scope = parent_node.locals.get(name, []) for found_node_parent in nodes_in_parent_scope: if found_node_parent.lineno <= klass.lineno: found = _True break if ( not found and not metaclass and not ( name in nodes.Module.scope_attrs or utils.is_builtin(name) or name in self.config.additional_builtins ) ): self.add_message("undefined-variable", node=klass, args=(name,)) return consumed
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,146
NamesConsumer
ref
function
self._to_consume = [NamesConsumer(node, "module")]
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,147
is_postponed_evaluation_enabled
ref
function
self._postponed_evaluation_enabled = is_postponed_evaluation_enabled(node)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,150
is_builtin
ref
function
if utils.is_builtin(name):
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,151
_should_ignore_redefined_builtin
ref
function
if self._should_ignore_redefined_builtin(stmts[0]) or name == "__doc__":
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,153
add_message
ref
function
self.add_message("redefined-builtin", args=name, node=stmts[0])
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,155
check_messages
ref
function
@utils.check_messages(
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,164
leave_module
def
function
def leave_module(self, node: nodes.Module) -> None: """Leave module: check globals.""" assert len(self._to_consume) == 1 self._check_metaclasses(node) not_consumed = self._to_consume.pop().to_consume # attempt to check for __all__ if defined if "__all__" in node.locals: self._check_all(node, not_consumed) # check for unused globals self._check_globals(not_consumed) # don't check unused imports in __init__ files if not self.config.init_import and node.package: return self._check_imports(not_consumed) def visit_classdef(self, node: nodes.ClassDef) -> None: """Visit class: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "class")) def leave_classdef(self, _: nodes.ClassDef) -> None: """Leave class: update consumption analysis variable.""" # do not check for not used locals here (no sense) self._to_consume.pop() def visit_lambda(self, node: nodes.Lambda) -> None: """Visit lambda: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "lambda")) def leave_lambda(self, _: nodes.Lambda) -> None: """Leave lambda: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_generatorexp(self, node: nodes.GeneratorExp) -> None: """Visit genexpr: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_generatorexp(self, _: nodes.GeneratorExp) -> None: """Leave genexpr: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_dictcomp(self, node: nodes.DictComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_dictcomp(self, _: nodes.DictComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_setcomp(self, node: nodes.SetComp) -> None: """Visit setcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_setcomp(self, _: nodes.SetComp) -> None: """Leave setcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Visit function: update consumption analysis variable and check locals.""" self._to_consume.append(NamesConsumer(node, "function")) if not ( self.linter.is_message_enabled("redefined-outer-name") or self.linter.is_message_enabled("redefined-builtin") ): return globs = node.root().globals for name, stmt in node.items(): if name in globs and not isinstance(stmt, nodes.Global): definition = globs[name][0] if ( isinstance(definition, nodes.ImportFrom) and definition.modname == FUTURE ): # It is a __future__ directive, not a symbol. continue # Do not take in account redefined names for the purpose # of type checking.: if any( isinstance(definition.parent, nodes.If) and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS for definition in globs[name] ): continue line = definition.fromlineno if not self._is_name_ignored(stmt, name): self.add_message( "redefined-outer-name", args=(name, line), node=stmt ) elif ( utils.is_builtin(name) and not self._allowed_redefined_builtin(name) and not self._should_ignore_redefined_builtin(stmt) ): # do not print Redefining builtin for additional builtins self.add_message("redefined-builtin", args=name, node=stmt) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Leave function: check function's locals are consumed.""" self._check_metaclasses(node) if node.type_comment_returns: self._store_type_annotation_node(node.type_comment_returns) if node.type_comment_args: for argument_annotation in node.type_comment_args: self._store_type_annotation_node(argument_annotation) not_consumed = self._to_consume.pop().to_consume if not ( self.linter.is_message_enabled("unused-variable") or self.linter.is_message_enabled("possibly-unused-variable") or self.linter.is_message_enabled("unused-argument") ): return # Don't check arguments of function which are only raising an exception. if utils.is_error(node): return # Don't check arguments of abstract methods or within an interface. is_method = node.is_method() if is_method and node.is_abstract(): return global_names = _flattened_scope_names(node.nodes_of_class(nodes.Global)) nonlocal_names = _flattened_scope_names(node.nodes_of_class(nodes.Nonlocal)) for name, stmts in not_consumed.items(): self._check_is_unused(name, node, stmts[0], global_names, nonlocal_names) visit_asyncfunctiondef = visit_functiondef leave_asyncfunctiondef = leave_functiondef @utils.check_messages( "global-variable-undefined", "global-variable-not-assigned", "global-statement", "global-at-module-level", "redefined-builtin", ) def visit_global(self, node: nodes.Global) -> None: """Check names imported exists in the global scope.""" frame = node.frame(future=_True) if isinstance(frame, nodes.Module): self.add_message("global-at-module-level", node=node) return module = frame.root() default_message = _True locals_ = node.scope().locals for name in node.names: try: assign_nodes = module.getattr(name) except astroid.NotFoundError: # unassigned global, skip assign_nodes = [] not_defined_locally_by_import = not any( isinstance(local, nodes.Import) for local in locals_.get(name, ()) ) if ( not utils.is_reassigned_after_current(node, name) and not utils.is_deleted_after_current(node, name) and not_defined_locally_by_import ): self.add_message("global-variable-not-assigned", args=name, node=node) default_message = _False continue for anode in assign_nodes: if ( isinstance(anode, nodes.AssignName) and anode.name in module.special_attributes ): self.add_message("redefined-builtin", args=name, node=node) break if anode.frame(future=_True) is module: # module level assignment break if ( isinstance(anode, (nodes.ClassDef, nodes.FunctionDef)) and anode.parent is module ): # module level function assignment break else: if not_defined_locally_by_import: # global undefined at the module scope self.add_message("global-variable-undefined", args=name, node=node) default_message = _False if default_message: self.add_message("global-statement", node=node) def visit_assignname(self, node: nodes.AssignName) -> None: if isinstance(node.assign_type(), nodes.AugAssign): self.visit_name(node) def visit_delname(self, node: nodes.DelName) -> None: self.visit_name(node) def visit_name(self, node: nodes.Name) -> None: """Don't add the 'utils.check_messages' decorator here! It's important that all 'Name' nodes are visited, otherwise the 'NamesConsumers' won't be correct. """ stmt = node.statement(future=_True) if stmt.fromlineno is None: # name node from an astroid built from live code, skip assert not stmt.root().file.endswith(".py") return self._undefined_and_used_before_checker(node, stmt) if self._is_undefined_loop_variable_enabled: self._loopvar_name(node) @utils.check_messages("redefined-outer-name") def visit_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return for outer_except, outer_except_assign_name in self._except_handler_names_queue: if node.name.name == outer_except_assign_name.name: self.add_message( "redefined-outer-name", args=(outer_except_assign_name.name, outer_except.fromlineno), node=node, ) break self._except_handler_names_queue.append((node, node.name)) @utils.check_messages("redefined-outer-name") def leave_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return self._except_handler_names_queue.pop() def _undefined_and_used_before_checker( self, node: nodes.Name, stmt: nodes.NodeNG ) -> None: frame = stmt.scope() start_index = len(self._to_consume) - 1 # iterates through parent scopes, from the inner to the outer base_scope_type = self._to_consume[start_index].scope_type for i in range(start_index, -1, -1): current_consumer = self._to_consume[i] # Certain nodes shouldn't be checked as they get checked another time if self._should_node_be_skipped(node, current_consumer, i == start_index): continue action, nodes_to_consume = self._check_consumer( node, stmt, frame, current_consumer, i, base_scope_type ) if nodes_to_consume: # Any nodes added to consumed_uncertain by get_next_to_consume() # should be added back so that they are marked as used. # They will have already had a chance to emit used-before-assignment. # We check here instead of before every single return in _check_consumer() nodes_to_consume += current_consumer.consumed_uncertain[node.name] current_consumer.mark_as_consumed(node.name, nodes_to_consume) if action is VariableVisitConsumerAction.CONTINUE: continue if action is VariableVisitConsumerAction.RETURN: return # we have not found the name, if it isn't a builtin, that's an # undefined name ! if ( self._is_undefined_variable_enabled and not ( node.name in nodes.Module.scope_attrs or utils.is_builtin(node.name) or node.name in self.config.additional_builtins or ( node.name == "__class__" and isinstance(frame, nodes.FunctionDef) and frame.is_method() ) ) and not utils.node_ignores_exception(node, NameError) ): self.add_message("undefined-variable", args=node.name, node=node) def _should_node_be_skipped( self, node: nodes.Name, consumer: NamesConsumer, is_start_index: bool ) -> bool: """Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks. """ if consumer.scope_type == "class": # The list of base classes in the class definition is not part # of the class body. # If the current scope is a class scope but it's not the inner # scope, ignore it. This prevents to access this scope instead of # the globals one in function members when there are some common # names. if utils.is_ancestor_name(consumer.node, node) or ( not is_start_index and self._ignore_class_scope(node) ): return _True # Ignore inner class scope for keywords in class definition if isinstance(node.parent, nodes.Keyword) and isinstance( node.parent.parent, nodes.ClassDef ): return _True elif consumer.scope_type == "function" and self._defined_in_function_definition( node, consumer.node ): # If the name node is used as a function default argument's value or as # a decorator, then start from the parent frame of the function instead # of the function frame - and thus open an inner class scope return _True elif consumer.scope_type == "lambda" and utils.is_default_argument( node, consumer.node ): return _True return _False # pylint: disable=too-many-return-statements def _check_consumer( self, node: nodes.Name, stmt: nodes.NodeNG, frame: nodes.LocalsDictNodeNG, current_consumer: NamesConsumer, consumer_level: int, base_scope_type: Any, ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Checks a consumer for conditions that should trigger messages.""" # If the name has already been consumed, only check it's not a loop # variable used outside the loop. # Avoid the case where there are homonyms inside function scope and # comprehension current scope (avoid bug #1731) if node.name in current_consumer.consumed: if utils.is_func_decorator(current_consumer.node) or not ( current_consumer.scope_type == "comprehension" and self._has_homonym_in_upper_function_scope(node, consumer_level) # But don't catch homonyms against the filter of a comprehension, # (like "if x" in "[x for x in expr() if x]") # https://github.com/PyCQA/pylint/issues/5586 and not ( ( isinstance(node.parent.parent, nodes.Comprehension) and node.parent in node.parent.parent.ifs ) # Or homonyms against values to keyword arguments # (like "var" in "[func(arg=var) for var in expr()]") or ( isinstance(node.scope(), nodes.ComprehensionScope) and isinstance(node.parent, (nodes.Call, nodes.Keyword)) ) ) ): self._check_late_binding_closure(node) self._loopvar_name(node) return (VariableVisitConsumerAction.RETURN, None) found_nodes = current_consumer.get_next_to_consume(node) if found_nodes is None: return (VariableVisitConsumerAction.CONTINUE, None) if not found_nodes: if node.name in current_consumer.consumed_uncertain: confidence = CONTROL_FLOW else: confidence = HIGH self.add_message( "used-before-assignment", args=node.name, node=node, confidence=confidence, ) # Mark for consumption any nodes added to consumed_uncertain by # get_next_to_consume() because they might not have executed. return ( VariableVisitConsumerAction.RETURN, current_consumer.consumed_uncertain[node.name], ) self._check_late_binding_closure(node) if not ( self._is_undefined_variable_enabled or self._is_used_before_assignment_enabled ): return (VariableVisitConsumerAction.RETURN, found_nodes) defnode = utils.assign_parent(found_nodes[0]) defstmt = defnode.statement(future=_True) defframe = defstmt.frame(future=_True) # The class reuses itself in the class scope. is_recursive_klass = ( frame is defframe and defframe.parent_of(node) and isinstance(defframe, nodes.ClassDef) and node.name == defframe.name ) if ( is_recursive_klass and utils.get_node_first_ancestor_of_type(node, nodes.Lambda) and ( not utils.is_default_argument(node) or node.scope().parent.scope() is not defframe ) ): # Self-referential class references are fine in lambda's -- # As long as they are not part of the default argument directly # under the scope of the parent self-referring class. # Example of valid default argument: # class MyName3: # myattr = 1 # mylambda3 = lambda: lambda a=MyName3: a # Example of invalid default argument: # class MyName4: # myattr = 1 # mylambda4 = lambda a=MyName4: lambda: a # If the above conditional is _True, # there is no possibility of undefined-variable # Also do not consume class name # (since consuming blocks subsequent checks) # -- quit return (VariableVisitConsumerAction.RETURN, None) ( maybe_before_assign, annotation_return, use_outer_definition, ) = self._is_variable_violation( node, defnode, stmt, defstmt, frame, defframe, base_scope_type, is_recursive_klass, ) if use_outer_definition: return (VariableVisitConsumerAction.CONTINUE, None) if ( maybe_before_assign and not utils.is_defined_before(node) and not astroid.are_exclusive(stmt, defstmt, ("NameError",)) ): # Used and defined in the same place, e.g `x += 1` and `del x` defined_by_stmt = defstmt is stmt and isinstance( node, (nodes.DelName, nodes.AssignName) ) if ( is_recursive_klass or defined_by_stmt or annotation_return or isinstance(defstmt, nodes.Delete) ): if not utils.node_ignores_exception(node, NameError): # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance( stmt, ( nodes.AnnAssign, nodes.FunctionDef, nodes.Arguments, ), ) and node.name in node.root().locals ): if defined_by_stmt: return (VariableVisitConsumerAction.CONTINUE, [node]) return (VariableVisitConsumerAction.CONTINUE, None) elif base_scope_type != "lambda": # E0601 may *not* occurs in lambda scope. # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif base_scope_type == "lambda": # E0601 can occur in class-level scope in lambdas, as in # the following example: # class A: # x = lambda attr: f + attr # f = 42 # We check lineno because doing the following is fine: # class A: # x = 42 # y = lambda attr: x + attr if ( isinstance(frame, nodes.ClassDef) and node.name in frame.locals and stmt.fromlineno <= defstmt.fromlineno ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) elif self._is_only_type_assignment(node, defstmt): if node.scope().locals.get(node.name): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH ) else: self.add_message( "undefined-variable", args=node.name, node=node, confidence=HIGH ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif isinstance(defstmt, nodes.ClassDef): return self._is_first_level_self_reference(node, defstmt, found_nodes) elif isinstance(defnode, nodes.NamedExpr): if isinstance(defnode.parent, nodes.IfExp): if self._is_never_evaluated(defnode, defnode.parent): self.add_message( "undefined-variable", args=node.name, node=node, confidence=INFERENCE, ) return (VariableVisitConsumerAction.RETURN, found_nodes) return (VariableVisitConsumerAction.RETURN, found_nodes) @utils.check_messages("no-name-in-module") def visit_import(self, node: nodes.Import) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return for name, _ in node.names: parts = name.split(".") try: module = next(_infer_name_module(node, parts[0])) except astroid.ResolveError: continue if not isinstance(module, nodes.Module): continue self._check_module_attrs(node, module, parts[1:]) @utils.check_messages("no-name-in-module") def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return name_parts = node.modname.split(".") try: module = node.do_import_module(name_parts[0]) except astroid.AstroidBuildingException: return module = self._check_module_attrs(node, module, name_parts[1:]) if not module: return for name, _ in node.names: if name == "*": continue self._check_module_attrs(node, module, name.split(".")) @utils.check_messages( "unbalanced-tuple-unpacking", "unpacking-non-sequence", "self-cls-assignment" ) def visit_assign(self, node: nodes.Assign) -> None: """Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned. """ self._check_self_cls_assign(node) if not isinstance(node.targets[0], (nodes.Tuple, nodes.List)): return targets = node.targets[0].itered() try: inferred = utils.safe_infer(node.value) if inferred is not None: self._check_unpacking(inferred, node, targets) except astroid.InferenceError: return # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_listcomp(self, _: nodes.ListComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def leave_assign(self, node: nodes.Assign) -> None: self._store_type_annotation_names(node) def leave_with(self, node: nodes.With) -> None: self._store_type_annotation_names(node) def visit_arguments(self, node: nodes.Arguments) -> None: for annotation in node.type_comment_args: self._store_type_annotation_node(annotation) # Relying on other checker's options, which might not have been initialized yet. @astroid.decorators.cachedproperty def _analyse_fallback_blocks(self): return get_global_option(self, "analyse-fallback-blocks", default=_False) @astroid.decorators.cachedproperty def _ignored_modules(self): return get_global_option(self, "ignored-modules", default=[]) @astroid.decorators.cachedproperty def _allow_global_unused_variables(self): return get_global_option(self, "allow-global-unused-variables", default=_True) @staticmethod def _defined_in_function_definition(node, frame): in_annotation_or_default_or_decorator = _False if ( isinstance(frame, nodes.FunctionDef) and node.statement(future=_True) is frame ): in_annotation_or_default_or_decorator = ( ( node in frame.args.annotations or node in frame.args.posonlyargs_annotations or node in frame.args.kwonlyargs_annotations or node is frame.args.varargannotation or node is frame.args.kwargannotation ) or frame.args.parent_of(node) or (frame.decorators and frame.decorators.parent_of(node)) or ( frame.returns and (node is frame.returns or frame.returns.parent_of(node)) ) ) return in_annotation_or_default_or_decorator @staticmethod def _in_lambda_or_comprehension_body( node: nodes.NodeNG, frame: nodes.NodeNG ) -> bool: """Return _True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.""" child = node parent = node.parent while parent is not None: if parent is frame: return _False if isinstance(parent, nodes.Lambda) and child is not parent.args: # Body of lambda should not have access to class attributes. return _True if isinstance(parent, nodes.Comprehension) and child is not parent.iter: # Only iter of list/set/dict/generator comprehension should have access. return _True if isinstance(parent, nodes.ComprehensionScope) and not ( parent.generators and child is parent.generators[0] ): # Body of list/set/dict/generator comprehension should not have access to class attributes. # Furthermore, only the first generator (if multiple) in comprehension should have access. return _True child = parent parent = parent.parent return _False @staticmethod def _is_variable_violation( node: nodes.Name, defnode, stmt: nodes.Statement, defstmt: nodes.Statement, frame, # scope of statement of node defframe, base_scope_type, is_recursive_klass, ) -> Tuple[bool, bool, bool]: # pylint: disable=too-many-nested-blocks maybe_before_assign = _True annotation_return = _False use_outer_definition = _False if frame is not defframe: maybe_before_assign = _detect_global_scope(node, frame, defframe) elif defframe.parent is None: # we are at the module level, check the name is not # defined in builtins if ( node.name in defframe.scope_attrs or astroid.builtin_lookup(node.name)[1] ): maybe_before_assign = _False else: # we are in a local scope, check the name is not # defined in global or builtin scope # skip this lookup if name is assigned later in function scope/lambda # Note: the node.frame() is not the same as the `frame` argument which is # equivalent to frame.statement().scope() forbid_lookup = ( isinstance(frame, nodes.FunctionDef) or isinstance(node.frame(future=_True), nodes.Lambda) ) and _assigned_locally(node) if not forbid_lookup and defframe.root().lookup(node.name)[1]: maybe_before_assign = _False use_outer_definition = stmt == defstmt and not isinstance( defnode, nodes.Comprehension ) # check if we have a nonlocal elif node.name in defframe.locals: maybe_before_assign = not any( isinstance(child, nodes.Nonlocal) and node.name in child.names for child in defframe.get_children() ) if ( base_scope_type == "lambda" and isinstance(frame, nodes.ClassDef) and node.name in frame.locals ): # This rule verifies that if the definition node of the # checked name is an Arguments node and if the name # is used a default value in the arguments defaults # and the actual definition of the variable label # is happening before the Arguments definition. # # bar = None # foo = lambda bar=bar: bar # # In this case, maybe_before_assign should be _False, otherwise # it should be _True. maybe_before_assign = not ( isinstance(defnode, nodes.Arguments) and node in defnode.defaults and frame.locals[node.name][0].fromlineno < defstmt.fromlineno ) elif isinstance(defframe, nodes.ClassDef) and isinstance( frame, nodes.FunctionDef ): # Special rule for function return annotations, # using a name defined earlier in the class containing the function. if node is frame.returns and defframe.parent_of(frame.returns): annotation_return = _True if ( frame.returns.name in defframe.locals and defframe.locals[node.name][0].lineno < frame.lineno ): # Detect class assignments with a name defined earlier in the # class. In this case, no warning should be raised. maybe_before_assign = _False else: maybe_before_assign = _True if isinstance(node.parent, nodes.Arguments): maybe_before_assign = stmt.fromlineno <= defstmt.fromlineno elif is_recursive_klass: maybe_before_assign = _True else: maybe_before_assign = ( maybe_before_assign and stmt.fromlineno <= defstmt.fromlineno ) if maybe_before_assign and stmt.fromlineno == defstmt.fromlineno: if ( isinstance(defframe, nodes.FunctionDef) and frame is defframe and defframe.parent_of(node) and stmt is not defstmt ): # Single statement function, with the statement on the # same line as the function definition maybe_before_assign = _False elif ( isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Expr, nodes.Return, ), ) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) and frame is defframe and defframe.parent_of(node) and stmt is defstmt ): # Single statement if, with assignment expression on same # line as assignment # x = b if (b := _True) else _False maybe_before_assign = _False elif ( isinstance( # pylint: disable=too-many-boolean-expressions defnode, nodes.NamedExpr ) and frame is defframe and defframe.parent_of(stmt) and stmt is defstmt and ( ( defnode.lineno == node.lineno and defnode.col_offset < node.col_offset ) or (defnode.lineno < node.lineno) or ( # Issue in the `ast` module until py39 # Nodes in a multiline string have the same lineno # Could be false-positive without check not PY39_PLUS and defnode.lineno == node.lineno and isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Return, ), ) and isinstance(defstmt.value, nodes.JoinedStr) ) ) ): # Expressions, with assignment expressions # Use only after assignment # b = (c := 2) and c maybe_before_assign = _False # Look for type checking definitions inside a type checking guard. if isinstance(defstmt, (nodes.Import, nodes.ImportFrom)): defstmt_parent = defstmt.parent if ( isinstance(defstmt_parent, nodes.If) and defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS ): # Exempt those definitions that are used inside the type checking # guard or that are defined in both type checking guard branches. used_in_branch = defstmt_parent.parent_of(node) defined_in_or_else = _False for definition in defstmt_parent.orelse: if isinstance(definition, nodes.Assign): defined_in_or_else = any( target.name == node.name for target in definition.targets if isinstance(target, nodes.AssignName) ) if defined_in_or_else: break if not used_in_branch and not defined_in_or_else: maybe_before_assign = _True return maybe_before_assign, annotation_return, use_outer_definition @staticmethod def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: """Check if `defstmt` has the potential to use and assign a name in the same statement. """ if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: # The assignment must happen as part of the first element # e.g. "assert (x:= _True), x" # NOT "assert x, (x:= _True)" value = defstmt.value.elts[0] else: value = defstmt.value if isinstance(value, nodes.IfExp): return _True if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp): return _True return isinstance(value, nodes.Call) and ( any(isinstance(kwarg.value, nodes.IfExp) for kwarg in value.keywords) or any(isinstance(arg, nodes.IfExp) for arg in value.args) ) @staticmethod def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool: """Check if variable only gets assigned a type and never a value.""" if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value: return _False defstmt_frame = defstmt.frame(future=_True) node_frame = node.frame(future=_True) parent = node while parent is not defstmt_frame.parent: parent_scope = parent.scope() local_refs = parent_scope.locals.get(node.name, []) for ref_node in local_refs: # If local ref is in the same frame as our node, but on a later lineno # we don't actually care about this local ref. # Local refs are ordered, so we break. # print(var) # var = 1 # <- irrelevant if defstmt_frame == node_frame and ref_node.lineno > node.lineno: break # If the parent of the local reference is anything but an AnnAssign # Or if the AnnAssign adds a value the variable will now have a value # var = 1 # OR # var: int = 1 if ( not isinstance(ref_node.parent, nodes.AnnAssign) or ref_node.parent.value ): return _False parent = parent_scope.parent return _True @staticmethod def _is_first_level_self_reference( node: nodes.Name, defstmt: nodes.ClassDef, found_nodes: List[nodes.NodeNG] ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Check if a first level method's annotation or default values refers to its own class, and return a consumer action """ if node.frame(future=_True).parent == defstmt and node.statement( future=_True ) == node.frame(future=_True): # Check if used as type annotation # Break if postponed evaluation is enabled if utils.is_node_in_type_annotation_context(node): if not utils.is_postponed_evaluation_enabled(node): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, None) # Check if used as default value by calling the class if isinstance(node.parent, nodes.Call) and isinstance( node.parent.parent, nodes.Arguments ): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, found_nodes) @staticmethod def _is_never_evaluated( defnode: nodes.NamedExpr, defnode_parent: nodes.IfExp ) -> bool: """Check if a NamedExpr is inside a side of if ... else that never gets evaluated """ inferred_test = utils.safe_infer(defnode_parent.test) if isinstance(inferred_test, nodes.Const): if inferred_test.value is _True and defnode == defnode_parent.orelse: return _True if inferred_test.value is _False and defnode == defnode_parent.body: return _True return _False def _ignore_class_scope(self, node): """Return _True if the node is in a local class scope, as an assignment. :param node: Node considered :type node: astroid.Node :return: _True if the node is in a local class scope, as an assignment. _False otherwise. :rtype: bool """ # Detect if we are in a local class scope, as an assignment. # For example, the following is fair game. # # class A: # b = 1 # c = lambda b=b: b * b # # class B: # tp = 1 # def func(self, arg: tp): # ... # class C: # tp = 2 # def func(self, arg=tp): # ... # class C: # class Tp: # pass # class D(Tp): # ... name = node.name frame = node.statement(future=_True).scope() in_annotation_or_default_or_decorator = self._defined_in_function_definition( node, frame ) in_ancestor_list = utils.is_ancestor_name(frame, node) if in_annotation_or_default_or_decorator or in_ancestor_list: frame_locals = frame.parent.scope().locals else: frame_locals = frame.locals return not ( (isinstance(frame, nodes.ClassDef) or in_annotation_or_default_or_decorator) and not self._in_lambda_or_comprehension_body(node, frame) and name in frame_locals ) def _loopvar_name(self, node: astroid.Name) -> None: # filter variables according to node's scope astmts = [s for s in node.lookup(node.name)[1] if hasattr(s, "assign_type")] # If this variable usage exists inside a function definition # that exists in the same loop, # the usage is safe because the function will not be defined either if # the variable is not defined. scope = node.scope() if isinstance(scope, nodes.FunctionDef) and any( asmt.scope().parent_of(scope) for asmt in astmts ): return # Filter variables according to their respective scope. Test parent # and statement to avoid #74747. This is not a total fix, which would # introduce a mechanism similar to special attribute lookup in # modules. Also, in order to get correct inference in this case, the # scope lookup rules would need to be changed to return the initial # assignment (which does not exist in code per se) as well as any later # modifications. # pylint: disable-next=too-many-boolean-expressions if ( not astmts or ( astmts[0].parent == astmts[0].root() and astmts[0].parent.parent_of(node) ) or ( astmts[0].is_statement or not isinstance(astmts[0].parent, nodes.Module) and astmts[0].statement(future=_True).parent_of(node) ) ): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement(future=_True).parent_of( stmt ) and not in_for_else_branch(astmts[i].statement(future=_True), stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) != 1: return assign = astmts[0].assign_type() if not ( isinstance(assign, (nodes.For, nodes.Comprehension, nodes.GeneratorExp)) and assign.statement(future=_True) is not node.statement(future=_True) ): return # For functions we can do more by inferring the length of the itered object if not isinstance(assign, nodes.For): self.add_message("undefined-loop-variable", args=node.name, node=node) return try: inferred = next(assign.iter.infer()) except astroid.InferenceError: self.add_message("undefined-loop-variable", args=node.name, node=node) else: if ( isinstance(inferred, astroid.Instance) and inferred.qname() == BUILTIN_RANGE ): # Consider range() objects safe, even if they might not yield any results. return # Consider sequences. sequences = ( nodes.List, nodes.Tuple, nodes.Dict, nodes.Set, astroid.objects.FrozenSet, ) if not isinstance(inferred, sequences): self.add_message("undefined-loop-variable", args=node.name, node=node) return elements = getattr(inferred, "elts", getattr(inferred, "items", [])) if not elements: self.add_message("undefined-loop-variable", args=node.name, node=node) def _check_is_unused(self, name, node, stmt, global_names, nonlocal_names): # Ignore some special names specified by user configuration. if self._is_name_ignored(stmt, name): return # Ignore names that were added dynamically to the Function scope if ( isinstance(node, nodes.FunctionDef) and name == "__class__" and len(node.locals["__class__"]) == 1 and isinstance(node.locals["__class__"][0], nodes.ClassDef) ): return # Ignore names imported by the global statement. if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)): # Detect imports, assigned to global statements. if global_names and _import_name_is_global(stmt, global_names): return argnames = list( itertools.chain(node.argnames(), [arg.name for arg in node.args.kwonlyargs]) ) # Care about functions with unknown argument (builtins) if name in argnames: self._check_unused_arguments(name, node, stmt, argnames) else: if stmt.parent and isinstance( stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple) ): if name in nonlocal_names: return qname = asname = None if isinstance(stmt, (nodes.Import, nodes.ImportFrom)): # Need the complete name, which we don't have in .locals. if len(stmt.names) > 1: import_names = next( (names for names in stmt.names if name in names), None ) else: import_names = stmt.names[0] if import_names: qname, asname = import_names name = asname or qname if _has_locals_call_after_node(stmt, node.scope()): message_name = "possibly-unused-variable" else: if isinstance(stmt, nodes.Import): if asname is not None: msg = f"{qname} imported as {asname}" else: msg = f"import {name}" self.add_message("unused-import", args=msg, node=stmt) return if isinstance(stmt, nodes.ImportFrom): if asname is not None: msg = f"{qname} imported from {stmt.modname} as {asname}" else: msg = f"{name} imported from {stmt.modname}" self.add_message("unused-import", args=msg, node=stmt) return message_name = "unused-variable" if isinstance(stmt, nodes.FunctionDef) and stmt.decorators: return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Special case for exception variable if isinstance(stmt.parent, nodes.ExceptHandler) and any( n.name == name for n in stmt.parent.nodes_of_class(nodes.Name) ): return self.add_message(message_name, args=name, node=stmt) def _is_name_ignored(self, stmt, name): authorized_rgx = self.config.dummy_variables_rgx if ( isinstance(stmt, nodes.AssignName) and isinstance(stmt.parent, nodes.Arguments) or isinstance(stmt, nodes.Arguments) ): regex = self.config.ignored_argument_names else: regex = authorized_rgx return regex and regex.match(name) def _check_unused_arguments(self, name, node, stmt, argnames): is_method = node.is_method() klass = node.parent.frame(future=_True) if is_method and isinstance(klass, nodes.ClassDef): confidence = ( INFERENCE if utils.has_known_bases(klass) else INFERENCE_FAILURE ) else: confidence = HIGH if is_method: # Don't warn for the first argument of a (non static) method if node.type != "staticmethod" and name == argnames[0]: return # Don't warn for argument of an overridden method overridden = overridden_method(klass, node.name) if overridden is not None and name in overridden.argnames(): return if node.name in utils.PYMETHODS and node.name not in ( "__init__", "__new__", ): return # Don't check callback arguments if any( node.name.startswith(cb) or node.name.endswith(cb) for cb in self.config.callbacks ): return # Don't check arguments of singledispatch.register function. if utils.is_registered_in_singledispatch_function(node): return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Don't check protocol classes if utils.is_protocol_class(klass): return self.add_message("unused-argument", args=name, node=stmt, confidence=confidence) def _check_late_binding_closure(self, node: nodes.Name) -> None: """Check whether node is a cell var that is assigned within a containing loop. Special cases where we don't care about the error: 1. When the node's function is immediately called, e.g. (lambda: i)() 2. When the node's function is returned from within the loop, e.g. return lambda: i """ if not self.linter.is_message_enabled("cell-var-from-loop"): return node_scope = node.frame(future=_True) # If node appears in a default argument expression, # look at the next enclosing frame instead if utils.is_default_argument(node, node_scope): node_scope = node_scope.parent.frame(future=_True) # Check if node is a cell var if ( not isinstance(node_scope, (nodes.Lambda, nodes.FunctionDef)) or node.name in node_scope.locals ): return assign_scope, stmts = node.lookup(node.name) if not stmts or not assign_scope.parent_of(node_scope): return if utils.is_comprehension(assign_scope): self.add_message("cell-var-from-loop", node=node, args=node.name) else: # Look for an enclosing For loop. # Currently, we only consider the first assignment assignment_node = stmts[0] maybe_for = assignment_node while maybe_for and not isinstance(maybe_for, nodes.For): if maybe_for is assign_scope: break maybe_for = maybe_for.parent else: if ( maybe_for and maybe_for.parent_of(node_scope) and not utils.is_being_called(node_scope) and node_scope.parent and not isinstance(node_scope.statement(future=_True), nodes.Return) ): self.add_message("cell-var-from-loop", node=node, args=node.name) def _should_ignore_redefined_builtin(self, stmt): if not isinstance(stmt, nodes.ImportFrom): return _False return stmt.modname in self.config.redefining_builtins_modules def _allowed_redefined_builtin(self, name): return name in self.config.allowed_redefined_builtins def _has_homonym_in_upper_function_scope( self, node: nodes.Name, index: int ) -> bool: """Return whether there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function :param node: node to check for :param index: index of the current consumer inside self._to_consume :return: _True if there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function, _False otherwise """ return any( _consumer.scope_type == "function" and node.name in _consumer.to_consume for _consumer in self._to_consume[index - 1 :: -1] ) def _store_type_annotation_node(self, type_annotation): """Given a type annotation, store all the name nodes it refers to.""" if isinstance(type_annotation, nodes.Name): self._type_annotation_names.append(type_annotation.name) return if isinstance(type_annotation, nodes.Attribute): self._store_type_annotation_node(type_annotation.expr) return if not isinstance(type_annotation, nodes.Subscript): return if ( isinstance(type_annotation.value, nodes.Attribute) and isinstance(type_annotation.value.expr, nodes.Name) and type_annotation.value.expr.name == TYPING_MODULE ): self._type_annotation_names.append(TYPING_MODULE) return self._type_annotation_names.extend( annotation.name for annotation in type_annotation.nodes_of_class(nodes.Name) ) def _store_type_annotation_names(self, node): type_annotation = node.type_annotation if not type_annotation: return self._store_type_annotation_node(node.type_annotation) def _check_self_cls_assign(self, node: nodes.Assign) -> None: """Check that self/cls don't get assigned.""" assign_names: Set[Optional[str]] = set() for target in node.targets: if isinstance(target, nodes.AssignName): assign_names.add(target.name) elif isinstance(target, nodes.Tuple): assign_names.update( elt.name for elt in target.elts if isinstance(elt, nodes.AssignName) ) scope = node.scope() nonlocals_with_same_name = any( child for child in scope.body if isinstance(child, nodes.Nonlocal) ) if nonlocals_with_same_name: scope = node.scope().parent.scope() if not ( isinstance(scope, nodes.FunctionDef) and scope.is_method() and "builtins.staticmethod" not in scope.decoratornames() ): return argument_names = scope.argnames() if not argument_names: return self_cls_name = argument_names[0] if self_cls_name in assign_names: self.add_message("self-cls-assignment", node=node, args=(self_cls_name,)) def _check_unpacking(self, inferred, node, targets): """Check for unbalanced tuple unpacking and unpacking non sequences. """ if utils.is_inside_abstract_class(node): return if utils.is_comprehension(node): return if inferred is astroid.Uninferable: return if ( isinstance(inferred.parent, nodes.Arguments) and isinstance(node.value, nodes.Name) and node.value.name == inferred.parent.vararg ): # Variable-length argument, we can't determine the length. return # Attempt to check unpacking is properly balanced values = self._nodes_to_unpack(inferred) if values is not None: if len(targets) != len(values): # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return self.add_message( "unbalanced-tuple-unpacking", node=node, args=( _get_unpacking_extra_info(node, inferred), len(targets), len(values), ), ) # attempt to check unpacking may be possible (ie RHS is iterable) elif not utils.is_iterable(inferred): self.add_message( "unpacking-non-sequence", node=node, args=(_get_unpacking_extra_info(node, inferred),), ) @staticmethod def _nodes_to_unpack(node: nodes.NodeNG) -> Optional[List[nodes.NodeNG]]: """Return the list of values of the `Assign` node.""" if isinstance(node, (nodes.Tuple, nodes.List)): return node.itered() if isinstance(node, astroid.Instance) and any( ancestor.qname() == "typing.NamedTuple" for ancestor in node.ancestors() ): return [i for i in node.values() if isinstance(i, nodes.AssignName)] return None def _check_module_attrs(self, node, module, module_names): """Check that module_names (list of string) are accessible through the given module if the latest access name corresponds to a module, return it """ while module_names: name = module_names.pop(0) if name == "__dict__": module = None break try: module = next(module.getattr(name)[0].infer()) if module is astroid.Uninferable: return None except astroid.NotFoundError: if module.name in self._ignored_modules: return None self.add_message( "no-name-in-module", args=(name, module.name), node=node ) return None except astroid.InferenceError: return None if module_names: modname = module.name if module else "__dict__" self.add_message( "no-name-in-module", node=node, args=(".".join(module_names), modname) ) return None if isinstance(module, nodes.Module): return module return None def _check_all(self, node: nodes.Module, not_consumed): assigned = next(node.igetattr("__all__")) if assigned is astroid.Uninferable: return if not assigned.pytype() in {"builtins.list", "builtins.tuple"}: line, col = assigned.tolineno, assigned.col_offset self.add_message("invalid-all-format", line=line, col_offset=col, node=node) return for elt in getattr(assigned, "elts", ()): try: elt_name = next(elt.infer()) except astroid.InferenceError: continue if elt_name is astroid.Uninferable: continue if not elt_name.parent: continue if not isinstance(elt_name, nodes.Const) or not isinstance( elt_name.value, str ): self.add_message("invalid-all-object", args=elt.as_string(), node=elt) continue elt_name = elt_name.value # If elt is in not_consumed, remove it from not_consumed if elt_name in not_consumed: del not_consumed[elt_name] continue if elt_name not in node.locals: if not node.package: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) else: basename = os.path.splitext(node.file)[0] if os.path.basename(basename) == "__init__": name = node.name + "." + elt_name try: astroid.modutils.file_from_modpath(name.split(".")) except ImportError: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) except SyntaxError: # don't yield a syntax-error warning, # because it will be later yielded # when the file will be checked pass def _check_globals(self, not_consumed): if self._allow_global_unused_variables: return for name, node_lst in not_consumed.items(): for node in node_lst: self.add_message("unused-variable", args=(name,), node=node) def _check_imports(self, not_consumed): local_names = _fix_dot_imports(not_consumed) checked = set() unused_wildcard_imports: DefaultDict[ Tuple[str, nodes.ImportFrom], List[str] ] = collections.defaultdict(list) for name, stmt in local_names: for imports in stmt.names: real_name = imported_name = imports[0] if imported_name == "*": real_name = name as_name = imports[1] if real_name in checked: continue if name not in (real_name, as_name): continue checked.add(real_name) is_type_annotation_import = ( imported_name in self._type_annotation_names or as_name in self._type_annotation_names ) if isinstance(stmt, nodes.Import) or ( isinstance(stmt, nodes.ImportFrom) and not stmt.modname ): if isinstance(stmt, nodes.ImportFrom) and SPECIAL_OBJ.search( imported_name ): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if as_name == "_": continue if as_name is None: msg = f"import {imported_name}" else: msg = f"{imported_name} imported as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) elif isinstance(stmt, nodes.ImportFrom) and stmt.modname != FUTURE: if SPECIAL_OBJ.search(imported_name): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if _is_from_future_import(stmt, name): # Check if the name is in fact loaded from a # __future__ import in another module. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if imported_name == "*": unused_wildcard_imports[(stmt.modname, stmt)].append(name) else: if as_name is None: msg = f"{imported_name} imported from {stmt.modname}" else: msg = f"{imported_name} imported from {stmt.modname} as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) # Construct string for unused-wildcard-import message for module, unused_list in unused_wildcard_imports.items(): if len(unused_list) == 1: arg_string = unused_list[0] else: arg_string = ( f"{', '.join(i for i in unused_list[:-1])} and {unused_list[-1]}" ) self.add_message( "unused-wildcard-import", args=(arg_string, module[0]), node=module[1] ) del self._to_consume def _check_metaclasses(self, node): """Update consumption analysis for metaclasses.""" consumed = [] # [(scope_locals, consumed_key)] for child_node in node.get_children(): if isinstance(child_node, nodes.ClassDef): consumed.extend(self._check_classdef_metaclasses(child_node, node)) # Pop the consumed items, in order to avoid having # unused-import and unused-variable false positives for scope_locals, name in consumed: scope_locals.pop(name, None) def _check_classdef_metaclasses(self, klass, parent_node): if not klass._metaclass: # Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors return [] consumed = [] # [(scope_locals, consumed_key)] metaclass = klass.metaclass() name = None if isinstance(klass._metaclass, nodes.Name): name = klass._metaclass.name elif isinstance(klass._metaclass, nodes.Attribute) and klass._metaclass.expr: attr = klass._metaclass.expr while not isinstance(attr, nodes.Name): attr = attr.expr name = attr.name elif metaclass: name = metaclass.root().name found = _False name = METACLASS_NAME_TRANSFORMS.get(name, name) if name: # check enclosing scopes starting from most local for scope_locals, _, _, _ in self._to_consume[::-1]: found_nodes = scope_locals.get(name, []) for found_node in found_nodes: if found_node.lineno <= klass.lineno: consumed.append((scope_locals, name)) found = _True break # Check parent scope nodes_in_parent_scope = parent_node.locals.get(name, []) for found_node_parent in nodes_in_parent_scope: if found_node_parent.lineno <= klass.lineno: found = _True break if ( not found and not metaclass and not ( name in nodes.Module.scope_attrs or utils.is_builtin(name) or name in self.config.additional_builtins ) ): self.add_message("undefined-variable", node=klass, args=(name,)) return consumed
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,168
_check_metaclasses
ref
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,172
_check_all
ref
function
self._check_all(node, not_consumed)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,175
_check_globals
ref
function
self._check_globals(not_consumed)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,181
_check_imports
ref
function
self._check_imports(not_consumed)
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,183
visit_classdef
def
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,185
NamesConsumer
ref
class
__init__ __repr__ __iter__ to_consume consumed consumed_uncertain scope_type mark_as_consumed get_next_to_consume _uncertain_nodes_in_except_blocks _defines_name_raises_or_returns _check_loop_finishes_via_except _recursive_search_for_continue_before_break _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,187
leave_classdef
def
class
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,192
visit_lambda
def
function
def visit_lambda(self, node: nodes.Lambda) -> None: """Visit lambda: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "lambda")) def leave_lambda(self, _: nodes.Lambda) -> None: """Leave lambda: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_generatorexp(self, node: nodes.GeneratorExp) -> None: """Visit genexpr: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_generatorexp(self, _: nodes.GeneratorExp) -> None: """Leave genexpr: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_dictcomp(self, node: nodes.DictComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_dictcomp(self, _: nodes.DictComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_setcomp(self, node: nodes.SetComp) -> None: """Visit setcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_setcomp(self, _: nodes.SetComp) -> None: """Leave setcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Visit function: update consumption analysis variable and check locals.""" self._to_consume.append(NamesConsumer(node, "function")) if not ( self.linter.is_message_enabled("redefined-outer-name") or self.linter.is_message_enabled("redefined-builtin") ): return globs = node.root().globals for name, stmt in node.items(): if name in globs and not isinstance(stmt, nodes.Global): definition = globs[name][0] if ( isinstance(definition, nodes.ImportFrom) and definition.modname == FUTURE ): # It is a __future__ directive, not a symbol. continue # Do not take in account redefined names for the purpose # of type checking.: if any( isinstance(definition.parent, nodes.If) and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS for definition in globs[name] ): continue line = definition.fromlineno if not self._is_name_ignored(stmt, name): self.add_message( "redefined-outer-name", args=(name, line), node=stmt ) elif ( utils.is_builtin(name) and not self._allowed_redefined_builtin(name) and not self._should_ignore_redefined_builtin(stmt) ): # do not print Redefining builtin for additional builtins self.add_message("redefined-builtin", args=name, node=stmt) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Leave function: check function's locals are consumed.""" self._check_metaclasses(node) if node.type_comment_returns: self._store_type_annotation_node(node.type_comment_returns) if node.type_comment_args: for argument_annotation in node.type_comment_args: self._store_type_annotation_node(argument_annotation) not_consumed = self._to_consume.pop().to_consume if not ( self.linter.is_message_enabled("unused-variable") or self.linter.is_message_enabled("possibly-unused-variable") or self.linter.is_message_enabled("unused-argument") ): return # Don't check arguments of function which are only raising an exception. if utils.is_error(node): return # Don't check arguments of abstract methods or within an interface. is_method = node.is_method() if is_method and node.is_abstract(): return global_names = _flattened_scope_names(node.nodes_of_class(nodes.Global)) nonlocal_names = _flattened_scope_names(node.nodes_of_class(nodes.Nonlocal)) for name, stmts in not_consumed.items(): self._check_is_unused(name, node, stmts[0], global_names, nonlocal_names) visit_asyncfunctiondef = visit_functiondef leave_asyncfunctiondef = leave_functiondef @utils.check_messages( "global-variable-undefined", "global-variable-not-assigned", "global-statement", "global-at-module-level", "redefined-builtin", ) def visit_global(self, node: nodes.Global) -> None: """Check names imported exists in the global scope.""" frame = node.frame(future=_True) if isinstance(frame, nodes.Module): self.add_message("global-at-module-level", node=node) return module = frame.root() default_message = _True locals_ = node.scope().locals for name in node.names: try: assign_nodes = module.getattr(name) except astroid.NotFoundError: # unassigned global, skip assign_nodes = [] not_defined_locally_by_import = not any( isinstance(local, nodes.Import) for local in locals_.get(name, ()) ) if ( not utils.is_reassigned_after_current(node, name) and not utils.is_deleted_after_current(node, name) and not_defined_locally_by_import ): self.add_message("global-variable-not-assigned", args=name, node=node) default_message = _False continue for anode in assign_nodes: if ( isinstance(anode, nodes.AssignName) and anode.name in module.special_attributes ): self.add_message("redefined-builtin", args=name, node=node) break if anode.frame(future=_True) is module: # module level assignment break if ( isinstance(anode, (nodes.ClassDef, nodes.FunctionDef)) and anode.parent is module ): # module level function assignment break else: if not_defined_locally_by_import: # global undefined at the module scope self.add_message("global-variable-undefined", args=name, node=node) default_message = _False if default_message: self.add_message("global-statement", node=node) def visit_assignname(self, node: nodes.AssignName) -> None: if isinstance(node.assign_type(), nodes.AugAssign): self.visit_name(node) def visit_delname(self, node: nodes.DelName) -> None: self.visit_name(node) def visit_name(self, node: nodes.Name) -> None: """Don't add the 'utils.check_messages' decorator here! It's important that all 'Name' nodes are visited, otherwise the 'NamesConsumers' won't be correct. """ stmt = node.statement(future=_True) if stmt.fromlineno is None: # name node from an astroid built from live code, skip assert not stmt.root().file.endswith(".py") return self._undefined_and_used_before_checker(node, stmt) if self._is_undefined_loop_variable_enabled: self._loopvar_name(node) @utils.check_messages("redefined-outer-name") def visit_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return for outer_except, outer_except_assign_name in self._except_handler_names_queue: if node.name.name == outer_except_assign_name.name: self.add_message( "redefined-outer-name", args=(outer_except_assign_name.name, outer_except.fromlineno), node=node, ) break self._except_handler_names_queue.append((node, node.name)) @utils.check_messages("redefined-outer-name") def leave_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return self._except_handler_names_queue.pop() def _undefined_and_used_before_checker( self, node: nodes.Name, stmt: nodes.NodeNG ) -> None: frame = stmt.scope() start_index = len(self._to_consume) - 1 # iterates through parent scopes, from the inner to the outer base_scope_type = self._to_consume[start_index].scope_type for i in range(start_index, -1, -1): current_consumer = self._to_consume[i] # Certain nodes shouldn't be checked as they get checked another time if self._should_node_be_skipped(node, current_consumer, i == start_index): continue action, nodes_to_consume = self._check_consumer( node, stmt, frame, current_consumer, i, base_scope_type ) if nodes_to_consume: # Any nodes added to consumed_uncertain by get_next_to_consume() # should be added back so that they are marked as used. # They will have already had a chance to emit used-before-assignment. # We check here instead of before every single return in _check_consumer() nodes_to_consume += current_consumer.consumed_uncertain[node.name] current_consumer.mark_as_consumed(node.name, nodes_to_consume) if action is VariableVisitConsumerAction.CONTINUE: continue if action is VariableVisitConsumerAction.RETURN: return # we have not found the name, if it isn't a builtin, that's an # undefined name ! if ( self._is_undefined_variable_enabled and not ( node.name in nodes.Module.scope_attrs or utils.is_builtin(node.name) or node.name in self.config.additional_builtins or ( node.name == "__class__" and isinstance(frame, nodes.FunctionDef) and frame.is_method() ) ) and not utils.node_ignores_exception(node, NameError) ): self.add_message("undefined-variable", args=node.name, node=node) def _should_node_be_skipped( self, node: nodes.Name, consumer: NamesConsumer, is_start_index: bool ) -> bool: """Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks. """ if consumer.scope_type == "class": # The list of base classes in the class definition is not part # of the class body. # If the current scope is a class scope but it's not the inner # scope, ignore it. This prevents to access this scope instead of # the globals one in function members when there are some common # names. if utils.is_ancestor_name(consumer.node, node) or ( not is_start_index and self._ignore_class_scope(node) ): return _True # Ignore inner class scope for keywords in class definition if isinstance(node.parent, nodes.Keyword) and isinstance( node.parent.parent, nodes.ClassDef ): return _True elif consumer.scope_type == "function" and self._defined_in_function_definition( node, consumer.node ): # If the name node is used as a function default argument's value or as # a decorator, then start from the parent frame of the function instead # of the function frame - and thus open an inner class scope return _True elif consumer.scope_type == "lambda" and utils.is_default_argument( node, consumer.node ): return _True return _False # pylint: disable=too-many-return-statements def _check_consumer( self, node: nodes.Name, stmt: nodes.NodeNG, frame: nodes.LocalsDictNodeNG, current_consumer: NamesConsumer, consumer_level: int, base_scope_type: Any, ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Checks a consumer for conditions that should trigger messages.""" # If the name has already been consumed, only check it's not a loop # variable used outside the loop. # Avoid the case where there are homonyms inside function scope and # comprehension current scope (avoid bug #1731) if node.name in current_consumer.consumed: if utils.is_func_decorator(current_consumer.node) or not ( current_consumer.scope_type == "comprehension" and self._has_homonym_in_upper_function_scope(node, consumer_level) # But don't catch homonyms against the filter of a comprehension, # (like "if x" in "[x for x in expr() if x]") # https://github.com/PyCQA/pylint/issues/5586 and not ( ( isinstance(node.parent.parent, nodes.Comprehension) and node.parent in node.parent.parent.ifs ) # Or homonyms against values to keyword arguments # (like "var" in "[func(arg=var) for var in expr()]") or ( isinstance(node.scope(), nodes.ComprehensionScope) and isinstance(node.parent, (nodes.Call, nodes.Keyword)) ) ) ): self._check_late_binding_closure(node) self._loopvar_name(node) return (VariableVisitConsumerAction.RETURN, None) found_nodes = current_consumer.get_next_to_consume(node) if found_nodes is None: return (VariableVisitConsumerAction.CONTINUE, None) if not found_nodes: if node.name in current_consumer.consumed_uncertain: confidence = CONTROL_FLOW else: confidence = HIGH self.add_message( "used-before-assignment", args=node.name, node=node, confidence=confidence, ) # Mark for consumption any nodes added to consumed_uncertain by # get_next_to_consume() because they might not have executed. return ( VariableVisitConsumerAction.RETURN, current_consumer.consumed_uncertain[node.name], ) self._check_late_binding_closure(node) if not ( self._is_undefined_variable_enabled or self._is_used_before_assignment_enabled ): return (VariableVisitConsumerAction.RETURN, found_nodes) defnode = utils.assign_parent(found_nodes[0]) defstmt = defnode.statement(future=_True) defframe = defstmt.frame(future=_True) # The class reuses itself in the class scope. is_recursive_klass = ( frame is defframe and defframe.parent_of(node) and isinstance(defframe, nodes.ClassDef) and node.name == defframe.name ) if ( is_recursive_klass and utils.get_node_first_ancestor_of_type(node, nodes.Lambda) and ( not utils.is_default_argument(node) or node.scope().parent.scope() is not defframe ) ): # Self-referential class references are fine in lambda's -- # As long as they are not part of the default argument directly # under the scope of the parent self-referring class. # Example of valid default argument: # class MyName3: # myattr = 1 # mylambda3 = lambda: lambda a=MyName3: a # Example of invalid default argument: # class MyName4: # myattr = 1 # mylambda4 = lambda a=MyName4: lambda: a # If the above conditional is _True, # there is no possibility of undefined-variable # Also do not consume class name # (since consuming blocks subsequent checks) # -- quit return (VariableVisitConsumerAction.RETURN, None) ( maybe_before_assign, annotation_return, use_outer_definition, ) = self._is_variable_violation( node, defnode, stmt, defstmt, frame, defframe, base_scope_type, is_recursive_klass, ) if use_outer_definition: return (VariableVisitConsumerAction.CONTINUE, None) if ( maybe_before_assign and not utils.is_defined_before(node) and not astroid.are_exclusive(stmt, defstmt, ("NameError",)) ): # Used and defined in the same place, e.g `x += 1` and `del x` defined_by_stmt = defstmt is stmt and isinstance( node, (nodes.DelName, nodes.AssignName) ) if ( is_recursive_klass or defined_by_stmt or annotation_return or isinstance(defstmt, nodes.Delete) ): if not utils.node_ignores_exception(node, NameError): # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance( stmt, ( nodes.AnnAssign, nodes.FunctionDef, nodes.Arguments, ), ) and node.name in node.root().locals ): if defined_by_stmt: return (VariableVisitConsumerAction.CONTINUE, [node]) return (VariableVisitConsumerAction.CONTINUE, None) elif base_scope_type != "lambda": # E0601 may *not* occurs in lambda scope. # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif base_scope_type == "lambda": # E0601 can occur in class-level scope in lambdas, as in # the following example: # class A: # x = lambda attr: f + attr # f = 42 # We check lineno because doing the following is fine: # class A: # x = 42 # y = lambda attr: x + attr if ( isinstance(frame, nodes.ClassDef) and node.name in frame.locals and stmt.fromlineno <= defstmt.fromlineno ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) elif self._is_only_type_assignment(node, defstmt): if node.scope().locals.get(node.name): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH ) else: self.add_message( "undefined-variable", args=node.name, node=node, confidence=HIGH ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif isinstance(defstmt, nodes.ClassDef): return self._is_first_level_self_reference(node, defstmt, found_nodes) elif isinstance(defnode, nodes.NamedExpr): if isinstance(defnode.parent, nodes.IfExp): if self._is_never_evaluated(defnode, defnode.parent): self.add_message( "undefined-variable", args=node.name, node=node, confidence=INFERENCE, ) return (VariableVisitConsumerAction.RETURN, found_nodes) return (VariableVisitConsumerAction.RETURN, found_nodes) @utils.check_messages("no-name-in-module") def visit_import(self, node: nodes.Import) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return for name, _ in node.names: parts = name.split(".") try: module = next(_infer_name_module(node, parts[0])) except astroid.ResolveError: continue if not isinstance(module, nodes.Module): continue self._check_module_attrs(node, module, parts[1:]) @utils.check_messages("no-name-in-module") def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return name_parts = node.modname.split(".") try: module = node.do_import_module(name_parts[0]) except astroid.AstroidBuildingException: return module = self._check_module_attrs(node, module, name_parts[1:]) if not module: return for name, _ in node.names: if name == "*": continue self._check_module_attrs(node, module, name.split(".")) @utils.check_messages( "unbalanced-tuple-unpacking", "unpacking-non-sequence", "self-cls-assignment" ) def visit_assign(self, node: nodes.Assign) -> None: """Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned. """ self._check_self_cls_assign(node) if not isinstance(node.targets[0], (nodes.Tuple, nodes.List)): return targets = node.targets[0].itered() try: inferred = utils.safe_infer(node.value) if inferred is not None: self._check_unpacking(inferred, node, targets) except astroid.InferenceError: return # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_listcomp(self, _: nodes.ListComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def leave_assign(self, node: nodes.Assign) -> None: self._store_type_annotation_names(node) def leave_with(self, node: nodes.With) -> None: self._store_type_annotation_names(node) def visit_arguments(self, node: nodes.Arguments) -> None: for annotation in node.type_comment_args: self._store_type_annotation_node(annotation) # Relying on other checker's options, which might not have been initialized yet. @astroid.decorators.cachedproperty def _analyse_fallback_blocks(self): return get_global_option(self, "analyse-fallback-blocks", default=_False) @astroid.decorators.cachedproperty def _ignored_modules(self): return get_global_option(self, "ignored-modules", default=[]) @astroid.decorators.cachedproperty def _allow_global_unused_variables(self): return get_global_option(self, "allow-global-unused-variables", default=_True) @staticmethod def _defined_in_function_definition(node, frame): in_annotation_or_default_or_decorator = _False if ( isinstance(frame, nodes.FunctionDef) and node.statement(future=_True) is frame ): in_annotation_or_default_or_decorator = ( ( node in frame.args.annotations or node in frame.args.posonlyargs_annotations or node in frame.args.kwonlyargs_annotations or node is frame.args.varargannotation or node is frame.args.kwargannotation ) or frame.args.parent_of(node) or (frame.decorators and frame.decorators.parent_of(node)) or ( frame.returns and (node is frame.returns or frame.returns.parent_of(node)) ) ) return in_annotation_or_default_or_decorator @staticmethod def _in_lambda_or_comprehension_body( node: nodes.NodeNG, frame: nodes.NodeNG ) -> bool: """Return _True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.""" child = node parent = node.parent while parent is not None: if parent is frame: return _False if isinstance(parent, nodes.Lambda) and child is not parent.args: # Body of lambda should not have access to class attributes. return _True if isinstance(parent, nodes.Comprehension) and child is not parent.iter: # Only iter of list/set/dict/generator comprehension should have access. return _True if isinstance(parent, nodes.ComprehensionScope) and not ( parent.generators and child is parent.generators[0] ): # Body of list/set/dict/generator comprehension should not have access to class attributes. # Furthermore, only the first generator (if multiple) in comprehension should have access. return _True child = parent parent = parent.parent return _False @staticmethod def _is_variable_violation( node: nodes.Name, defnode, stmt: nodes.Statement, defstmt: nodes.Statement, frame, # scope of statement of node defframe, base_scope_type, is_recursive_klass, ) -> Tuple[bool, bool, bool]: # pylint: disable=too-many-nested-blocks maybe_before_assign = _True annotation_return = _False use_outer_definition = _False if frame is not defframe: maybe_before_assign = _detect_global_scope(node, frame, defframe) elif defframe.parent is None: # we are at the module level, check the name is not # defined in builtins if ( node.name in defframe.scope_attrs or astroid.builtin_lookup(node.name)[1] ): maybe_before_assign = _False else: # we are in a local scope, check the name is not # defined in global or builtin scope # skip this lookup if name is assigned later in function scope/lambda # Note: the node.frame() is not the same as the `frame` argument which is # equivalent to frame.statement().scope() forbid_lookup = ( isinstance(frame, nodes.FunctionDef) or isinstance(node.frame(future=_True), nodes.Lambda) ) and _assigned_locally(node) if not forbid_lookup and defframe.root().lookup(node.name)[1]: maybe_before_assign = _False use_outer_definition = stmt == defstmt and not isinstance( defnode, nodes.Comprehension ) # check if we have a nonlocal elif node.name in defframe.locals: maybe_before_assign = not any( isinstance(child, nodes.Nonlocal) and node.name in child.names for child in defframe.get_children() ) if ( base_scope_type == "lambda" and isinstance(frame, nodes.ClassDef) and node.name in frame.locals ): # This rule verifies that if the definition node of the # checked name is an Arguments node and if the name # is used a default value in the arguments defaults # and the actual definition of the variable label # is happening before the Arguments definition. # # bar = None # foo = lambda bar=bar: bar # # In this case, maybe_before_assign should be _False, otherwise # it should be _True. maybe_before_assign = not ( isinstance(defnode, nodes.Arguments) and node in defnode.defaults and frame.locals[node.name][0].fromlineno < defstmt.fromlineno ) elif isinstance(defframe, nodes.ClassDef) and isinstance( frame, nodes.FunctionDef ): # Special rule for function return annotations, # using a name defined earlier in the class containing the function. if node is frame.returns and defframe.parent_of(frame.returns): annotation_return = _True if ( frame.returns.name in defframe.locals and defframe.locals[node.name][0].lineno < frame.lineno ): # Detect class assignments with a name defined earlier in the # class. In this case, no warning should be raised. maybe_before_assign = _False else: maybe_before_assign = _True if isinstance(node.parent, nodes.Arguments): maybe_before_assign = stmt.fromlineno <= defstmt.fromlineno elif is_recursive_klass: maybe_before_assign = _True else: maybe_before_assign = ( maybe_before_assign and stmt.fromlineno <= defstmt.fromlineno ) if maybe_before_assign and stmt.fromlineno == defstmt.fromlineno: if ( isinstance(defframe, nodes.FunctionDef) and frame is defframe and defframe.parent_of(node) and stmt is not defstmt ): # Single statement function, with the statement on the # same line as the function definition maybe_before_assign = _False elif ( isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Expr, nodes.Return, ), ) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) and frame is defframe and defframe.parent_of(node) and stmt is defstmt ): # Single statement if, with assignment expression on same # line as assignment # x = b if (b := _True) else _False maybe_before_assign = _False elif ( isinstance( # pylint: disable=too-many-boolean-expressions defnode, nodes.NamedExpr ) and frame is defframe and defframe.parent_of(stmt) and stmt is defstmt and ( ( defnode.lineno == node.lineno and defnode.col_offset < node.col_offset ) or (defnode.lineno < node.lineno) or ( # Issue in the `ast` module until py39 # Nodes in a multiline string have the same lineno # Could be false-positive without check not PY39_PLUS and defnode.lineno == node.lineno and isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Return, ), ) and isinstance(defstmt.value, nodes.JoinedStr) ) ) ): # Expressions, with assignment expressions # Use only after assignment # b = (c := 2) and c maybe_before_assign = _False # Look for type checking definitions inside a type checking guard. if isinstance(defstmt, (nodes.Import, nodes.ImportFrom)): defstmt_parent = defstmt.parent if ( isinstance(defstmt_parent, nodes.If) and defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS ): # Exempt those definitions that are used inside the type checking # guard or that are defined in both type checking guard branches. used_in_branch = defstmt_parent.parent_of(node) defined_in_or_else = _False for definition in defstmt_parent.orelse: if isinstance(definition, nodes.Assign): defined_in_or_else = any( target.name == node.name for target in definition.targets if isinstance(target, nodes.AssignName) ) if defined_in_or_else: break if not used_in_branch and not defined_in_or_else: maybe_before_assign = _True return maybe_before_assign, annotation_return, use_outer_definition @staticmethod def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: """Check if `defstmt` has the potential to use and assign a name in the same statement. """ if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: # The assignment must happen as part of the first element # e.g. "assert (x:= _True), x" # NOT "assert x, (x:= _True)" value = defstmt.value.elts[0] else: value = defstmt.value if isinstance(value, nodes.IfExp): return _True if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp): return _True return isinstance(value, nodes.Call) and ( any(isinstance(kwarg.value, nodes.IfExp) for kwarg in value.keywords) or any(isinstance(arg, nodes.IfExp) for arg in value.args) ) @staticmethod def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool: """Check if variable only gets assigned a type and never a value.""" if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value: return _False defstmt_frame = defstmt.frame(future=_True) node_frame = node.frame(future=_True) parent = node while parent is not defstmt_frame.parent: parent_scope = parent.scope() local_refs = parent_scope.locals.get(node.name, []) for ref_node in local_refs: # If local ref is in the same frame as our node, but on a later lineno # we don't actually care about this local ref. # Local refs are ordered, so we break. # print(var) # var = 1 # <- irrelevant if defstmt_frame == node_frame and ref_node.lineno > node.lineno: break # If the parent of the local reference is anything but an AnnAssign # Or if the AnnAssign adds a value the variable will now have a value # var = 1 # OR # var: int = 1 if ( not isinstance(ref_node.parent, nodes.AnnAssign) or ref_node.parent.value ): return _False parent = parent_scope.parent return _True @staticmethod def _is_first_level_self_reference( node: nodes.Name, defstmt: nodes.ClassDef, found_nodes: List[nodes.NodeNG] ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Check if a first level method's annotation or default values refers to its own class, and return a consumer action """ if node.frame(future=_True).parent == defstmt and node.statement( future=_True ) == node.frame(future=_True): # Check if used as type annotation # Break if postponed evaluation is enabled if utils.is_node_in_type_annotation_context(node): if not utils.is_postponed_evaluation_enabled(node): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, None) # Check if used as default value by calling the class if isinstance(node.parent, nodes.Call) and isinstance( node.parent.parent, nodes.Arguments ): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, found_nodes) @staticmethod def _is_never_evaluated( defnode: nodes.NamedExpr, defnode_parent: nodes.IfExp ) -> bool: """Check if a NamedExpr is inside a side of if ... else that never gets evaluated """ inferred_test = utils.safe_infer(defnode_parent.test) if isinstance(inferred_test, nodes.Const): if inferred_test.value is _True and defnode == defnode_parent.orelse: return _True if inferred_test.value is _False and defnode == defnode_parent.body: return _True return _False def _ignore_class_scope(self, node): """Return _True if the node is in a local class scope, as an assignment. :param node: Node considered :type node: astroid.Node :return: _True if the node is in a local class scope, as an assignment. _False otherwise. :rtype: bool """ # Detect if we are in a local class scope, as an assignment. # For example, the following is fair game. # # class A: # b = 1 # c = lambda b=b: b * b # # class B: # tp = 1 # def func(self, arg: tp): # ... # class C: # tp = 2 # def func(self, arg=tp): # ... # class C: # class Tp: # pass # class D(Tp): # ... name = node.name frame = node.statement(future=_True).scope() in_annotation_or_default_or_decorator = self._defined_in_function_definition( node, frame ) in_ancestor_list = utils.is_ancestor_name(frame, node) if in_annotation_or_default_or_decorator or in_ancestor_list: frame_locals = frame.parent.scope().locals else: frame_locals = frame.locals return not ( (isinstance(frame, nodes.ClassDef) or in_annotation_or_default_or_decorator) and not self._in_lambda_or_comprehension_body(node, frame) and name in frame_locals ) def _loopvar_name(self, node: astroid.Name) -> None: # filter variables according to node's scope astmts = [s for s in node.lookup(node.name)[1] if hasattr(s, "assign_type")] # If this variable usage exists inside a function definition # that exists in the same loop, # the usage is safe because the function will not be defined either if # the variable is not defined. scope = node.scope() if isinstance(scope, nodes.FunctionDef) and any( asmt.scope().parent_of(scope) for asmt in astmts ): return # Filter variables according to their respective scope. Test parent # and statement to avoid #74747. This is not a total fix, which would # introduce a mechanism similar to special attribute lookup in # modules. Also, in order to get correct inference in this case, the # scope lookup rules would need to be changed to return the initial # assignment (which does not exist in code per se) as well as any later # modifications. # pylint: disable-next=too-many-boolean-expressions if ( not astmts or ( astmts[0].parent == astmts[0].root() and astmts[0].parent.parent_of(node) ) or ( astmts[0].is_statement or not isinstance(astmts[0].parent, nodes.Module) and astmts[0].statement(future=_True).parent_of(node) ) ): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement(future=_True).parent_of( stmt ) and not in_for_else_branch(astmts[i].statement(future=_True), stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) != 1: return assign = astmts[0].assign_type() if not ( isinstance(assign, (nodes.For, nodes.Comprehension, nodes.GeneratorExp)) and assign.statement(future=_True) is not node.statement(future=_True) ): return # For functions we can do more by inferring the length of the itered object if not isinstance(assign, nodes.For): self.add_message("undefined-loop-variable", args=node.name, node=node) return try: inferred = next(assign.iter.infer()) except astroid.InferenceError: self.add_message("undefined-loop-variable", args=node.name, node=node) else: if ( isinstance(inferred, astroid.Instance) and inferred.qname() == BUILTIN_RANGE ): # Consider range() objects safe, even if they might not yield any results. return # Consider sequences. sequences = ( nodes.List, nodes.Tuple, nodes.Dict, nodes.Set, astroid.objects.FrozenSet, ) if not isinstance(inferred, sequences): self.add_message("undefined-loop-variable", args=node.name, node=node) return elements = getattr(inferred, "elts", getattr(inferred, "items", [])) if not elements: self.add_message("undefined-loop-variable", args=node.name, node=node) def _check_is_unused(self, name, node, stmt, global_names, nonlocal_names): # Ignore some special names specified by user configuration. if self._is_name_ignored(stmt, name): return # Ignore names that were added dynamically to the Function scope if ( isinstance(node, nodes.FunctionDef) and name == "__class__" and len(node.locals["__class__"]) == 1 and isinstance(node.locals["__class__"][0], nodes.ClassDef) ): return # Ignore names imported by the global statement. if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)): # Detect imports, assigned to global statements. if global_names and _import_name_is_global(stmt, global_names): return argnames = list( itertools.chain(node.argnames(), [arg.name for arg in node.args.kwonlyargs]) ) # Care about functions with unknown argument (builtins) if name in argnames: self._check_unused_arguments(name, node, stmt, argnames) else: if stmt.parent and isinstance( stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple) ): if name in nonlocal_names: return qname = asname = None if isinstance(stmt, (nodes.Import, nodes.ImportFrom)): # Need the complete name, which we don't have in .locals. if len(stmt.names) > 1: import_names = next( (names for names in stmt.names if name in names), None ) else: import_names = stmt.names[0] if import_names: qname, asname = import_names name = asname or qname if _has_locals_call_after_node(stmt, node.scope()): message_name = "possibly-unused-variable" else: if isinstance(stmt, nodes.Import): if asname is not None: msg = f"{qname} imported as {asname}" else: msg = f"import {name}" self.add_message("unused-import", args=msg, node=stmt) return if isinstance(stmt, nodes.ImportFrom): if asname is not None: msg = f"{qname} imported from {stmt.modname} as {asname}" else: msg = f"{name} imported from {stmt.modname}" self.add_message("unused-import", args=msg, node=stmt) return message_name = "unused-variable" if isinstance(stmt, nodes.FunctionDef) and stmt.decorators: return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Special case for exception variable if isinstance(stmt.parent, nodes.ExceptHandler) and any( n.name == name for n in stmt.parent.nodes_of_class(nodes.Name) ): return self.add_message(message_name, args=name, node=stmt) def _is_name_ignored(self, stmt, name): authorized_rgx = self.config.dummy_variables_rgx if ( isinstance(stmt, nodes.AssignName) and isinstance(stmt.parent, nodes.Arguments) or isinstance(stmt, nodes.Arguments) ): regex = self.config.ignored_argument_names else: regex = authorized_rgx return regex and regex.match(name) def _check_unused_arguments(self, name, node, stmt, argnames): is_method = node.is_method() klass = node.parent.frame(future=_True) if is_method and isinstance(klass, nodes.ClassDef): confidence = ( INFERENCE if utils.has_known_bases(klass) else INFERENCE_FAILURE ) else: confidence = HIGH if is_method: # Don't warn for the first argument of a (non static) method if node.type != "staticmethod" and name == argnames[0]: return # Don't warn for argument of an overridden method overridden = overridden_method(klass, node.name) if overridden is not None and name in overridden.argnames(): return if node.name in utils.PYMETHODS and node.name not in ( "__init__", "__new__", ): return # Don't check callback arguments if any( node.name.startswith(cb) or node.name.endswith(cb) for cb in self.config.callbacks ): return # Don't check arguments of singledispatch.register function. if utils.is_registered_in_singledispatch_function(node): return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Don't check protocol classes if utils.is_protocol_class(klass): return self.add_message("unused-argument", args=name, node=stmt, confidence=confidence) def _check_late_binding_closure(self, node: nodes.Name) -> None: """Check whether node is a cell var that is assigned within a containing loop. Special cases where we don't care about the error: 1. When the node's function is immediately called, e.g. (lambda: i)() 2. When the node's function is returned from within the loop, e.g. return lambda: i """ if not self.linter.is_message_enabled("cell-var-from-loop"): return node_scope = node.frame(future=_True) # If node appears in a default argument expression, # look at the next enclosing frame instead if utils.is_default_argument(node, node_scope): node_scope = node_scope.parent.frame(future=_True) # Check if node is a cell var if ( not isinstance(node_scope, (nodes.Lambda, nodes.FunctionDef)) or node.name in node_scope.locals ): return assign_scope, stmts = node.lookup(node.name) if not stmts or not assign_scope.parent_of(node_scope): return if utils.is_comprehension(assign_scope): self.add_message("cell-var-from-loop", node=node, args=node.name) else: # Look for an enclosing For loop. # Currently, we only consider the first assignment assignment_node = stmts[0] maybe_for = assignment_node while maybe_for and not isinstance(maybe_for, nodes.For): if maybe_for is assign_scope: break maybe_for = maybe_for.parent else: if ( maybe_for and maybe_for.parent_of(node_scope) and not utils.is_being_called(node_scope) and node_scope.parent and not isinstance(node_scope.statement(future=_True), nodes.Return) ): self.add_message("cell-var-from-loop", node=node, args=node.name) def _should_ignore_redefined_builtin(self, stmt): if not isinstance(stmt, nodes.ImportFrom): return _False return stmt.modname in self.config.redefining_builtins_modules def _allowed_redefined_builtin(self, name): return name in self.config.allowed_redefined_builtins def _has_homonym_in_upper_function_scope( self, node: nodes.Name, index: int ) -> bool: """Return whether there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function :param node: node to check for :param index: index of the current consumer inside self._to_consume :return: _True if there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function, _False otherwise """ return any( _consumer.scope_type == "function" and node.name in _consumer.to_consume for _consumer in self._to_consume[index - 1 :: -1] ) def _store_type_annotation_node(self, type_annotation): """Given a type annotation, store all the name nodes it refers to.""" if isinstance(type_annotation, nodes.Name): self._type_annotation_names.append(type_annotation.name) return if isinstance(type_annotation, nodes.Attribute): self._store_type_annotation_node(type_annotation.expr) return if not isinstance(type_annotation, nodes.Subscript): return if ( isinstance(type_annotation.value, nodes.Attribute) and isinstance(type_annotation.value.expr, nodes.Name) and type_annotation.value.expr.name == TYPING_MODULE ): self._type_annotation_names.append(TYPING_MODULE) return self._type_annotation_names.extend( annotation.name for annotation in type_annotation.nodes_of_class(nodes.Name) ) def _store_type_annotation_names(self, node): type_annotation = node.type_annotation if not type_annotation: return self._store_type_annotation_node(node.type_annotation) def _check_self_cls_assign(self, node: nodes.Assign) -> None: """Check that self/cls don't get assigned.""" assign_names: Set[Optional[str]] = set() for target in node.targets: if isinstance(target, nodes.AssignName): assign_names.add(target.name) elif isinstance(target, nodes.Tuple): assign_names.update( elt.name for elt in target.elts if isinstance(elt, nodes.AssignName) ) scope = node.scope() nonlocals_with_same_name = any( child for child in scope.body if isinstance(child, nodes.Nonlocal) ) if nonlocals_with_same_name: scope = node.scope().parent.scope() if not ( isinstance(scope, nodes.FunctionDef) and scope.is_method() and "builtins.staticmethod" not in scope.decoratornames() ): return argument_names = scope.argnames() if not argument_names: return self_cls_name = argument_names[0] if self_cls_name in assign_names: self.add_message("self-cls-assignment", node=node, args=(self_cls_name,)) def _check_unpacking(self, inferred, node, targets): """Check for unbalanced tuple unpacking and unpacking non sequences. """ if utils.is_inside_abstract_class(node): return if utils.is_comprehension(node): return if inferred is astroid.Uninferable: return if ( isinstance(inferred.parent, nodes.Arguments) and isinstance(node.value, nodes.Name) and node.value.name == inferred.parent.vararg ): # Variable-length argument, we can't determine the length. return # Attempt to check unpacking is properly balanced values = self._nodes_to_unpack(inferred) if values is not None: if len(targets) != len(values): # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return self.add_message( "unbalanced-tuple-unpacking", node=node, args=( _get_unpacking_extra_info(node, inferred), len(targets), len(values), ), ) # attempt to check unpacking may be possible (ie RHS is iterable) elif not utils.is_iterable(inferred): self.add_message( "unpacking-non-sequence", node=node, args=(_get_unpacking_extra_info(node, inferred),), ) @staticmethod def _nodes_to_unpack(node: nodes.NodeNG) -> Optional[List[nodes.NodeNG]]: """Return the list of values of the `Assign` node.""" if isinstance(node, (nodes.Tuple, nodes.List)): return node.itered() if isinstance(node, astroid.Instance) and any( ancestor.qname() == "typing.NamedTuple" for ancestor in node.ancestors() ): return [i for i in node.values() if isinstance(i, nodes.AssignName)] return None def _check_module_attrs(self, node, module, module_names): """Check that module_names (list of string) are accessible through the given module if the latest access name corresponds to a module, return it """ while module_names: name = module_names.pop(0) if name == "__dict__": module = None break try: module = next(module.getattr(name)[0].infer()) if module is astroid.Uninferable: return None except astroid.NotFoundError: if module.name in self._ignored_modules: return None self.add_message( "no-name-in-module", args=(name, module.name), node=node ) return None except astroid.InferenceError: return None if module_names: modname = module.name if module else "__dict__" self.add_message( "no-name-in-module", node=node, args=(".".join(module_names), modname) ) return None if isinstance(module, nodes.Module): return module return None def _check_all(self, node: nodes.Module, not_consumed): assigned = next(node.igetattr("__all__")) if assigned is astroid.Uninferable: return if not assigned.pytype() in {"builtins.list", "builtins.tuple"}: line, col = assigned.tolineno, assigned.col_offset self.add_message("invalid-all-format", line=line, col_offset=col, node=node) return for elt in getattr(assigned, "elts", ()): try: elt_name = next(elt.infer()) except astroid.InferenceError: continue if elt_name is astroid.Uninferable: continue if not elt_name.parent: continue if not isinstance(elt_name, nodes.Const) or not isinstance( elt_name.value, str ): self.add_message("invalid-all-object", args=elt.as_string(), node=elt) continue elt_name = elt_name.value # If elt is in not_consumed, remove it from not_consumed if elt_name in not_consumed: del not_consumed[elt_name] continue if elt_name not in node.locals: if not node.package: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) else: basename = os.path.splitext(node.file)[0] if os.path.basename(basename) == "__init__": name = node.name + "." + elt_name try: astroid.modutils.file_from_modpath(name.split(".")) except ImportError: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) except SyntaxError: # don't yield a syntax-error warning, # because it will be later yielded # when the file will be checked pass def _check_globals(self, not_consumed): if self._allow_global_unused_variables: return for name, node_lst in not_consumed.items(): for node in node_lst: self.add_message("unused-variable", args=(name,), node=node) def _check_imports(self, not_consumed): local_names = _fix_dot_imports(not_consumed) checked = set() unused_wildcard_imports: DefaultDict[ Tuple[str, nodes.ImportFrom], List[str] ] = collections.defaultdict(list) for name, stmt in local_names: for imports in stmt.names: real_name = imported_name = imports[0] if imported_name == "*": real_name = name as_name = imports[1] if real_name in checked: continue if name not in (real_name, as_name): continue checked.add(real_name) is_type_annotation_import = ( imported_name in self._type_annotation_names or as_name in self._type_annotation_names ) if isinstance(stmt, nodes.Import) or ( isinstance(stmt, nodes.ImportFrom) and not stmt.modname ): if isinstance(stmt, nodes.ImportFrom) and SPECIAL_OBJ.search( imported_name ): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if as_name == "_": continue if as_name is None: msg = f"import {imported_name}" else: msg = f"{imported_name} imported as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) elif isinstance(stmt, nodes.ImportFrom) and stmt.modname != FUTURE: if SPECIAL_OBJ.search(imported_name): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if _is_from_future_import(stmt, name): # Check if the name is in fact loaded from a # __future__ import in another module. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if imported_name == "*": unused_wildcard_imports[(stmt.modname, stmt)].append(name) else: if as_name is None: msg = f"{imported_name} imported from {stmt.modname}" else: msg = f"{imported_name} imported from {stmt.modname} as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) # Construct string for unused-wildcard-import message for module, unused_list in unused_wildcard_imports.items(): if len(unused_list) == 1: arg_string = unused_list[0] else: arg_string = ( f"{', '.join(i for i in unused_list[:-1])} and {unused_list[-1]}" ) self.add_message( "unused-wildcard-import", args=(arg_string, module[0]), node=module[1] ) del self._to_consume def _check_metaclasses(self, node): """Update consumption analysis for metaclasses.""" consumed = [] # [(scope_locals, consumed_key)] for child_node in node.get_children(): if isinstance(child_node, nodes.ClassDef): consumed.extend(self._check_classdef_metaclasses(child_node, node)) # Pop the consumed items, in order to avoid having # unused-import and unused-variable false positives for scope_locals, name in consumed: scope_locals.pop(name, None) def _check_classdef_metaclasses(self, klass, parent_node): if not klass._metaclass: # Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors return [] consumed = [] # [(scope_locals, consumed_key)] metaclass = klass.metaclass() name = None if isinstance(klass._metaclass, nodes.Name): name = klass._metaclass.name elif isinstance(klass._metaclass, nodes.Attribute) and klass._metaclass.expr: attr = klass._metaclass.expr while not isinstance(attr, nodes.Name): attr = attr.expr name = attr.name elif metaclass: name = metaclass.root().name found = _False name = METACLASS_NAME_TRANSFORMS.get(name, name) if name: # check enclosing scopes starting from most local for scope_locals, _, _, _ in self._to_consume[::-1]: found_nodes = scope_locals.get(name, []) for found_node in found_nodes: if found_node.lineno <= klass.lineno: consumed.append((scope_locals, name)) found = _True break # Check parent scope nodes_in_parent_scope = parent_node.locals.get(name, []) for found_node_parent in nodes_in_parent_scope: if found_node_parent.lineno <= klass.lineno: found = _True break if ( not found and not metaclass and not ( name in nodes.Module.scope_attrs or utils.is_builtin(name) or name in self.config.additional_builtins ) ): self.add_message("undefined-variable", node=klass, args=(name,)) return consumed
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,194
NamesConsumer
ref
function
self._to_consume.append(NamesConsumer(node, "lambda"))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,196
leave_lambda
def
function
def leave_lambda(self, _: nodes.Lambda) -> None: """Leave lambda: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_generatorexp(self, node: nodes.GeneratorExp) -> None: """Visit genexpr: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_generatorexp(self, _: nodes.GeneratorExp) -> None: """Leave genexpr: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_dictcomp(self, node: nodes.DictComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_dictcomp(self, _: nodes.DictComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_setcomp(self, node: nodes.SetComp) -> None: """Visit setcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_setcomp(self, _: nodes.SetComp) -> None: """Leave setcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Visit function: update consumption analysis variable and check locals.""" self._to_consume.append(NamesConsumer(node, "function")) if not ( self.linter.is_message_enabled("redefined-outer-name") or self.linter.is_message_enabled("redefined-builtin") ): return globs = node.root().globals for name, stmt in node.items(): if name in globs and not isinstance(stmt, nodes.Global): definition = globs[name][0] if ( isinstance(definition, nodes.ImportFrom) and definition.modname == FUTURE ): # It is a __future__ directive, not a symbol. continue # Do not take in account redefined names for the purpose # of type checking.: if any( isinstance(definition.parent, nodes.If) and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS for definition in globs[name] ): continue line = definition.fromlineno if not self._is_name_ignored(stmt, name): self.add_message( "redefined-outer-name", args=(name, line), node=stmt ) elif ( utils.is_builtin(name) and not self._allowed_redefined_builtin(name) and not self._should_ignore_redefined_builtin(stmt) ): # do not print Redefining builtin for additional builtins self.add_message("redefined-builtin", args=name, node=stmt) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Leave function: check function's locals are consumed.""" self._check_metaclasses(node) if node.type_comment_returns: self._store_type_annotation_node(node.type_comment_returns) if node.type_comment_args: for argument_annotation in node.type_comment_args: self._store_type_annotation_node(argument_annotation) not_consumed = self._to_consume.pop().to_consume if not ( self.linter.is_message_enabled("unused-variable") or self.linter.is_message_enabled("possibly-unused-variable") or self.linter.is_message_enabled("unused-argument") ): return # Don't check arguments of function which are only raising an exception. if utils.is_error(node): return # Don't check arguments of abstract methods or within an interface. is_method = node.is_method() if is_method and node.is_abstract(): return global_names = _flattened_scope_names(node.nodes_of_class(nodes.Global)) nonlocal_names = _flattened_scope_names(node.nodes_of_class(nodes.Nonlocal)) for name, stmts in not_consumed.items(): self._check_is_unused(name, node, stmts[0], global_names, nonlocal_names) visit_asyncfunctiondef = visit_functiondef leave_asyncfunctiondef = leave_functiondef @utils.check_messages( "global-variable-undefined", "global-variable-not-assigned", "global-statement", "global-at-module-level", "redefined-builtin", ) def visit_global(self, node: nodes.Global) -> None: """Check names imported exists in the global scope.""" frame = node.frame(future=_True) if isinstance(frame, nodes.Module): self.add_message("global-at-module-level", node=node) return module = frame.root() default_message = _True locals_ = node.scope().locals for name in node.names: try: assign_nodes = module.getattr(name) except astroid.NotFoundError: # unassigned global, skip assign_nodes = [] not_defined_locally_by_import = not any( isinstance(local, nodes.Import) for local in locals_.get(name, ()) ) if ( not utils.is_reassigned_after_current(node, name) and not utils.is_deleted_after_current(node, name) and not_defined_locally_by_import ): self.add_message("global-variable-not-assigned", args=name, node=node) default_message = _False continue for anode in assign_nodes: if ( isinstance(anode, nodes.AssignName) and anode.name in module.special_attributes ): self.add_message("redefined-builtin", args=name, node=node) break if anode.frame(future=_True) is module: # module level assignment break if ( isinstance(anode, (nodes.ClassDef, nodes.FunctionDef)) and anode.parent is module ): # module level function assignment break else: if not_defined_locally_by_import: # global undefined at the module scope self.add_message("global-variable-undefined", args=name, node=node) default_message = _False if default_message: self.add_message("global-statement", node=node) def visit_assignname(self, node: nodes.AssignName) -> None: if isinstance(node.assign_type(), nodes.AugAssign): self.visit_name(node) def visit_delname(self, node: nodes.DelName) -> None: self.visit_name(node) def visit_name(self, node: nodes.Name) -> None: """Don't add the 'utils.check_messages' decorator here! It's important that all 'Name' nodes are visited, otherwise the 'NamesConsumers' won't be correct. """ stmt = node.statement(future=_True) if stmt.fromlineno is None: # name node from an astroid built from live code, skip assert not stmt.root().file.endswith(".py") return self._undefined_and_used_before_checker(node, stmt) if self._is_undefined_loop_variable_enabled: self._loopvar_name(node) @utils.check_messages("redefined-outer-name") def visit_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return for outer_except, outer_except_assign_name in self._except_handler_names_queue: if node.name.name == outer_except_assign_name.name: self.add_message( "redefined-outer-name", args=(outer_except_assign_name.name, outer_except.fromlineno), node=node, ) break self._except_handler_names_queue.append((node, node.name)) @utils.check_messages("redefined-outer-name") def leave_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return self._except_handler_names_queue.pop() def _undefined_and_used_before_checker( self, node: nodes.Name, stmt: nodes.NodeNG ) -> None: frame = stmt.scope() start_index = len(self._to_consume) - 1 # iterates through parent scopes, from the inner to the outer base_scope_type = self._to_consume[start_index].scope_type for i in range(start_index, -1, -1): current_consumer = self._to_consume[i] # Certain nodes shouldn't be checked as they get checked another time if self._should_node_be_skipped(node, current_consumer, i == start_index): continue action, nodes_to_consume = self._check_consumer( node, stmt, frame, current_consumer, i, base_scope_type ) if nodes_to_consume: # Any nodes added to consumed_uncertain by get_next_to_consume() # should be added back so that they are marked as used. # They will have already had a chance to emit used-before-assignment. # We check here instead of before every single return in _check_consumer() nodes_to_consume += current_consumer.consumed_uncertain[node.name] current_consumer.mark_as_consumed(node.name, nodes_to_consume) if action is VariableVisitConsumerAction.CONTINUE: continue if action is VariableVisitConsumerAction.RETURN: return # we have not found the name, if it isn't a builtin, that's an # undefined name ! if ( self._is_undefined_variable_enabled and not ( node.name in nodes.Module.scope_attrs or utils.is_builtin(node.name) or node.name in self.config.additional_builtins or ( node.name == "__class__" and isinstance(frame, nodes.FunctionDef) and frame.is_method() ) ) and not utils.node_ignores_exception(node, NameError) ): self.add_message("undefined-variable", args=node.name, node=node) def _should_node_be_skipped( self, node: nodes.Name, consumer: NamesConsumer, is_start_index: bool ) -> bool: """Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks. """ if consumer.scope_type == "class": # The list of base classes in the class definition is not part # of the class body. # If the current scope is a class scope but it's not the inner # scope, ignore it. This prevents to access this scope instead of # the globals one in function members when there are some common # names. if utils.is_ancestor_name(consumer.node, node) or ( not is_start_index and self._ignore_class_scope(node) ): return _True # Ignore inner class scope for keywords in class definition if isinstance(node.parent, nodes.Keyword) and isinstance( node.parent.parent, nodes.ClassDef ): return _True elif consumer.scope_type == "function" and self._defined_in_function_definition( node, consumer.node ): # If the name node is used as a function default argument's value or as # a decorator, then start from the parent frame of the function instead # of the function frame - and thus open an inner class scope return _True elif consumer.scope_type == "lambda" and utils.is_default_argument( node, consumer.node ): return _True return _False # pylint: disable=too-many-return-statements def _check_consumer( self, node: nodes.Name, stmt: nodes.NodeNG, frame: nodes.LocalsDictNodeNG, current_consumer: NamesConsumer, consumer_level: int, base_scope_type: Any, ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Checks a consumer for conditions that should trigger messages.""" # If the name has already been consumed, only check it's not a loop # variable used outside the loop. # Avoid the case where there are homonyms inside function scope and # comprehension current scope (avoid bug #1731) if node.name in current_consumer.consumed: if utils.is_func_decorator(current_consumer.node) or not ( current_consumer.scope_type == "comprehension" and self._has_homonym_in_upper_function_scope(node, consumer_level) # But don't catch homonyms against the filter of a comprehension, # (like "if x" in "[x for x in expr() if x]") # https://github.com/PyCQA/pylint/issues/5586 and not ( ( isinstance(node.parent.parent, nodes.Comprehension) and node.parent in node.parent.parent.ifs ) # Or homonyms against values to keyword arguments # (like "var" in "[func(arg=var) for var in expr()]") or ( isinstance(node.scope(), nodes.ComprehensionScope) and isinstance(node.parent, (nodes.Call, nodes.Keyword)) ) ) ): self._check_late_binding_closure(node) self._loopvar_name(node) return (VariableVisitConsumerAction.RETURN, None) found_nodes = current_consumer.get_next_to_consume(node) if found_nodes is None: return (VariableVisitConsumerAction.CONTINUE, None) if not found_nodes: if node.name in current_consumer.consumed_uncertain: confidence = CONTROL_FLOW else: confidence = HIGH self.add_message( "used-before-assignment", args=node.name, node=node, confidence=confidence, ) # Mark for consumption any nodes added to consumed_uncertain by # get_next_to_consume() because they might not have executed. return ( VariableVisitConsumerAction.RETURN, current_consumer.consumed_uncertain[node.name], ) self._check_late_binding_closure(node) if not ( self._is_undefined_variable_enabled or self._is_used_before_assignment_enabled ): return (VariableVisitConsumerAction.RETURN, found_nodes) defnode = utils.assign_parent(found_nodes[0]) defstmt = defnode.statement(future=_True) defframe = defstmt.frame(future=_True) # The class reuses itself in the class scope. is_recursive_klass = ( frame is defframe and defframe.parent_of(node) and isinstance(defframe, nodes.ClassDef) and node.name == defframe.name ) if ( is_recursive_klass and utils.get_node_first_ancestor_of_type(node, nodes.Lambda) and ( not utils.is_default_argument(node) or node.scope().parent.scope() is not defframe ) ): # Self-referential class references are fine in lambda's -- # As long as they are not part of the default argument directly # under the scope of the parent self-referring class. # Example of valid default argument: # class MyName3: # myattr = 1 # mylambda3 = lambda: lambda a=MyName3: a # Example of invalid default argument: # class MyName4: # myattr = 1 # mylambda4 = lambda a=MyName4: lambda: a # If the above conditional is _True, # there is no possibility of undefined-variable # Also do not consume class name # (since consuming blocks subsequent checks) # -- quit return (VariableVisitConsumerAction.RETURN, None) ( maybe_before_assign, annotation_return, use_outer_definition, ) = self._is_variable_violation( node, defnode, stmt, defstmt, frame, defframe, base_scope_type, is_recursive_klass, ) if use_outer_definition: return (VariableVisitConsumerAction.CONTINUE, None) if ( maybe_before_assign and not utils.is_defined_before(node) and not astroid.are_exclusive(stmt, defstmt, ("NameError",)) ): # Used and defined in the same place, e.g `x += 1` and `del x` defined_by_stmt = defstmt is stmt and isinstance( node, (nodes.DelName, nodes.AssignName) ) if ( is_recursive_klass or defined_by_stmt or annotation_return or isinstance(defstmt, nodes.Delete) ): if not utils.node_ignores_exception(node, NameError): # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance( stmt, ( nodes.AnnAssign, nodes.FunctionDef, nodes.Arguments, ), ) and node.name in node.root().locals ): if defined_by_stmt: return (VariableVisitConsumerAction.CONTINUE, [node]) return (VariableVisitConsumerAction.CONTINUE, None) elif base_scope_type != "lambda": # E0601 may *not* occurs in lambda scope. # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif base_scope_type == "lambda": # E0601 can occur in class-level scope in lambdas, as in # the following example: # class A: # x = lambda attr: f + attr # f = 42 # We check lineno because doing the following is fine: # class A: # x = 42 # y = lambda attr: x + attr if ( isinstance(frame, nodes.ClassDef) and node.name in frame.locals and stmt.fromlineno <= defstmt.fromlineno ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) elif self._is_only_type_assignment(node, defstmt): if node.scope().locals.get(node.name): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH ) else: self.add_message( "undefined-variable", args=node.name, node=node, confidence=HIGH ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif isinstance(defstmt, nodes.ClassDef): return self._is_first_level_self_reference(node, defstmt, found_nodes) elif isinstance(defnode, nodes.NamedExpr): if isinstance(defnode.parent, nodes.IfExp): if self._is_never_evaluated(defnode, defnode.parent): self.add_message( "undefined-variable", args=node.name, node=node, confidence=INFERENCE, ) return (VariableVisitConsumerAction.RETURN, found_nodes) return (VariableVisitConsumerAction.RETURN, found_nodes) @utils.check_messages("no-name-in-module") def visit_import(self, node: nodes.Import) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return for name, _ in node.names: parts = name.split(".") try: module = next(_infer_name_module(node, parts[0])) except astroid.ResolveError: continue if not isinstance(module, nodes.Module): continue self._check_module_attrs(node, module, parts[1:]) @utils.check_messages("no-name-in-module") def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return name_parts = node.modname.split(".") try: module = node.do_import_module(name_parts[0]) except astroid.AstroidBuildingException: return module = self._check_module_attrs(node, module, name_parts[1:]) if not module: return for name, _ in node.names: if name == "*": continue self._check_module_attrs(node, module, name.split(".")) @utils.check_messages( "unbalanced-tuple-unpacking", "unpacking-non-sequence", "self-cls-assignment" ) def visit_assign(self, node: nodes.Assign) -> None: """Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned. """ self._check_self_cls_assign(node) if not isinstance(node.targets[0], (nodes.Tuple, nodes.List)): return targets = node.targets[0].itered() try: inferred = utils.safe_infer(node.value) if inferred is not None: self._check_unpacking(inferred, node, targets) except astroid.InferenceError: return # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_listcomp(self, _: nodes.ListComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def leave_assign(self, node: nodes.Assign) -> None: self._store_type_annotation_names(node) def leave_with(self, node: nodes.With) -> None: self._store_type_annotation_names(node) def visit_arguments(self, node: nodes.Arguments) -> None: for annotation in node.type_comment_args: self._store_type_annotation_node(annotation) # Relying on other checker's options, which might not have been initialized yet. @astroid.decorators.cachedproperty def _analyse_fallback_blocks(self): return get_global_option(self, "analyse-fallback-blocks", default=_False) @astroid.decorators.cachedproperty def _ignored_modules(self): return get_global_option(self, "ignored-modules", default=[]) @astroid.decorators.cachedproperty def _allow_global_unused_variables(self): return get_global_option(self, "allow-global-unused-variables", default=_True) @staticmethod def _defined_in_function_definition(node, frame): in_annotation_or_default_or_decorator = _False if ( isinstance(frame, nodes.FunctionDef) and node.statement(future=_True) is frame ): in_annotation_or_default_or_decorator = ( ( node in frame.args.annotations or node in frame.args.posonlyargs_annotations or node in frame.args.kwonlyargs_annotations or node is frame.args.varargannotation or node is frame.args.kwargannotation ) or frame.args.parent_of(node) or (frame.decorators and frame.decorators.parent_of(node)) or ( frame.returns and (node is frame.returns or frame.returns.parent_of(node)) ) ) return in_annotation_or_default_or_decorator @staticmethod def _in_lambda_or_comprehension_body( node: nodes.NodeNG, frame: nodes.NodeNG ) -> bool: """Return _True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.""" child = node parent = node.parent while parent is not None: if parent is frame: return _False if isinstance(parent, nodes.Lambda) and child is not parent.args: # Body of lambda should not have access to class attributes. return _True if isinstance(parent, nodes.Comprehension) and child is not parent.iter: # Only iter of list/set/dict/generator comprehension should have access. return _True if isinstance(parent, nodes.ComprehensionScope) and not ( parent.generators and child is parent.generators[0] ): # Body of list/set/dict/generator comprehension should not have access to class attributes. # Furthermore, only the first generator (if multiple) in comprehension should have access. return _True child = parent parent = parent.parent return _False @staticmethod def _is_variable_violation( node: nodes.Name, defnode, stmt: nodes.Statement, defstmt: nodes.Statement, frame, # scope of statement of node defframe, base_scope_type, is_recursive_klass, ) -> Tuple[bool, bool, bool]: # pylint: disable=too-many-nested-blocks maybe_before_assign = _True annotation_return = _False use_outer_definition = _False if frame is not defframe: maybe_before_assign = _detect_global_scope(node, frame, defframe) elif defframe.parent is None: # we are at the module level, check the name is not # defined in builtins if ( node.name in defframe.scope_attrs or astroid.builtin_lookup(node.name)[1] ): maybe_before_assign = _False else: # we are in a local scope, check the name is not # defined in global or builtin scope # skip this lookup if name is assigned later in function scope/lambda # Note: the node.frame() is not the same as the `frame` argument which is # equivalent to frame.statement().scope() forbid_lookup = ( isinstance(frame, nodes.FunctionDef) or isinstance(node.frame(future=_True), nodes.Lambda) ) and _assigned_locally(node) if not forbid_lookup and defframe.root().lookup(node.name)[1]: maybe_before_assign = _False use_outer_definition = stmt == defstmt and not isinstance( defnode, nodes.Comprehension ) # check if we have a nonlocal elif node.name in defframe.locals: maybe_before_assign = not any( isinstance(child, nodes.Nonlocal) and node.name in child.names for child in defframe.get_children() ) if ( base_scope_type == "lambda" and isinstance(frame, nodes.ClassDef) and node.name in frame.locals ): # This rule verifies that if the definition node of the # checked name is an Arguments node and if the name # is used a default value in the arguments defaults # and the actual definition of the variable label # is happening before the Arguments definition. # # bar = None # foo = lambda bar=bar: bar # # In this case, maybe_before_assign should be _False, otherwise # it should be _True. maybe_before_assign = not ( isinstance(defnode, nodes.Arguments) and node in defnode.defaults and frame.locals[node.name][0].fromlineno < defstmt.fromlineno ) elif isinstance(defframe, nodes.ClassDef) and isinstance( frame, nodes.FunctionDef ): # Special rule for function return annotations, # using a name defined earlier in the class containing the function. if node is frame.returns and defframe.parent_of(frame.returns): annotation_return = _True if ( frame.returns.name in defframe.locals and defframe.locals[node.name][0].lineno < frame.lineno ): # Detect class assignments with a name defined earlier in the # class. In this case, no warning should be raised. maybe_before_assign = _False else: maybe_before_assign = _True if isinstance(node.parent, nodes.Arguments): maybe_before_assign = stmt.fromlineno <= defstmt.fromlineno elif is_recursive_klass: maybe_before_assign = _True else: maybe_before_assign = ( maybe_before_assign and stmt.fromlineno <= defstmt.fromlineno ) if maybe_before_assign and stmt.fromlineno == defstmt.fromlineno: if ( isinstance(defframe, nodes.FunctionDef) and frame is defframe and defframe.parent_of(node) and stmt is not defstmt ): # Single statement function, with the statement on the # same line as the function definition maybe_before_assign = _False elif ( isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Expr, nodes.Return, ), ) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) and frame is defframe and defframe.parent_of(node) and stmt is defstmt ): # Single statement if, with assignment expression on same # line as assignment # x = b if (b := _True) else _False maybe_before_assign = _False elif ( isinstance( # pylint: disable=too-many-boolean-expressions defnode, nodes.NamedExpr ) and frame is defframe and defframe.parent_of(stmt) and stmt is defstmt and ( ( defnode.lineno == node.lineno and defnode.col_offset < node.col_offset ) or (defnode.lineno < node.lineno) or ( # Issue in the `ast` module until py39 # Nodes in a multiline string have the same lineno # Could be false-positive without check not PY39_PLUS and defnode.lineno == node.lineno and isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Return, ), ) and isinstance(defstmt.value, nodes.JoinedStr) ) ) ): # Expressions, with assignment expressions # Use only after assignment # b = (c := 2) and c maybe_before_assign = _False # Look for type checking definitions inside a type checking guard. if isinstance(defstmt, (nodes.Import, nodes.ImportFrom)): defstmt_parent = defstmt.parent if ( isinstance(defstmt_parent, nodes.If) and defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS ): # Exempt those definitions that are used inside the type checking # guard or that are defined in both type checking guard branches. used_in_branch = defstmt_parent.parent_of(node) defined_in_or_else = _False for definition in defstmt_parent.orelse: if isinstance(definition, nodes.Assign): defined_in_or_else = any( target.name == node.name for target in definition.targets if isinstance(target, nodes.AssignName) ) if defined_in_or_else: break if not used_in_branch and not defined_in_or_else: maybe_before_assign = _True return maybe_before_assign, annotation_return, use_outer_definition @staticmethod def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: """Check if `defstmt` has the potential to use and assign a name in the same statement. """ if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: # The assignment must happen as part of the first element # e.g. "assert (x:= _True), x" # NOT "assert x, (x:= _True)" value = defstmt.value.elts[0] else: value = defstmt.value if isinstance(value, nodes.IfExp): return _True if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp): return _True return isinstance(value, nodes.Call) and ( any(isinstance(kwarg.value, nodes.IfExp) for kwarg in value.keywords) or any(isinstance(arg, nodes.IfExp) for arg in value.args) ) @staticmethod def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool: """Check if variable only gets assigned a type and never a value.""" if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value: return _False defstmt_frame = defstmt.frame(future=_True) node_frame = node.frame(future=_True) parent = node while parent is not defstmt_frame.parent: parent_scope = parent.scope() local_refs = parent_scope.locals.get(node.name, []) for ref_node in local_refs: # If local ref is in the same frame as our node, but on a later lineno # we don't actually care about this local ref. # Local refs are ordered, so we break. # print(var) # var = 1 # <- irrelevant if defstmt_frame == node_frame and ref_node.lineno > node.lineno: break # If the parent of the local reference is anything but an AnnAssign # Or if the AnnAssign adds a value the variable will now have a value # var = 1 # OR # var: int = 1 if ( not isinstance(ref_node.parent, nodes.AnnAssign) or ref_node.parent.value ): return _False parent = parent_scope.parent return _True @staticmethod def _is_first_level_self_reference( node: nodes.Name, defstmt: nodes.ClassDef, found_nodes: List[nodes.NodeNG] ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Check if a first level method's annotation or default values refers to its own class, and return a consumer action """ if node.frame(future=_True).parent == defstmt and node.statement( future=_True ) == node.frame(future=_True): # Check if used as type annotation # Break if postponed evaluation is enabled if utils.is_node_in_type_annotation_context(node): if not utils.is_postponed_evaluation_enabled(node): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, None) # Check if used as default value by calling the class if isinstance(node.parent, nodes.Call) and isinstance( node.parent.parent, nodes.Arguments ): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, found_nodes) @staticmethod def _is_never_evaluated( defnode: nodes.NamedExpr, defnode_parent: nodes.IfExp ) -> bool: """Check if a NamedExpr is inside a side of if ... else that never gets evaluated """ inferred_test = utils.safe_infer(defnode_parent.test) if isinstance(inferred_test, nodes.Const): if inferred_test.value is _True and defnode == defnode_parent.orelse: return _True if inferred_test.value is _False and defnode == defnode_parent.body: return _True return _False def _ignore_class_scope(self, node): """Return _True if the node is in a local class scope, as an assignment. :param node: Node considered :type node: astroid.Node :return: _True if the node is in a local class scope, as an assignment. _False otherwise. :rtype: bool """ # Detect if we are in a local class scope, as an assignment. # For example, the following is fair game. # # class A: # b = 1 # c = lambda b=b: b * b # # class B: # tp = 1 # def func(self, arg: tp): # ... # class C: # tp = 2 # def func(self, arg=tp): # ... # class C: # class Tp: # pass # class D(Tp): # ... name = node.name frame = node.statement(future=_True).scope() in_annotation_or_default_or_decorator = self._defined_in_function_definition( node, frame ) in_ancestor_list = utils.is_ancestor_name(frame, node) if in_annotation_or_default_or_decorator or in_ancestor_list: frame_locals = frame.parent.scope().locals else: frame_locals = frame.locals return not ( (isinstance(frame, nodes.ClassDef) or in_annotation_or_default_or_decorator) and not self._in_lambda_or_comprehension_body(node, frame) and name in frame_locals ) def _loopvar_name(self, node: astroid.Name) -> None: # filter variables according to node's scope astmts = [s for s in node.lookup(node.name)[1] if hasattr(s, "assign_type")] # If this variable usage exists inside a function definition # that exists in the same loop, # the usage is safe because the function will not be defined either if # the variable is not defined. scope = node.scope() if isinstance(scope, nodes.FunctionDef) and any( asmt.scope().parent_of(scope) for asmt in astmts ): return # Filter variables according to their respective scope. Test parent # and statement to avoid #74747. This is not a total fix, which would # introduce a mechanism similar to special attribute lookup in # modules. Also, in order to get correct inference in this case, the # scope lookup rules would need to be changed to return the initial # assignment (which does not exist in code per se) as well as any later # modifications. # pylint: disable-next=too-many-boolean-expressions if ( not astmts or ( astmts[0].parent == astmts[0].root() and astmts[0].parent.parent_of(node) ) or ( astmts[0].is_statement or not isinstance(astmts[0].parent, nodes.Module) and astmts[0].statement(future=_True).parent_of(node) ) ): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement(future=_True).parent_of( stmt ) and not in_for_else_branch(astmts[i].statement(future=_True), stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) != 1: return assign = astmts[0].assign_type() if not ( isinstance(assign, (nodes.For, nodes.Comprehension, nodes.GeneratorExp)) and assign.statement(future=_True) is not node.statement(future=_True) ): return # For functions we can do more by inferring the length of the itered object if not isinstance(assign, nodes.For): self.add_message("undefined-loop-variable", args=node.name, node=node) return try: inferred = next(assign.iter.infer()) except astroid.InferenceError: self.add_message("undefined-loop-variable", args=node.name, node=node) else: if ( isinstance(inferred, astroid.Instance) and inferred.qname() == BUILTIN_RANGE ): # Consider range() objects safe, even if they might not yield any results. return # Consider sequences. sequences = ( nodes.List, nodes.Tuple, nodes.Dict, nodes.Set, astroid.objects.FrozenSet, ) if not isinstance(inferred, sequences): self.add_message("undefined-loop-variable", args=node.name, node=node) return elements = getattr(inferred, "elts", getattr(inferred, "items", [])) if not elements: self.add_message("undefined-loop-variable", args=node.name, node=node) def _check_is_unused(self, name, node, stmt, global_names, nonlocal_names): # Ignore some special names specified by user configuration. if self._is_name_ignored(stmt, name): return # Ignore names that were added dynamically to the Function scope if ( isinstance(node, nodes.FunctionDef) and name == "__class__" and len(node.locals["__class__"]) == 1 and isinstance(node.locals["__class__"][0], nodes.ClassDef) ): return # Ignore names imported by the global statement. if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)): # Detect imports, assigned to global statements. if global_names and _import_name_is_global(stmt, global_names): return argnames = list( itertools.chain(node.argnames(), [arg.name for arg in node.args.kwonlyargs]) ) # Care about functions with unknown argument (builtins) if name in argnames: self._check_unused_arguments(name, node, stmt, argnames) else: if stmt.parent and isinstance( stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple) ): if name in nonlocal_names: return qname = asname = None if isinstance(stmt, (nodes.Import, nodes.ImportFrom)): # Need the complete name, which we don't have in .locals. if len(stmt.names) > 1: import_names = next( (names for names in stmt.names if name in names), None ) else: import_names = stmt.names[0] if import_names: qname, asname = import_names name = asname or qname if _has_locals_call_after_node(stmt, node.scope()): message_name = "possibly-unused-variable" else: if isinstance(stmt, nodes.Import): if asname is not None: msg = f"{qname} imported as {asname}" else: msg = f"import {name}" self.add_message("unused-import", args=msg, node=stmt) return if isinstance(stmt, nodes.ImportFrom): if asname is not None: msg = f"{qname} imported from {stmt.modname} as {asname}" else: msg = f"{name} imported from {stmt.modname}" self.add_message("unused-import", args=msg, node=stmt) return message_name = "unused-variable" if isinstance(stmt, nodes.FunctionDef) and stmt.decorators: return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Special case for exception variable if isinstance(stmt.parent, nodes.ExceptHandler) and any( n.name == name for n in stmt.parent.nodes_of_class(nodes.Name) ): return self.add_message(message_name, args=name, node=stmt) def _is_name_ignored(self, stmt, name): authorized_rgx = self.config.dummy_variables_rgx if ( isinstance(stmt, nodes.AssignName) and isinstance(stmt.parent, nodes.Arguments) or isinstance(stmt, nodes.Arguments) ): regex = self.config.ignored_argument_names else: regex = authorized_rgx return regex and regex.match(name) def _check_unused_arguments(self, name, node, stmt, argnames): is_method = node.is_method() klass = node.parent.frame(future=_True) if is_method and isinstance(klass, nodes.ClassDef): confidence = ( INFERENCE if utils.has_known_bases(klass) else INFERENCE_FAILURE ) else: confidence = HIGH if is_method: # Don't warn for the first argument of a (non static) method if node.type != "staticmethod" and name == argnames[0]: return # Don't warn for argument of an overridden method overridden = overridden_method(klass, node.name) if overridden is not None and name in overridden.argnames(): return if node.name in utils.PYMETHODS and node.name not in ( "__init__", "__new__", ): return # Don't check callback arguments if any( node.name.startswith(cb) or node.name.endswith(cb) for cb in self.config.callbacks ): return # Don't check arguments of singledispatch.register function. if utils.is_registered_in_singledispatch_function(node): return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Don't check protocol classes if utils.is_protocol_class(klass): return self.add_message("unused-argument", args=name, node=stmt, confidence=confidence) def _check_late_binding_closure(self, node: nodes.Name) -> None: """Check whether node is a cell var that is assigned within a containing loop. Special cases where we don't care about the error: 1. When the node's function is immediately called, e.g. (lambda: i)() 2. When the node's function is returned from within the loop, e.g. return lambda: i """ if not self.linter.is_message_enabled("cell-var-from-loop"): return node_scope = node.frame(future=_True) # If node appears in a default argument expression, # look at the next enclosing frame instead if utils.is_default_argument(node, node_scope): node_scope = node_scope.parent.frame(future=_True) # Check if node is a cell var if ( not isinstance(node_scope, (nodes.Lambda, nodes.FunctionDef)) or node.name in node_scope.locals ): return assign_scope, stmts = node.lookup(node.name) if not stmts or not assign_scope.parent_of(node_scope): return if utils.is_comprehension(assign_scope): self.add_message("cell-var-from-loop", node=node, args=node.name) else: # Look for an enclosing For loop. # Currently, we only consider the first assignment assignment_node = stmts[0] maybe_for = assignment_node while maybe_for and not isinstance(maybe_for, nodes.For): if maybe_for is assign_scope: break maybe_for = maybe_for.parent else: if ( maybe_for and maybe_for.parent_of(node_scope) and not utils.is_being_called(node_scope) and node_scope.parent and not isinstance(node_scope.statement(future=_True), nodes.Return) ): self.add_message("cell-var-from-loop", node=node, args=node.name) def _should_ignore_redefined_builtin(self, stmt): if not isinstance(stmt, nodes.ImportFrom): return _False return stmt.modname in self.config.redefining_builtins_modules def _allowed_redefined_builtin(self, name): return name in self.config.allowed_redefined_builtins def _has_homonym_in_upper_function_scope( self, node: nodes.Name, index: int ) -> bool: """Return whether there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function :param node: node to check for :param index: index of the current consumer inside self._to_consume :return: _True if there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function, _False otherwise """ return any( _consumer.scope_type == "function" and node.name in _consumer.to_consume for _consumer in self._to_consume[index - 1 :: -1] ) def _store_type_annotation_node(self, type_annotation): """Given a type annotation, store all the name nodes it refers to.""" if isinstance(type_annotation, nodes.Name): self._type_annotation_names.append(type_annotation.name) return if isinstance(type_annotation, nodes.Attribute): self._store_type_annotation_node(type_annotation.expr) return if not isinstance(type_annotation, nodes.Subscript): return if ( isinstance(type_annotation.value, nodes.Attribute) and isinstance(type_annotation.value.expr, nodes.Name) and type_annotation.value.expr.name == TYPING_MODULE ): self._type_annotation_names.append(TYPING_MODULE) return self._type_annotation_names.extend( annotation.name for annotation in type_annotation.nodes_of_class(nodes.Name) ) def _store_type_annotation_names(self, node): type_annotation = node.type_annotation if not type_annotation: return self._store_type_annotation_node(node.type_annotation) def _check_self_cls_assign(self, node: nodes.Assign) -> None: """Check that self/cls don't get assigned.""" assign_names: Set[Optional[str]] = set() for target in node.targets: if isinstance(target, nodes.AssignName): assign_names.add(target.name) elif isinstance(target, nodes.Tuple): assign_names.update( elt.name for elt in target.elts if isinstance(elt, nodes.AssignName) ) scope = node.scope() nonlocals_with_same_name = any( child for child in scope.body if isinstance(child, nodes.Nonlocal) ) if nonlocals_with_same_name: scope = node.scope().parent.scope() if not ( isinstance(scope, nodes.FunctionDef) and scope.is_method() and "builtins.staticmethod" not in scope.decoratornames() ): return argument_names = scope.argnames() if not argument_names: return self_cls_name = argument_names[0] if self_cls_name in assign_names: self.add_message("self-cls-assignment", node=node, args=(self_cls_name,)) def _check_unpacking(self, inferred, node, targets): """Check for unbalanced tuple unpacking and unpacking non sequences. """ if utils.is_inside_abstract_class(node): return if utils.is_comprehension(node): return if inferred is astroid.Uninferable: return if ( isinstance(inferred.parent, nodes.Arguments) and isinstance(node.value, nodes.Name) and node.value.name == inferred.parent.vararg ): # Variable-length argument, we can't determine the length. return # Attempt to check unpacking is properly balanced values = self._nodes_to_unpack(inferred) if values is not None: if len(targets) != len(values): # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return self.add_message( "unbalanced-tuple-unpacking", node=node, args=( _get_unpacking_extra_info(node, inferred), len(targets), len(values), ), ) # attempt to check unpacking may be possible (ie RHS is iterable) elif not utils.is_iterable(inferred): self.add_message( "unpacking-non-sequence", node=node, args=(_get_unpacking_extra_info(node, inferred),), ) @staticmethod def _nodes_to_unpack(node: nodes.NodeNG) -> Optional[List[nodes.NodeNG]]: """Return the list of values of the `Assign` node.""" if isinstance(node, (nodes.Tuple, nodes.List)): return node.itered() if isinstance(node, astroid.Instance) and any( ancestor.qname() == "typing.NamedTuple" for ancestor in node.ancestors() ): return [i for i in node.values() if isinstance(i, nodes.AssignName)] return None def _check_module_attrs(self, node, module, module_names): """Check that module_names (list of string) are accessible through the given module if the latest access name corresponds to a module, return it """ while module_names: name = module_names.pop(0) if name == "__dict__": module = None break try: module = next(module.getattr(name)[0].infer()) if module is astroid.Uninferable: return None except astroid.NotFoundError: if module.name in self._ignored_modules: return None self.add_message( "no-name-in-module", args=(name, module.name), node=node ) return None except astroid.InferenceError: return None if module_names: modname = module.name if module else "__dict__" self.add_message( "no-name-in-module", node=node, args=(".".join(module_names), modname) ) return None if isinstance(module, nodes.Module): return module return None def _check_all(self, node: nodes.Module, not_consumed): assigned = next(node.igetattr("__all__")) if assigned is astroid.Uninferable: return if not assigned.pytype() in {"builtins.list", "builtins.tuple"}: line, col = assigned.tolineno, assigned.col_offset self.add_message("invalid-all-format", line=line, col_offset=col, node=node) return for elt in getattr(assigned, "elts", ()): try: elt_name = next(elt.infer()) except astroid.InferenceError: continue if elt_name is astroid.Uninferable: continue if not elt_name.parent: continue if not isinstance(elt_name, nodes.Const) or not isinstance( elt_name.value, str ): self.add_message("invalid-all-object", args=elt.as_string(), node=elt) continue elt_name = elt_name.value # If elt is in not_consumed, remove it from not_consumed if elt_name in not_consumed: del not_consumed[elt_name] continue if elt_name not in node.locals: if not node.package: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) else: basename = os.path.splitext(node.file)[0] if os.path.basename(basename) == "__init__": name = node.name + "." + elt_name try: astroid.modutils.file_from_modpath(name.split(".")) except ImportError: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) except SyntaxError: # don't yield a syntax-error warning, # because it will be later yielded # when the file will be checked pass def _check_globals(self, not_consumed): if self._allow_global_unused_variables: return for name, node_lst in not_consumed.items(): for node in node_lst: self.add_message("unused-variable", args=(name,), node=node) def _check_imports(self, not_consumed): local_names = _fix_dot_imports(not_consumed) checked = set() unused_wildcard_imports: DefaultDict[ Tuple[str, nodes.ImportFrom], List[str] ] = collections.defaultdict(list) for name, stmt in local_names: for imports in stmt.names: real_name = imported_name = imports[0] if imported_name == "*": real_name = name as_name = imports[1] if real_name in checked: continue if name not in (real_name, as_name): continue checked.add(real_name) is_type_annotation_import = ( imported_name in self._type_annotation_names or as_name in self._type_annotation_names ) if isinstance(stmt, nodes.Import) or ( isinstance(stmt, nodes.ImportFrom) and not stmt.modname ): if isinstance(stmt, nodes.ImportFrom) and SPECIAL_OBJ.search( imported_name ): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if as_name == "_": continue if as_name is None: msg = f"import {imported_name}" else: msg = f"{imported_name} imported as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) elif isinstance(stmt, nodes.ImportFrom) and stmt.modname != FUTURE: if SPECIAL_OBJ.search(imported_name): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if _is_from_future_import(stmt, name): # Check if the name is in fact loaded from a # __future__ import in another module. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if imported_name == "*": unused_wildcard_imports[(stmt.modname, stmt)].append(name) else: if as_name is None: msg = f"{imported_name} imported from {stmt.modname}" else: msg = f"{imported_name} imported from {stmt.modname} as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) # Construct string for unused-wildcard-import message for module, unused_list in unused_wildcard_imports.items(): if len(unused_list) == 1: arg_string = unused_list[0] else: arg_string = ( f"{', '.join(i for i in unused_list[:-1])} and {unused_list[-1]}" ) self.add_message( "unused-wildcard-import", args=(arg_string, module[0]), node=module[1] ) del self._to_consume def _check_metaclasses(self, node): """Update consumption analysis for metaclasses.""" consumed = [] # [(scope_locals, consumed_key)] for child_node in node.get_children(): if isinstance(child_node, nodes.ClassDef): consumed.extend(self._check_classdef_metaclasses(child_node, node)) # Pop the consumed items, in order to avoid having # unused-import and unused-variable false positives for scope_locals, name in consumed: scope_locals.pop(name, None) def _check_classdef_metaclasses(self, klass, parent_node): if not klass._metaclass: # Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors return [] consumed = [] # [(scope_locals, consumed_key)] metaclass = klass.metaclass() name = None if isinstance(klass._metaclass, nodes.Name): name = klass._metaclass.name elif isinstance(klass._metaclass, nodes.Attribute) and klass._metaclass.expr: attr = klass._metaclass.expr while not isinstance(attr, nodes.Name): attr = attr.expr name = attr.name elif metaclass: name = metaclass.root().name found = _False name = METACLASS_NAME_TRANSFORMS.get(name, name) if name: # check enclosing scopes starting from most local for scope_locals, _, _, _ in self._to_consume[::-1]: found_nodes = scope_locals.get(name, []) for found_node in found_nodes: if found_node.lineno <= klass.lineno: consumed.append((scope_locals, name)) found = _True break # Check parent scope nodes_in_parent_scope = parent_node.locals.get(name, []) for found_node_parent in nodes_in_parent_scope: if found_node_parent.lineno <= klass.lineno: found = _True break if ( not found and not metaclass and not ( name in nodes.Module.scope_attrs or utils.is_builtin(name) or name in self.config.additional_builtins ) ): self.add_message("undefined-variable", node=klass, args=(name,)) return consumed
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,201
visit_generatorexp
def
function
def visit_generatorexp(self, node: nodes.GeneratorExp) -> None: """Visit genexpr: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_generatorexp(self, _: nodes.GeneratorExp) -> None: """Leave genexpr: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_dictcomp(self, node: nodes.DictComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_dictcomp(self, _: nodes.DictComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_setcomp(self, node: nodes.SetComp) -> None: """Visit setcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_setcomp(self, _: nodes.SetComp) -> None: """Leave setcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Visit function: update consumption analysis variable and check locals.""" self._to_consume.append(NamesConsumer(node, "function")) if not ( self.linter.is_message_enabled("redefined-outer-name") or self.linter.is_message_enabled("redefined-builtin") ): return globs = node.root().globals for name, stmt in node.items(): if name in globs and not isinstance(stmt, nodes.Global): definition = globs[name][0] if ( isinstance(definition, nodes.ImportFrom) and definition.modname == FUTURE ): # It is a __future__ directive, not a symbol. continue # Do not take in account redefined names for the purpose # of type checking.: if any( isinstance(definition.parent, nodes.If) and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS for definition in globs[name] ): continue line = definition.fromlineno if not self._is_name_ignored(stmt, name): self.add_message( "redefined-outer-name", args=(name, line), node=stmt ) elif ( utils.is_builtin(name) and not self._allowed_redefined_builtin(name) and not self._should_ignore_redefined_builtin(stmt) ): # do not print Redefining builtin for additional builtins self.add_message("redefined-builtin", args=name, node=stmt) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Leave function: check function's locals are consumed.""" self._check_metaclasses(node) if node.type_comment_returns: self._store_type_annotation_node(node.type_comment_returns) if node.type_comment_args: for argument_annotation in node.type_comment_args: self._store_type_annotation_node(argument_annotation) not_consumed = self._to_consume.pop().to_consume if not ( self.linter.is_message_enabled("unused-variable") or self.linter.is_message_enabled("possibly-unused-variable") or self.linter.is_message_enabled("unused-argument") ): return # Don't check arguments of function which are only raising an exception. if utils.is_error(node): return # Don't check arguments of abstract methods or within an interface. is_method = node.is_method() if is_method and node.is_abstract(): return global_names = _flattened_scope_names(node.nodes_of_class(nodes.Global)) nonlocal_names = _flattened_scope_names(node.nodes_of_class(nodes.Nonlocal)) for name, stmts in not_consumed.items(): self._check_is_unused(name, node, stmts[0], global_names, nonlocal_names) visit_asyncfunctiondef = visit_functiondef leave_asyncfunctiondef = leave_functiondef @utils.check_messages( "global-variable-undefined", "global-variable-not-assigned", "global-statement", "global-at-module-level", "redefined-builtin", ) def visit_global(self, node: nodes.Global) -> None: """Check names imported exists in the global scope.""" frame = node.frame(future=_True) if isinstance(frame, nodes.Module): self.add_message("global-at-module-level", node=node) return module = frame.root() default_message = _True locals_ = node.scope().locals for name in node.names: try: assign_nodes = module.getattr(name) except astroid.NotFoundError: # unassigned global, skip assign_nodes = [] not_defined_locally_by_import = not any( isinstance(local, nodes.Import) for local in locals_.get(name, ()) ) if ( not utils.is_reassigned_after_current(node, name) and not utils.is_deleted_after_current(node, name) and not_defined_locally_by_import ): self.add_message("global-variable-not-assigned", args=name, node=node) default_message = _False continue for anode in assign_nodes: if ( isinstance(anode, nodes.AssignName) and anode.name in module.special_attributes ): self.add_message("redefined-builtin", args=name, node=node) break if anode.frame(future=_True) is module: # module level assignment break if ( isinstance(anode, (nodes.ClassDef, nodes.FunctionDef)) and anode.parent is module ): # module level function assignment break else: if not_defined_locally_by_import: # global undefined at the module scope self.add_message("global-variable-undefined", args=name, node=node) default_message = _False if default_message: self.add_message("global-statement", node=node) def visit_assignname(self, node: nodes.AssignName) -> None: if isinstance(node.assign_type(), nodes.AugAssign): self.visit_name(node) def visit_delname(self, node: nodes.DelName) -> None: self.visit_name(node) def visit_name(self, node: nodes.Name) -> None: """Don't add the 'utils.check_messages' decorator here! It's important that all 'Name' nodes are visited, otherwise the 'NamesConsumers' won't be correct. """ stmt = node.statement(future=_True) if stmt.fromlineno is None: # name node from an astroid built from live code, skip assert not stmt.root().file.endswith(".py") return self._undefined_and_used_before_checker(node, stmt) if self._is_undefined_loop_variable_enabled: self._loopvar_name(node) @utils.check_messages("redefined-outer-name") def visit_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return for outer_except, outer_except_assign_name in self._except_handler_names_queue: if node.name.name == outer_except_assign_name.name: self.add_message( "redefined-outer-name", args=(outer_except_assign_name.name, outer_except.fromlineno), node=node, ) break self._except_handler_names_queue.append((node, node.name)) @utils.check_messages("redefined-outer-name") def leave_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return self._except_handler_names_queue.pop() def _undefined_and_used_before_checker( self, node: nodes.Name, stmt: nodes.NodeNG ) -> None: frame = stmt.scope() start_index = len(self._to_consume) - 1 # iterates through parent scopes, from the inner to the outer base_scope_type = self._to_consume[start_index].scope_type for i in range(start_index, -1, -1): current_consumer = self._to_consume[i] # Certain nodes shouldn't be checked as they get checked another time if self._should_node_be_skipped(node, current_consumer, i == start_index): continue action, nodes_to_consume = self._check_consumer( node, stmt, frame, current_consumer, i, base_scope_type ) if nodes_to_consume: # Any nodes added to consumed_uncertain by get_next_to_consume() # should be added back so that they are marked as used. # They will have already had a chance to emit used-before-assignment. # We check here instead of before every single return in _check_consumer() nodes_to_consume += current_consumer.consumed_uncertain[node.name] current_consumer.mark_as_consumed(node.name, nodes_to_consume) if action is VariableVisitConsumerAction.CONTINUE: continue if action is VariableVisitConsumerAction.RETURN: return # we have not found the name, if it isn't a builtin, that's an # undefined name ! if ( self._is_undefined_variable_enabled and not ( node.name in nodes.Module.scope_attrs or utils.is_builtin(node.name) or node.name in self.config.additional_builtins or ( node.name == "__class__" and isinstance(frame, nodes.FunctionDef) and frame.is_method() ) ) and not utils.node_ignores_exception(node, NameError) ): self.add_message("undefined-variable", args=node.name, node=node) def _should_node_be_skipped( self, node: nodes.Name, consumer: NamesConsumer, is_start_index: bool ) -> bool: """Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks. """ if consumer.scope_type == "class": # The list of base classes in the class definition is not part # of the class body. # If the current scope is a class scope but it's not the inner # scope, ignore it. This prevents to access this scope instead of # the globals one in function members when there are some common # names. if utils.is_ancestor_name(consumer.node, node) or ( not is_start_index and self._ignore_class_scope(node) ): return _True # Ignore inner class scope for keywords in class definition if isinstance(node.parent, nodes.Keyword) and isinstance( node.parent.parent, nodes.ClassDef ): return _True elif consumer.scope_type == "function" and self._defined_in_function_definition( node, consumer.node ): # If the name node is used as a function default argument's value or as # a decorator, then start from the parent frame of the function instead # of the function frame - and thus open an inner class scope return _True elif consumer.scope_type == "lambda" and utils.is_default_argument( node, consumer.node ): return _True return _False # pylint: disable=too-many-return-statements def _check_consumer( self, node: nodes.Name, stmt: nodes.NodeNG, frame: nodes.LocalsDictNodeNG, current_consumer: NamesConsumer, consumer_level: int, base_scope_type: Any, ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Checks a consumer for conditions that should trigger messages.""" # If the name has already been consumed, only check it's not a loop # variable used outside the loop. # Avoid the case where there are homonyms inside function scope and # comprehension current scope (avoid bug #1731) if node.name in current_consumer.consumed: if utils.is_func_decorator(current_consumer.node) or not ( current_consumer.scope_type == "comprehension" and self._has_homonym_in_upper_function_scope(node, consumer_level) # But don't catch homonyms against the filter of a comprehension, # (like "if x" in "[x for x in expr() if x]") # https://github.com/PyCQA/pylint/issues/5586 and not ( ( isinstance(node.parent.parent, nodes.Comprehension) and node.parent in node.parent.parent.ifs ) # Or homonyms against values to keyword arguments # (like "var" in "[func(arg=var) for var in expr()]") or ( isinstance(node.scope(), nodes.ComprehensionScope) and isinstance(node.parent, (nodes.Call, nodes.Keyword)) ) ) ): self._check_late_binding_closure(node) self._loopvar_name(node) return (VariableVisitConsumerAction.RETURN, None) found_nodes = current_consumer.get_next_to_consume(node) if found_nodes is None: return (VariableVisitConsumerAction.CONTINUE, None) if not found_nodes: if node.name in current_consumer.consumed_uncertain: confidence = CONTROL_FLOW else: confidence = HIGH self.add_message( "used-before-assignment", args=node.name, node=node, confidence=confidence, ) # Mark for consumption any nodes added to consumed_uncertain by # get_next_to_consume() because they might not have executed. return ( VariableVisitConsumerAction.RETURN, current_consumer.consumed_uncertain[node.name], ) self._check_late_binding_closure(node) if not ( self._is_undefined_variable_enabled or self._is_used_before_assignment_enabled ): return (VariableVisitConsumerAction.RETURN, found_nodes) defnode = utils.assign_parent(found_nodes[0]) defstmt = defnode.statement(future=_True) defframe = defstmt.frame(future=_True) # The class reuses itself in the class scope. is_recursive_klass = ( frame is defframe and defframe.parent_of(node) and isinstance(defframe, nodes.ClassDef) and node.name == defframe.name ) if ( is_recursive_klass and utils.get_node_first_ancestor_of_type(node, nodes.Lambda) and ( not utils.is_default_argument(node) or node.scope().parent.scope() is not defframe ) ): # Self-referential class references are fine in lambda's -- # As long as they are not part of the default argument directly # under the scope of the parent self-referring class. # Example of valid default argument: # class MyName3: # myattr = 1 # mylambda3 = lambda: lambda a=MyName3: a # Example of invalid default argument: # class MyName4: # myattr = 1 # mylambda4 = lambda a=MyName4: lambda: a # If the above conditional is _True, # there is no possibility of undefined-variable # Also do not consume class name # (since consuming blocks subsequent checks) # -- quit return (VariableVisitConsumerAction.RETURN, None) ( maybe_before_assign, annotation_return, use_outer_definition, ) = self._is_variable_violation( node, defnode, stmt, defstmt, frame, defframe, base_scope_type, is_recursive_klass, ) if use_outer_definition: return (VariableVisitConsumerAction.CONTINUE, None) if ( maybe_before_assign and not utils.is_defined_before(node) and not astroid.are_exclusive(stmt, defstmt, ("NameError",)) ): # Used and defined in the same place, e.g `x += 1` and `del x` defined_by_stmt = defstmt is stmt and isinstance( node, (nodes.DelName, nodes.AssignName) ) if ( is_recursive_klass or defined_by_stmt or annotation_return or isinstance(defstmt, nodes.Delete) ): if not utils.node_ignores_exception(node, NameError): # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance( stmt, ( nodes.AnnAssign, nodes.FunctionDef, nodes.Arguments, ), ) and node.name in node.root().locals ): if defined_by_stmt: return (VariableVisitConsumerAction.CONTINUE, [node]) return (VariableVisitConsumerAction.CONTINUE, None) elif base_scope_type != "lambda": # E0601 may *not* occurs in lambda scope. # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif base_scope_type == "lambda": # E0601 can occur in class-level scope in lambdas, as in # the following example: # class A: # x = lambda attr: f + attr # f = 42 # We check lineno because doing the following is fine: # class A: # x = 42 # y = lambda attr: x + attr if ( isinstance(frame, nodes.ClassDef) and node.name in frame.locals and stmt.fromlineno <= defstmt.fromlineno ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) elif self._is_only_type_assignment(node, defstmt): if node.scope().locals.get(node.name): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH ) else: self.add_message( "undefined-variable", args=node.name, node=node, confidence=HIGH ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif isinstance(defstmt, nodes.ClassDef): return self._is_first_level_self_reference(node, defstmt, found_nodes) elif isinstance(defnode, nodes.NamedExpr): if isinstance(defnode.parent, nodes.IfExp): if self._is_never_evaluated(defnode, defnode.parent): self.add_message( "undefined-variable", args=node.name, node=node, confidence=INFERENCE, ) return (VariableVisitConsumerAction.RETURN, found_nodes) return (VariableVisitConsumerAction.RETURN, found_nodes) @utils.check_messages("no-name-in-module") def visit_import(self, node: nodes.Import) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return for name, _ in node.names: parts = name.split(".") try: module = next(_infer_name_module(node, parts[0])) except astroid.ResolveError: continue if not isinstance(module, nodes.Module): continue self._check_module_attrs(node, module, parts[1:]) @utils.check_messages("no-name-in-module") def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return name_parts = node.modname.split(".") try: module = node.do_import_module(name_parts[0]) except astroid.AstroidBuildingException: return module = self._check_module_attrs(node, module, name_parts[1:]) if not module: return for name, _ in node.names: if name == "*": continue self._check_module_attrs(node, module, name.split(".")) @utils.check_messages( "unbalanced-tuple-unpacking", "unpacking-non-sequence", "self-cls-assignment" ) def visit_assign(self, node: nodes.Assign) -> None: """Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned. """ self._check_self_cls_assign(node) if not isinstance(node.targets[0], (nodes.Tuple, nodes.List)): return targets = node.targets[0].itered() try: inferred = utils.safe_infer(node.value) if inferred is not None: self._check_unpacking(inferred, node, targets) except astroid.InferenceError: return # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_listcomp(self, _: nodes.ListComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def leave_assign(self, node: nodes.Assign) -> None: self._store_type_annotation_names(node) def leave_with(self, node: nodes.With) -> None: self._store_type_annotation_names(node) def visit_arguments(self, node: nodes.Arguments) -> None: for annotation in node.type_comment_args: self._store_type_annotation_node(annotation) # Relying on other checker's options, which might not have been initialized yet. @astroid.decorators.cachedproperty def _analyse_fallback_blocks(self): return get_global_option(self, "analyse-fallback-blocks", default=_False) @astroid.decorators.cachedproperty def _ignored_modules(self): return get_global_option(self, "ignored-modules", default=[]) @astroid.decorators.cachedproperty def _allow_global_unused_variables(self): return get_global_option(self, "allow-global-unused-variables", default=_True) @staticmethod def _defined_in_function_definition(node, frame): in_annotation_or_default_or_decorator = _False if ( isinstance(frame, nodes.FunctionDef) and node.statement(future=_True) is frame ): in_annotation_or_default_or_decorator = ( ( node in frame.args.annotations or node in frame.args.posonlyargs_annotations or node in frame.args.kwonlyargs_annotations or node is frame.args.varargannotation or node is frame.args.kwargannotation ) or frame.args.parent_of(node) or (frame.decorators and frame.decorators.parent_of(node)) or ( frame.returns and (node is frame.returns or frame.returns.parent_of(node)) ) ) return in_annotation_or_default_or_decorator @staticmethod def _in_lambda_or_comprehension_body( node: nodes.NodeNG, frame: nodes.NodeNG ) -> bool: """Return _True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.""" child = node parent = node.parent while parent is not None: if parent is frame: return _False if isinstance(parent, nodes.Lambda) and child is not parent.args: # Body of lambda should not have access to class attributes. return _True if isinstance(parent, nodes.Comprehension) and child is not parent.iter: # Only iter of list/set/dict/generator comprehension should have access. return _True if isinstance(parent, nodes.ComprehensionScope) and not ( parent.generators and child is parent.generators[0] ): # Body of list/set/dict/generator comprehension should not have access to class attributes. # Furthermore, only the first generator (if multiple) in comprehension should have access. return _True child = parent parent = parent.parent return _False @staticmethod def _is_variable_violation( node: nodes.Name, defnode, stmt: nodes.Statement, defstmt: nodes.Statement, frame, # scope of statement of node defframe, base_scope_type, is_recursive_klass, ) -> Tuple[bool, bool, bool]: # pylint: disable=too-many-nested-blocks maybe_before_assign = _True annotation_return = _False use_outer_definition = _False if frame is not defframe: maybe_before_assign = _detect_global_scope(node, frame, defframe) elif defframe.parent is None: # we are at the module level, check the name is not # defined in builtins if ( node.name in defframe.scope_attrs or astroid.builtin_lookup(node.name)[1] ): maybe_before_assign = _False else: # we are in a local scope, check the name is not # defined in global or builtin scope # skip this lookup if name is assigned later in function scope/lambda # Note: the node.frame() is not the same as the `frame` argument which is # equivalent to frame.statement().scope() forbid_lookup = ( isinstance(frame, nodes.FunctionDef) or isinstance(node.frame(future=_True), nodes.Lambda) ) and _assigned_locally(node) if not forbid_lookup and defframe.root().lookup(node.name)[1]: maybe_before_assign = _False use_outer_definition = stmt == defstmt and not isinstance( defnode, nodes.Comprehension ) # check if we have a nonlocal elif node.name in defframe.locals: maybe_before_assign = not any( isinstance(child, nodes.Nonlocal) and node.name in child.names for child in defframe.get_children() ) if ( base_scope_type == "lambda" and isinstance(frame, nodes.ClassDef) and node.name in frame.locals ): # This rule verifies that if the definition node of the # checked name is an Arguments node and if the name # is used a default value in the arguments defaults # and the actual definition of the variable label # is happening before the Arguments definition. # # bar = None # foo = lambda bar=bar: bar # # In this case, maybe_before_assign should be _False, otherwise # it should be _True. maybe_before_assign = not ( isinstance(defnode, nodes.Arguments) and node in defnode.defaults and frame.locals[node.name][0].fromlineno < defstmt.fromlineno ) elif isinstance(defframe, nodes.ClassDef) and isinstance( frame, nodes.FunctionDef ): # Special rule for function return annotations, # using a name defined earlier in the class containing the function. if node is frame.returns and defframe.parent_of(frame.returns): annotation_return = _True if ( frame.returns.name in defframe.locals and defframe.locals[node.name][0].lineno < frame.lineno ): # Detect class assignments with a name defined earlier in the # class. In this case, no warning should be raised. maybe_before_assign = _False else: maybe_before_assign = _True if isinstance(node.parent, nodes.Arguments): maybe_before_assign = stmt.fromlineno <= defstmt.fromlineno elif is_recursive_klass: maybe_before_assign = _True else: maybe_before_assign = ( maybe_before_assign and stmt.fromlineno <= defstmt.fromlineno ) if maybe_before_assign and stmt.fromlineno == defstmt.fromlineno: if ( isinstance(defframe, nodes.FunctionDef) and frame is defframe and defframe.parent_of(node) and stmt is not defstmt ): # Single statement function, with the statement on the # same line as the function definition maybe_before_assign = _False elif ( isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Expr, nodes.Return, ), ) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) and frame is defframe and defframe.parent_of(node) and stmt is defstmt ): # Single statement if, with assignment expression on same # line as assignment # x = b if (b := _True) else _False maybe_before_assign = _False elif ( isinstance( # pylint: disable=too-many-boolean-expressions defnode, nodes.NamedExpr ) and frame is defframe and defframe.parent_of(stmt) and stmt is defstmt and ( ( defnode.lineno == node.lineno and defnode.col_offset < node.col_offset ) or (defnode.lineno < node.lineno) or ( # Issue in the `ast` module until py39 # Nodes in a multiline string have the same lineno # Could be false-positive without check not PY39_PLUS and defnode.lineno == node.lineno and isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Return, ), ) and isinstance(defstmt.value, nodes.JoinedStr) ) ) ): # Expressions, with assignment expressions # Use only after assignment # b = (c := 2) and c maybe_before_assign = _False # Look for type checking definitions inside a type checking guard. if isinstance(defstmt, (nodes.Import, nodes.ImportFrom)): defstmt_parent = defstmt.parent if ( isinstance(defstmt_parent, nodes.If) and defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS ): # Exempt those definitions that are used inside the type checking # guard or that are defined in both type checking guard branches. used_in_branch = defstmt_parent.parent_of(node) defined_in_or_else = _False for definition in defstmt_parent.orelse: if isinstance(definition, nodes.Assign): defined_in_or_else = any( target.name == node.name for target in definition.targets if isinstance(target, nodes.AssignName) ) if defined_in_or_else: break if not used_in_branch and not defined_in_or_else: maybe_before_assign = _True return maybe_before_assign, annotation_return, use_outer_definition @staticmethod def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: """Check if `defstmt` has the potential to use and assign a name in the same statement. """ if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: # The assignment must happen as part of the first element # e.g. "assert (x:= _True), x" # NOT "assert x, (x:= _True)" value = defstmt.value.elts[0] else: value = defstmt.value if isinstance(value, nodes.IfExp): return _True if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp): return _True return isinstance(value, nodes.Call) and ( any(isinstance(kwarg.value, nodes.IfExp) for kwarg in value.keywords) or any(isinstance(arg, nodes.IfExp) for arg in value.args) ) @staticmethod def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool: """Check if variable only gets assigned a type and never a value.""" if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value: return _False defstmt_frame = defstmt.frame(future=_True) node_frame = node.frame(future=_True) parent = node while parent is not defstmt_frame.parent: parent_scope = parent.scope() local_refs = parent_scope.locals.get(node.name, []) for ref_node in local_refs: # If local ref is in the same frame as our node, but on a later lineno # we don't actually care about this local ref. # Local refs are ordered, so we break. # print(var) # var = 1 # <- irrelevant if defstmt_frame == node_frame and ref_node.lineno > node.lineno: break # If the parent of the local reference is anything but an AnnAssign # Or if the AnnAssign adds a value the variable will now have a value # var = 1 # OR # var: int = 1 if ( not isinstance(ref_node.parent, nodes.AnnAssign) or ref_node.parent.value ): return _False parent = parent_scope.parent return _True @staticmethod def _is_first_level_self_reference( node: nodes.Name, defstmt: nodes.ClassDef, found_nodes: List[nodes.NodeNG] ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Check if a first level method's annotation or default values refers to its own class, and return a consumer action """ if node.frame(future=_True).parent == defstmt and node.statement( future=_True ) == node.frame(future=_True): # Check if used as type annotation # Break if postponed evaluation is enabled if utils.is_node_in_type_annotation_context(node): if not utils.is_postponed_evaluation_enabled(node): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, None) # Check if used as default value by calling the class if isinstance(node.parent, nodes.Call) and isinstance( node.parent.parent, nodes.Arguments ): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, found_nodes) @staticmethod def _is_never_evaluated( defnode: nodes.NamedExpr, defnode_parent: nodes.IfExp ) -> bool: """Check if a NamedExpr is inside a side of if ... else that never gets evaluated """ inferred_test = utils.safe_infer(defnode_parent.test) if isinstance(inferred_test, nodes.Const): if inferred_test.value is _True and defnode == defnode_parent.orelse: return _True if inferred_test.value is _False and defnode == defnode_parent.body: return _True return _False def _ignore_class_scope(self, node): """Return _True if the node is in a local class scope, as an assignment. :param node: Node considered :type node: astroid.Node :return: _True if the node is in a local class scope, as an assignment. _False otherwise. :rtype: bool """ # Detect if we are in a local class scope, as an assignment. # For example, the following is fair game. # # class A: # b = 1 # c = lambda b=b: b * b # # class B: # tp = 1 # def func(self, arg: tp): # ... # class C: # tp = 2 # def func(self, arg=tp): # ... # class C: # class Tp: # pass # class D(Tp): # ... name = node.name frame = node.statement(future=_True).scope() in_annotation_or_default_or_decorator = self._defined_in_function_definition( node, frame ) in_ancestor_list = utils.is_ancestor_name(frame, node) if in_annotation_or_default_or_decorator or in_ancestor_list: frame_locals = frame.parent.scope().locals else: frame_locals = frame.locals return not ( (isinstance(frame, nodes.ClassDef) or in_annotation_or_default_or_decorator) and not self._in_lambda_or_comprehension_body(node, frame) and name in frame_locals ) def _loopvar_name(self, node: astroid.Name) -> None: # filter variables according to node's scope astmts = [s for s in node.lookup(node.name)[1] if hasattr(s, "assign_type")] # If this variable usage exists inside a function definition # that exists in the same loop, # the usage is safe because the function will not be defined either if # the variable is not defined. scope = node.scope() if isinstance(scope, nodes.FunctionDef) and any( asmt.scope().parent_of(scope) for asmt in astmts ): return # Filter variables according to their respective scope. Test parent # and statement to avoid #74747. This is not a total fix, which would # introduce a mechanism similar to special attribute lookup in # modules. Also, in order to get correct inference in this case, the # scope lookup rules would need to be changed to return the initial # assignment (which does not exist in code per se) as well as any later # modifications. # pylint: disable-next=too-many-boolean-expressions if ( not astmts or ( astmts[0].parent == astmts[0].root() and astmts[0].parent.parent_of(node) ) or ( astmts[0].is_statement or not isinstance(astmts[0].parent, nodes.Module) and astmts[0].statement(future=_True).parent_of(node) ) ): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement(future=_True).parent_of( stmt ) and not in_for_else_branch(astmts[i].statement(future=_True), stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) != 1: return assign = astmts[0].assign_type() if not ( isinstance(assign, (nodes.For, nodes.Comprehension, nodes.GeneratorExp)) and assign.statement(future=_True) is not node.statement(future=_True) ): return # For functions we can do more by inferring the length of the itered object if not isinstance(assign, nodes.For): self.add_message("undefined-loop-variable", args=node.name, node=node) return try: inferred = next(assign.iter.infer()) except astroid.InferenceError: self.add_message("undefined-loop-variable", args=node.name, node=node) else: if ( isinstance(inferred, astroid.Instance) and inferred.qname() == BUILTIN_RANGE ): # Consider range() objects safe, even if they might not yield any results. return # Consider sequences. sequences = ( nodes.List, nodes.Tuple, nodes.Dict, nodes.Set, astroid.objects.FrozenSet, ) if not isinstance(inferred, sequences): self.add_message("undefined-loop-variable", args=node.name, node=node) return elements = getattr(inferred, "elts", getattr(inferred, "items", [])) if not elements: self.add_message("undefined-loop-variable", args=node.name, node=node) def _check_is_unused(self, name, node, stmt, global_names, nonlocal_names): # Ignore some special names specified by user configuration. if self._is_name_ignored(stmt, name): return # Ignore names that were added dynamically to the Function scope if ( isinstance(node, nodes.FunctionDef) and name == "__class__" and len(node.locals["__class__"]) == 1 and isinstance(node.locals["__class__"][0], nodes.ClassDef) ): return # Ignore names imported by the global statement. if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)): # Detect imports, assigned to global statements. if global_names and _import_name_is_global(stmt, global_names): return argnames = list( itertools.chain(node.argnames(), [arg.name for arg in node.args.kwonlyargs]) ) # Care about functions with unknown argument (builtins) if name in argnames: self._check_unused_arguments(name, node, stmt, argnames) else: if stmt.parent and isinstance( stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple) ): if name in nonlocal_names: return qname = asname = None if isinstance(stmt, (nodes.Import, nodes.ImportFrom)): # Need the complete name, which we don't have in .locals. if len(stmt.names) > 1: import_names = next( (names for names in stmt.names if name in names), None ) else: import_names = stmt.names[0] if import_names: qname, asname = import_names name = asname or qname if _has_locals_call_after_node(stmt, node.scope()): message_name = "possibly-unused-variable" else: if isinstance(stmt, nodes.Import): if asname is not None: msg = f"{qname} imported as {asname}" else: msg = f"import {name}" self.add_message("unused-import", args=msg, node=stmt) return if isinstance(stmt, nodes.ImportFrom): if asname is not None: msg = f"{qname} imported from {stmt.modname} as {asname}" else: msg = f"{name} imported from {stmt.modname}" self.add_message("unused-import", args=msg, node=stmt) return message_name = "unused-variable" if isinstance(stmt, nodes.FunctionDef) and stmt.decorators: return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Special case for exception variable if isinstance(stmt.parent, nodes.ExceptHandler) and any( n.name == name for n in stmt.parent.nodes_of_class(nodes.Name) ): return self.add_message(message_name, args=name, node=stmt) def _is_name_ignored(self, stmt, name): authorized_rgx = self.config.dummy_variables_rgx if ( isinstance(stmt, nodes.AssignName) and isinstance(stmt.parent, nodes.Arguments) or isinstance(stmt, nodes.Arguments) ): regex = self.config.ignored_argument_names else: regex = authorized_rgx return regex and regex.match(name) def _check_unused_arguments(self, name, node, stmt, argnames): is_method = node.is_method() klass = node.parent.frame(future=_True) if is_method and isinstance(klass, nodes.ClassDef): confidence = ( INFERENCE if utils.has_known_bases(klass) else INFERENCE_FAILURE ) else: confidence = HIGH if is_method: # Don't warn for the first argument of a (non static) method if node.type != "staticmethod" and name == argnames[0]: return # Don't warn for argument of an overridden method overridden = overridden_method(klass, node.name) if overridden is not None and name in overridden.argnames(): return if node.name in utils.PYMETHODS and node.name not in ( "__init__", "__new__", ): return # Don't check callback arguments if any( node.name.startswith(cb) or node.name.endswith(cb) for cb in self.config.callbacks ): return # Don't check arguments of singledispatch.register function. if utils.is_registered_in_singledispatch_function(node): return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Don't check protocol classes if utils.is_protocol_class(klass): return self.add_message("unused-argument", args=name, node=stmt, confidence=confidence) def _check_late_binding_closure(self, node: nodes.Name) -> None: """Check whether node is a cell var that is assigned within a containing loop. Special cases where we don't care about the error: 1. When the node's function is immediately called, e.g. (lambda: i)() 2. When the node's function is returned from within the loop, e.g. return lambda: i """ if not self.linter.is_message_enabled("cell-var-from-loop"): return node_scope = node.frame(future=_True) # If node appears in a default argument expression, # look at the next enclosing frame instead if utils.is_default_argument(node, node_scope): node_scope = node_scope.parent.frame(future=_True) # Check if node is a cell var if ( not isinstance(node_scope, (nodes.Lambda, nodes.FunctionDef)) or node.name in node_scope.locals ): return assign_scope, stmts = node.lookup(node.name) if not stmts or not assign_scope.parent_of(node_scope): return if utils.is_comprehension(assign_scope): self.add_message("cell-var-from-loop", node=node, args=node.name) else: # Look for an enclosing For loop. # Currently, we only consider the first assignment assignment_node = stmts[0] maybe_for = assignment_node while maybe_for and not isinstance(maybe_for, nodes.For): if maybe_for is assign_scope: break maybe_for = maybe_for.parent else: if ( maybe_for and maybe_for.parent_of(node_scope) and not utils.is_being_called(node_scope) and node_scope.parent and not isinstance(node_scope.statement(future=_True), nodes.Return) ): self.add_message("cell-var-from-loop", node=node, args=node.name) def _should_ignore_redefined_builtin(self, stmt): if not isinstance(stmt, nodes.ImportFrom): return _False return stmt.modname in self.config.redefining_builtins_modules def _allowed_redefined_builtin(self, name): return name in self.config.allowed_redefined_builtins def _has_homonym_in_upper_function_scope( self, node: nodes.Name, index: int ) -> bool: """Return whether there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function :param node: node to check for :param index: index of the current consumer inside self._to_consume :return: _True if there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function, _False otherwise """ return any( _consumer.scope_type == "function" and node.name in _consumer.to_consume for _consumer in self._to_consume[index - 1 :: -1] ) def _store_type_annotation_node(self, type_annotation): """Given a type annotation, store all the name nodes it refers to.""" if isinstance(type_annotation, nodes.Name): self._type_annotation_names.append(type_annotation.name) return if isinstance(type_annotation, nodes.Attribute): self._store_type_annotation_node(type_annotation.expr) return if not isinstance(type_annotation, nodes.Subscript): return if ( isinstance(type_annotation.value, nodes.Attribute) and isinstance(type_annotation.value.expr, nodes.Name) and type_annotation.value.expr.name == TYPING_MODULE ): self._type_annotation_names.append(TYPING_MODULE) return self._type_annotation_names.extend( annotation.name for annotation in type_annotation.nodes_of_class(nodes.Name) ) def _store_type_annotation_names(self, node): type_annotation = node.type_annotation if not type_annotation: return self._store_type_annotation_node(node.type_annotation) def _check_self_cls_assign(self, node: nodes.Assign) -> None: """Check that self/cls don't get assigned.""" assign_names: Set[Optional[str]] = set() for target in node.targets: if isinstance(target, nodes.AssignName): assign_names.add(target.name) elif isinstance(target, nodes.Tuple): assign_names.update( elt.name for elt in target.elts if isinstance(elt, nodes.AssignName) ) scope = node.scope() nonlocals_with_same_name = any( child for child in scope.body if isinstance(child, nodes.Nonlocal) ) if nonlocals_with_same_name: scope = node.scope().parent.scope() if not ( isinstance(scope, nodes.FunctionDef) and scope.is_method() and "builtins.staticmethod" not in scope.decoratornames() ): return argument_names = scope.argnames() if not argument_names: return self_cls_name = argument_names[0] if self_cls_name in assign_names: self.add_message("self-cls-assignment", node=node, args=(self_cls_name,)) def _check_unpacking(self, inferred, node, targets): """Check for unbalanced tuple unpacking and unpacking non sequences. """ if utils.is_inside_abstract_class(node): return if utils.is_comprehension(node): return if inferred is astroid.Uninferable: return if ( isinstance(inferred.parent, nodes.Arguments) and isinstance(node.value, nodes.Name) and node.value.name == inferred.parent.vararg ): # Variable-length argument, we can't determine the length. return # Attempt to check unpacking is properly balanced values = self._nodes_to_unpack(inferred) if values is not None: if len(targets) != len(values): # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return self.add_message( "unbalanced-tuple-unpacking", node=node, args=( _get_unpacking_extra_info(node, inferred), len(targets), len(values), ), ) # attempt to check unpacking may be possible (ie RHS is iterable) elif not utils.is_iterable(inferred): self.add_message( "unpacking-non-sequence", node=node, args=(_get_unpacking_extra_info(node, inferred),), ) @staticmethod def _nodes_to_unpack(node: nodes.NodeNG) -> Optional[List[nodes.NodeNG]]: """Return the list of values of the `Assign` node.""" if isinstance(node, (nodes.Tuple, nodes.List)): return node.itered() if isinstance(node, astroid.Instance) and any( ancestor.qname() == "typing.NamedTuple" for ancestor in node.ancestors() ): return [i for i in node.values() if isinstance(i, nodes.AssignName)] return None def _check_module_attrs(self, node, module, module_names): """Check that module_names (list of string) are accessible through the given module if the latest access name corresponds to a module, return it """ while module_names: name = module_names.pop(0) if name == "__dict__": module = None break try: module = next(module.getattr(name)[0].infer()) if module is astroid.Uninferable: return None except astroid.NotFoundError: if module.name in self._ignored_modules: return None self.add_message( "no-name-in-module", args=(name, module.name), node=node ) return None except astroid.InferenceError: return None if module_names: modname = module.name if module else "__dict__" self.add_message( "no-name-in-module", node=node, args=(".".join(module_names), modname) ) return None if isinstance(module, nodes.Module): return module return None def _check_all(self, node: nodes.Module, not_consumed): assigned = next(node.igetattr("__all__")) if assigned is astroid.Uninferable: return if not assigned.pytype() in {"builtins.list", "builtins.tuple"}: line, col = assigned.tolineno, assigned.col_offset self.add_message("invalid-all-format", line=line, col_offset=col, node=node) return for elt in getattr(assigned, "elts", ()): try: elt_name = next(elt.infer()) except astroid.InferenceError: continue if elt_name is astroid.Uninferable: continue if not elt_name.parent: continue if not isinstance(elt_name, nodes.Const) or not isinstance( elt_name.value, str ): self.add_message("invalid-all-object", args=elt.as_string(), node=elt) continue elt_name = elt_name.value # If elt is in not_consumed, remove it from not_consumed if elt_name in not_consumed: del not_consumed[elt_name] continue if elt_name not in node.locals: if not node.package: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) else: basename = os.path.splitext(node.file)[0] if os.path.basename(basename) == "__init__": name = node.name + "." + elt_name try: astroid.modutils.file_from_modpath(name.split(".")) except ImportError: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) except SyntaxError: # don't yield a syntax-error warning, # because it will be later yielded # when the file will be checked pass def _check_globals(self, not_consumed): if self._allow_global_unused_variables: return for name, node_lst in not_consumed.items(): for node in node_lst: self.add_message("unused-variable", args=(name,), node=node) def _check_imports(self, not_consumed): local_names = _fix_dot_imports(not_consumed) checked = set() unused_wildcard_imports: DefaultDict[ Tuple[str, nodes.ImportFrom], List[str] ] = collections.defaultdict(list) for name, stmt in local_names: for imports in stmt.names: real_name = imported_name = imports[0] if imported_name == "*": real_name = name as_name = imports[1] if real_name in checked: continue if name not in (real_name, as_name): continue checked.add(real_name) is_type_annotation_import = ( imported_name in self._type_annotation_names or as_name in self._type_annotation_names ) if isinstance(stmt, nodes.Import) or ( isinstance(stmt, nodes.ImportFrom) and not stmt.modname ): if isinstance(stmt, nodes.ImportFrom) and SPECIAL_OBJ.search( imported_name ): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if as_name == "_": continue if as_name is None: msg = f"import {imported_name}" else: msg = f"{imported_name} imported as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) elif isinstance(stmt, nodes.ImportFrom) and stmt.modname != FUTURE: if SPECIAL_OBJ.search(imported_name): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if _is_from_future_import(stmt, name): # Check if the name is in fact loaded from a # __future__ import in another module. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if imported_name == "*": unused_wildcard_imports[(stmt.modname, stmt)].append(name) else: if as_name is None: msg = f"{imported_name} imported from {stmt.modname}" else: msg = f"{imported_name} imported from {stmt.modname} as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) # Construct string for unused-wildcard-import message for module, unused_list in unused_wildcard_imports.items(): if len(unused_list) == 1: arg_string = unused_list[0] else: arg_string = ( f"{', '.join(i for i in unused_list[:-1])} and {unused_list[-1]}" ) self.add_message( "unused-wildcard-import", args=(arg_string, module[0]), node=module[1] ) del self._to_consume def _check_metaclasses(self, node): """Update consumption analysis for metaclasses.""" consumed = [] # [(scope_locals, consumed_key)] for child_node in node.get_children(): if isinstance(child_node, nodes.ClassDef): consumed.extend(self._check_classdef_metaclasses(child_node, node)) # Pop the consumed items, in order to avoid having # unused-import and unused-variable false positives for scope_locals, name in consumed: scope_locals.pop(name, None) def _check_classdef_metaclasses(self, klass, parent_node): if not klass._metaclass: # Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors return [] consumed = [] # [(scope_locals, consumed_key)] metaclass = klass.metaclass() name = None if isinstance(klass._metaclass, nodes.Name): name = klass._metaclass.name elif isinstance(klass._metaclass, nodes.Attribute) and klass._metaclass.expr: attr = klass._metaclass.expr while not isinstance(attr, nodes.Name): attr = attr.expr name = attr.name elif metaclass: name = metaclass.root().name found = _False name = METACLASS_NAME_TRANSFORMS.get(name, name) if name: # check enclosing scopes starting from most local for scope_locals, _, _, _ in self._to_consume[::-1]: found_nodes = scope_locals.get(name, []) for found_node in found_nodes: if found_node.lineno <= klass.lineno: consumed.append((scope_locals, name)) found = _True break # Check parent scope nodes_in_parent_scope = parent_node.locals.get(name, []) for found_node_parent in nodes_in_parent_scope: if found_node_parent.lineno <= klass.lineno: found = _True break if ( not found and not metaclass and not ( name in nodes.Module.scope_attrs or utils.is_builtin(name) or name in self.config.additional_builtins ) ): self.add_message("undefined-variable", node=klass, args=(name,)) return consumed
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,203
NamesConsumer
ref
function
self._to_consume.append(NamesConsumer(node, "comprehension"))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,205
leave_generatorexp
def
function
def leave_generatorexp(self, _: nodes.GeneratorExp) -> None: """Leave genexpr: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_dictcomp(self, node: nodes.DictComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_dictcomp(self, _: nodes.DictComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_setcomp(self, node: nodes.SetComp) -> None: """Visit setcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_setcomp(self, _: nodes.SetComp) -> None: """Leave setcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Visit function: update consumption analysis variable and check locals.""" self._to_consume.append(NamesConsumer(node, "function")) if not ( self.linter.is_message_enabled("redefined-outer-name") or self.linter.is_message_enabled("redefined-builtin") ): return globs = node.root().globals for name, stmt in node.items(): if name in globs and not isinstance(stmt, nodes.Global): definition = globs[name][0] if ( isinstance(definition, nodes.ImportFrom) and definition.modname == FUTURE ): # It is a __future__ directive, not a symbol. continue # Do not take in account redefined names for the purpose # of type checking.: if any( isinstance(definition.parent, nodes.If) and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS for definition in globs[name] ): continue line = definition.fromlineno if not self._is_name_ignored(stmt, name): self.add_message( "redefined-outer-name", args=(name, line), node=stmt ) elif ( utils.is_builtin(name) and not self._allowed_redefined_builtin(name) and not self._should_ignore_redefined_builtin(stmt) ): # do not print Redefining builtin for additional builtins self.add_message("redefined-builtin", args=name, node=stmt) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Leave function: check function's locals are consumed.""" self._check_metaclasses(node) if node.type_comment_returns: self._store_type_annotation_node(node.type_comment_returns) if node.type_comment_args: for argument_annotation in node.type_comment_args: self._store_type_annotation_node(argument_annotation) not_consumed = self._to_consume.pop().to_consume if not ( self.linter.is_message_enabled("unused-variable") or self.linter.is_message_enabled("possibly-unused-variable") or self.linter.is_message_enabled("unused-argument") ): return # Don't check arguments of function which are only raising an exception. if utils.is_error(node): return # Don't check arguments of abstract methods or within an interface. is_method = node.is_method() if is_method and node.is_abstract(): return global_names = _flattened_scope_names(node.nodes_of_class(nodes.Global)) nonlocal_names = _flattened_scope_names(node.nodes_of_class(nodes.Nonlocal)) for name, stmts in not_consumed.items(): self._check_is_unused(name, node, stmts[0], global_names, nonlocal_names) visit_asyncfunctiondef = visit_functiondef leave_asyncfunctiondef = leave_functiondef @utils.check_messages( "global-variable-undefined", "global-variable-not-assigned", "global-statement", "global-at-module-level", "redefined-builtin", ) def visit_global(self, node: nodes.Global) -> None: """Check names imported exists in the global scope.""" frame = node.frame(future=_True) if isinstance(frame, nodes.Module): self.add_message("global-at-module-level", node=node) return module = frame.root() default_message = _True locals_ = node.scope().locals for name in node.names: try: assign_nodes = module.getattr(name) except astroid.NotFoundError: # unassigned global, skip assign_nodes = [] not_defined_locally_by_import = not any( isinstance(local, nodes.Import) for local in locals_.get(name, ()) ) if ( not utils.is_reassigned_after_current(node, name) and not utils.is_deleted_after_current(node, name) and not_defined_locally_by_import ): self.add_message("global-variable-not-assigned", args=name, node=node) default_message = _False continue for anode in assign_nodes: if ( isinstance(anode, nodes.AssignName) and anode.name in module.special_attributes ): self.add_message("redefined-builtin", args=name, node=node) break if anode.frame(future=_True) is module: # module level assignment break if ( isinstance(anode, (nodes.ClassDef, nodes.FunctionDef)) and anode.parent is module ): # module level function assignment break else: if not_defined_locally_by_import: # global undefined at the module scope self.add_message("global-variable-undefined", args=name, node=node) default_message = _False if default_message: self.add_message("global-statement", node=node) def visit_assignname(self, node: nodes.AssignName) -> None: if isinstance(node.assign_type(), nodes.AugAssign): self.visit_name(node) def visit_delname(self, node: nodes.DelName) -> None: self.visit_name(node) def visit_name(self, node: nodes.Name) -> None: """Don't add the 'utils.check_messages' decorator here! It's important that all 'Name' nodes are visited, otherwise the 'NamesConsumers' won't be correct. """ stmt = node.statement(future=_True) if stmt.fromlineno is None: # name node from an astroid built from live code, skip assert not stmt.root().file.endswith(".py") return self._undefined_and_used_before_checker(node, stmt) if self._is_undefined_loop_variable_enabled: self._loopvar_name(node) @utils.check_messages("redefined-outer-name") def visit_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return for outer_except, outer_except_assign_name in self._except_handler_names_queue: if node.name.name == outer_except_assign_name.name: self.add_message( "redefined-outer-name", args=(outer_except_assign_name.name, outer_except.fromlineno), node=node, ) break self._except_handler_names_queue.append((node, node.name)) @utils.check_messages("redefined-outer-name") def leave_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return self._except_handler_names_queue.pop() def _undefined_and_used_before_checker( self, node: nodes.Name, stmt: nodes.NodeNG ) -> None: frame = stmt.scope() start_index = len(self._to_consume) - 1 # iterates through parent scopes, from the inner to the outer base_scope_type = self._to_consume[start_index].scope_type for i in range(start_index, -1, -1): current_consumer = self._to_consume[i] # Certain nodes shouldn't be checked as they get checked another time if self._should_node_be_skipped(node, current_consumer, i == start_index): continue action, nodes_to_consume = self._check_consumer( node, stmt, frame, current_consumer, i, base_scope_type ) if nodes_to_consume: # Any nodes added to consumed_uncertain by get_next_to_consume() # should be added back so that they are marked as used. # They will have already had a chance to emit used-before-assignment. # We check here instead of before every single return in _check_consumer() nodes_to_consume += current_consumer.consumed_uncertain[node.name] current_consumer.mark_as_consumed(node.name, nodes_to_consume) if action is VariableVisitConsumerAction.CONTINUE: continue if action is VariableVisitConsumerAction.RETURN: return # we have not found the name, if it isn't a builtin, that's an # undefined name ! if ( self._is_undefined_variable_enabled and not ( node.name in nodes.Module.scope_attrs or utils.is_builtin(node.name) or node.name in self.config.additional_builtins or ( node.name == "__class__" and isinstance(frame, nodes.FunctionDef) and frame.is_method() ) ) and not utils.node_ignores_exception(node, NameError) ): self.add_message("undefined-variable", args=node.name, node=node) def _should_node_be_skipped( self, node: nodes.Name, consumer: NamesConsumer, is_start_index: bool ) -> bool: """Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks. """ if consumer.scope_type == "class": # The list of base classes in the class definition is not part # of the class body. # If the current scope is a class scope but it's not the inner # scope, ignore it. This prevents to access this scope instead of # the globals one in function members when there are some common # names. if utils.is_ancestor_name(consumer.node, node) or ( not is_start_index and self._ignore_class_scope(node) ): return _True # Ignore inner class scope for keywords in class definition if isinstance(node.parent, nodes.Keyword) and isinstance( node.parent.parent, nodes.ClassDef ): return _True elif consumer.scope_type == "function" and self._defined_in_function_definition( node, consumer.node ): # If the name node is used as a function default argument's value or as # a decorator, then start from the parent frame of the function instead # of the function frame - and thus open an inner class scope return _True elif consumer.scope_type == "lambda" and utils.is_default_argument( node, consumer.node ): return _True return _False # pylint: disable=too-many-return-statements def _check_consumer( self, node: nodes.Name, stmt: nodes.NodeNG, frame: nodes.LocalsDictNodeNG, current_consumer: NamesConsumer, consumer_level: int, base_scope_type: Any, ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Checks a consumer for conditions that should trigger messages.""" # If the name has already been consumed, only check it's not a loop # variable used outside the loop. # Avoid the case where there are homonyms inside function scope and # comprehension current scope (avoid bug #1731) if node.name in current_consumer.consumed: if utils.is_func_decorator(current_consumer.node) or not ( current_consumer.scope_type == "comprehension" and self._has_homonym_in_upper_function_scope(node, consumer_level) # But don't catch homonyms against the filter of a comprehension, # (like "if x" in "[x for x in expr() if x]") # https://github.com/PyCQA/pylint/issues/5586 and not ( ( isinstance(node.parent.parent, nodes.Comprehension) and node.parent in node.parent.parent.ifs ) # Or homonyms against values to keyword arguments # (like "var" in "[func(arg=var) for var in expr()]") or ( isinstance(node.scope(), nodes.ComprehensionScope) and isinstance(node.parent, (nodes.Call, nodes.Keyword)) ) ) ): self._check_late_binding_closure(node) self._loopvar_name(node) return (VariableVisitConsumerAction.RETURN, None) found_nodes = current_consumer.get_next_to_consume(node) if found_nodes is None: return (VariableVisitConsumerAction.CONTINUE, None) if not found_nodes: if node.name in current_consumer.consumed_uncertain: confidence = CONTROL_FLOW else: confidence = HIGH self.add_message( "used-before-assignment", args=node.name, node=node, confidence=confidence, ) # Mark for consumption any nodes added to consumed_uncertain by # get_next_to_consume() because they might not have executed. return ( VariableVisitConsumerAction.RETURN, current_consumer.consumed_uncertain[node.name], ) self._check_late_binding_closure(node) if not ( self._is_undefined_variable_enabled or self._is_used_before_assignment_enabled ): return (VariableVisitConsumerAction.RETURN, found_nodes) defnode = utils.assign_parent(found_nodes[0]) defstmt = defnode.statement(future=_True) defframe = defstmt.frame(future=_True) # The class reuses itself in the class scope. is_recursive_klass = ( frame is defframe and defframe.parent_of(node) and isinstance(defframe, nodes.ClassDef) and node.name == defframe.name ) if ( is_recursive_klass and utils.get_node_first_ancestor_of_type(node, nodes.Lambda) and ( not utils.is_default_argument(node) or node.scope().parent.scope() is not defframe ) ): # Self-referential class references are fine in lambda's -- # As long as they are not part of the default argument directly # under the scope of the parent self-referring class. # Example of valid default argument: # class MyName3: # myattr = 1 # mylambda3 = lambda: lambda a=MyName3: a # Example of invalid default argument: # class MyName4: # myattr = 1 # mylambda4 = lambda a=MyName4: lambda: a # If the above conditional is _True, # there is no possibility of undefined-variable # Also do not consume class name # (since consuming blocks subsequent checks) # -- quit return (VariableVisitConsumerAction.RETURN, None) ( maybe_before_assign, annotation_return, use_outer_definition, ) = self._is_variable_violation( node, defnode, stmt, defstmt, frame, defframe, base_scope_type, is_recursive_klass, ) if use_outer_definition: return (VariableVisitConsumerAction.CONTINUE, None) if ( maybe_before_assign and not utils.is_defined_before(node) and not astroid.are_exclusive(stmt, defstmt, ("NameError",)) ): # Used and defined in the same place, e.g `x += 1` and `del x` defined_by_stmt = defstmt is stmt and isinstance( node, (nodes.DelName, nodes.AssignName) ) if ( is_recursive_klass or defined_by_stmt or annotation_return or isinstance(defstmt, nodes.Delete) ): if not utils.node_ignores_exception(node, NameError): # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance( stmt, ( nodes.AnnAssign, nodes.FunctionDef, nodes.Arguments, ), ) and node.name in node.root().locals ): if defined_by_stmt: return (VariableVisitConsumerAction.CONTINUE, [node]) return (VariableVisitConsumerAction.CONTINUE, None) elif base_scope_type != "lambda": # E0601 may *not* occurs in lambda scope. # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif base_scope_type == "lambda": # E0601 can occur in class-level scope in lambdas, as in # the following example: # class A: # x = lambda attr: f + attr # f = 42 # We check lineno because doing the following is fine: # class A: # x = 42 # y = lambda attr: x + attr if ( isinstance(frame, nodes.ClassDef) and node.name in frame.locals and stmt.fromlineno <= defstmt.fromlineno ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) elif self._is_only_type_assignment(node, defstmt): if node.scope().locals.get(node.name): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH ) else: self.add_message( "undefined-variable", args=node.name, node=node, confidence=HIGH ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif isinstance(defstmt, nodes.ClassDef): return self._is_first_level_self_reference(node, defstmt, found_nodes) elif isinstance(defnode, nodes.NamedExpr): if isinstance(defnode.parent, nodes.IfExp): if self._is_never_evaluated(defnode, defnode.parent): self.add_message( "undefined-variable", args=node.name, node=node, confidence=INFERENCE, ) return (VariableVisitConsumerAction.RETURN, found_nodes) return (VariableVisitConsumerAction.RETURN, found_nodes) @utils.check_messages("no-name-in-module") def visit_import(self, node: nodes.Import) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return for name, _ in node.names: parts = name.split(".") try: module = next(_infer_name_module(node, parts[0])) except astroid.ResolveError: continue if not isinstance(module, nodes.Module): continue self._check_module_attrs(node, module, parts[1:]) @utils.check_messages("no-name-in-module") def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return name_parts = node.modname.split(".") try: module = node.do_import_module(name_parts[0]) except astroid.AstroidBuildingException: return module = self._check_module_attrs(node, module, name_parts[1:]) if not module: return for name, _ in node.names: if name == "*": continue self._check_module_attrs(node, module, name.split(".")) @utils.check_messages( "unbalanced-tuple-unpacking", "unpacking-non-sequence", "self-cls-assignment" ) def visit_assign(self, node: nodes.Assign) -> None: """Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned. """ self._check_self_cls_assign(node) if not isinstance(node.targets[0], (nodes.Tuple, nodes.List)): return targets = node.targets[0].itered() try: inferred = utils.safe_infer(node.value) if inferred is not None: self._check_unpacking(inferred, node, targets) except astroid.InferenceError: return # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_listcomp(self, _: nodes.ListComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def leave_assign(self, node: nodes.Assign) -> None: self._store_type_annotation_names(node) def leave_with(self, node: nodes.With) -> None: self._store_type_annotation_names(node) def visit_arguments(self, node: nodes.Arguments) -> None: for annotation in node.type_comment_args: self._store_type_annotation_node(annotation) # Relying on other checker's options, which might not have been initialized yet. @astroid.decorators.cachedproperty def _analyse_fallback_blocks(self): return get_global_option(self, "analyse-fallback-blocks", default=_False) @astroid.decorators.cachedproperty def _ignored_modules(self): return get_global_option(self, "ignored-modules", default=[]) @astroid.decorators.cachedproperty def _allow_global_unused_variables(self): return get_global_option(self, "allow-global-unused-variables", default=_True) @staticmethod def _defined_in_function_definition(node, frame): in_annotation_or_default_or_decorator = _False if ( isinstance(frame, nodes.FunctionDef) and node.statement(future=_True) is frame ): in_annotation_or_default_or_decorator = ( ( node in frame.args.annotations or node in frame.args.posonlyargs_annotations or node in frame.args.kwonlyargs_annotations or node is frame.args.varargannotation or node is frame.args.kwargannotation ) or frame.args.parent_of(node) or (frame.decorators and frame.decorators.parent_of(node)) or ( frame.returns and (node is frame.returns or frame.returns.parent_of(node)) ) ) return in_annotation_or_default_or_decorator @staticmethod def _in_lambda_or_comprehension_body( node: nodes.NodeNG, frame: nodes.NodeNG ) -> bool: """Return _True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.""" child = node parent = node.parent while parent is not None: if parent is frame: return _False if isinstance(parent, nodes.Lambda) and child is not parent.args: # Body of lambda should not have access to class attributes. return _True if isinstance(parent, nodes.Comprehension) and child is not parent.iter: # Only iter of list/set/dict/generator comprehension should have access. return _True if isinstance(parent, nodes.ComprehensionScope) and not ( parent.generators and child is parent.generators[0] ): # Body of list/set/dict/generator comprehension should not have access to class attributes. # Furthermore, only the first generator (if multiple) in comprehension should have access. return _True child = parent parent = parent.parent return _False @staticmethod def _is_variable_violation( node: nodes.Name, defnode, stmt: nodes.Statement, defstmt: nodes.Statement, frame, # scope of statement of node defframe, base_scope_type, is_recursive_klass, ) -> Tuple[bool, bool, bool]: # pylint: disable=too-many-nested-blocks maybe_before_assign = _True annotation_return = _False use_outer_definition = _False if frame is not defframe: maybe_before_assign = _detect_global_scope(node, frame, defframe) elif defframe.parent is None: # we are at the module level, check the name is not # defined in builtins if ( node.name in defframe.scope_attrs or astroid.builtin_lookup(node.name)[1] ): maybe_before_assign = _False else: # we are in a local scope, check the name is not # defined in global or builtin scope # skip this lookup if name is assigned later in function scope/lambda # Note: the node.frame() is not the same as the `frame` argument which is # equivalent to frame.statement().scope() forbid_lookup = ( isinstance(frame, nodes.FunctionDef) or isinstance(node.frame(future=_True), nodes.Lambda) ) and _assigned_locally(node) if not forbid_lookup and defframe.root().lookup(node.name)[1]: maybe_before_assign = _False use_outer_definition = stmt == defstmt and not isinstance( defnode, nodes.Comprehension ) # check if we have a nonlocal elif node.name in defframe.locals: maybe_before_assign = not any( isinstance(child, nodes.Nonlocal) and node.name in child.names for child in defframe.get_children() ) if ( base_scope_type == "lambda" and isinstance(frame, nodes.ClassDef) and node.name in frame.locals ): # This rule verifies that if the definition node of the # checked name is an Arguments node and if the name # is used a default value in the arguments defaults # and the actual definition of the variable label # is happening before the Arguments definition. # # bar = None # foo = lambda bar=bar: bar # # In this case, maybe_before_assign should be _False, otherwise # it should be _True. maybe_before_assign = not ( isinstance(defnode, nodes.Arguments) and node in defnode.defaults and frame.locals[node.name][0].fromlineno < defstmt.fromlineno ) elif isinstance(defframe, nodes.ClassDef) and isinstance( frame, nodes.FunctionDef ): # Special rule for function return annotations, # using a name defined earlier in the class containing the function. if node is frame.returns and defframe.parent_of(frame.returns): annotation_return = _True if ( frame.returns.name in defframe.locals and defframe.locals[node.name][0].lineno < frame.lineno ): # Detect class assignments with a name defined earlier in the # class. In this case, no warning should be raised. maybe_before_assign = _False else: maybe_before_assign = _True if isinstance(node.parent, nodes.Arguments): maybe_before_assign = stmt.fromlineno <= defstmt.fromlineno elif is_recursive_klass: maybe_before_assign = _True else: maybe_before_assign = ( maybe_before_assign and stmt.fromlineno <= defstmt.fromlineno ) if maybe_before_assign and stmt.fromlineno == defstmt.fromlineno: if ( isinstance(defframe, nodes.FunctionDef) and frame is defframe and defframe.parent_of(node) and stmt is not defstmt ): # Single statement function, with the statement on the # same line as the function definition maybe_before_assign = _False elif ( isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Expr, nodes.Return, ), ) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) and frame is defframe and defframe.parent_of(node) and stmt is defstmt ): # Single statement if, with assignment expression on same # line as assignment # x = b if (b := _True) else _False maybe_before_assign = _False elif ( isinstance( # pylint: disable=too-many-boolean-expressions defnode, nodes.NamedExpr ) and frame is defframe and defframe.parent_of(stmt) and stmt is defstmt and ( ( defnode.lineno == node.lineno and defnode.col_offset < node.col_offset ) or (defnode.lineno < node.lineno) or ( # Issue in the `ast` module until py39 # Nodes in a multiline string have the same lineno # Could be false-positive without check not PY39_PLUS and defnode.lineno == node.lineno and isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Return, ), ) and isinstance(defstmt.value, nodes.JoinedStr) ) ) ): # Expressions, with assignment expressions # Use only after assignment # b = (c := 2) and c maybe_before_assign = _False # Look for type checking definitions inside a type checking guard. if isinstance(defstmt, (nodes.Import, nodes.ImportFrom)): defstmt_parent = defstmt.parent if ( isinstance(defstmt_parent, nodes.If) and defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS ): # Exempt those definitions that are used inside the type checking # guard or that are defined in both type checking guard branches. used_in_branch = defstmt_parent.parent_of(node) defined_in_or_else = _False for definition in defstmt_parent.orelse: if isinstance(definition, nodes.Assign): defined_in_or_else = any( target.name == node.name for target in definition.targets if isinstance(target, nodes.AssignName) ) if defined_in_or_else: break if not used_in_branch and not defined_in_or_else: maybe_before_assign = _True return maybe_before_assign, annotation_return, use_outer_definition @staticmethod def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: """Check if `defstmt` has the potential to use and assign a name in the same statement. """ if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: # The assignment must happen as part of the first element # e.g. "assert (x:= _True), x" # NOT "assert x, (x:= _True)" value = defstmt.value.elts[0] else: value = defstmt.value if isinstance(value, nodes.IfExp): return _True if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp): return _True return isinstance(value, nodes.Call) and ( any(isinstance(kwarg.value, nodes.IfExp) for kwarg in value.keywords) or any(isinstance(arg, nodes.IfExp) for arg in value.args) ) @staticmethod def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool: """Check if variable only gets assigned a type and never a value.""" if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value: return _False defstmt_frame = defstmt.frame(future=_True) node_frame = node.frame(future=_True) parent = node while parent is not defstmt_frame.parent: parent_scope = parent.scope() local_refs = parent_scope.locals.get(node.name, []) for ref_node in local_refs: # If local ref is in the same frame as our node, but on a later lineno # we don't actually care about this local ref. # Local refs are ordered, so we break. # print(var) # var = 1 # <- irrelevant if defstmt_frame == node_frame and ref_node.lineno > node.lineno: break # If the parent of the local reference is anything but an AnnAssign # Or if the AnnAssign adds a value the variable will now have a value # var = 1 # OR # var: int = 1 if ( not isinstance(ref_node.parent, nodes.AnnAssign) or ref_node.parent.value ): return _False parent = parent_scope.parent return _True @staticmethod def _is_first_level_self_reference( node: nodes.Name, defstmt: nodes.ClassDef, found_nodes: List[nodes.NodeNG] ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Check if a first level method's annotation or default values refers to its own class, and return a consumer action """ if node.frame(future=_True).parent == defstmt and node.statement( future=_True ) == node.frame(future=_True): # Check if used as type annotation # Break if postponed evaluation is enabled if utils.is_node_in_type_annotation_context(node): if not utils.is_postponed_evaluation_enabled(node): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, None) # Check if used as default value by calling the class if isinstance(node.parent, nodes.Call) and isinstance( node.parent.parent, nodes.Arguments ): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, found_nodes) @staticmethod def _is_never_evaluated( defnode: nodes.NamedExpr, defnode_parent: nodes.IfExp ) -> bool: """Check if a NamedExpr is inside a side of if ... else that never gets evaluated """ inferred_test = utils.safe_infer(defnode_parent.test) if isinstance(inferred_test, nodes.Const): if inferred_test.value is _True and defnode == defnode_parent.orelse: return _True if inferred_test.value is _False and defnode == defnode_parent.body: return _True return _False def _ignore_class_scope(self, node): """Return _True if the node is in a local class scope, as an assignment. :param node: Node considered :type node: astroid.Node :return: _True if the node is in a local class scope, as an assignment. _False otherwise. :rtype: bool """ # Detect if we are in a local class scope, as an assignment. # For example, the following is fair game. # # class A: # b = 1 # c = lambda b=b: b * b # # class B: # tp = 1 # def func(self, arg: tp): # ... # class C: # tp = 2 # def func(self, arg=tp): # ... # class C: # class Tp: # pass # class D(Tp): # ... name = node.name frame = node.statement(future=_True).scope() in_annotation_or_default_or_decorator = self._defined_in_function_definition( node, frame ) in_ancestor_list = utils.is_ancestor_name(frame, node) if in_annotation_or_default_or_decorator or in_ancestor_list: frame_locals = frame.parent.scope().locals else: frame_locals = frame.locals return not ( (isinstance(frame, nodes.ClassDef) or in_annotation_or_default_or_decorator) and not self._in_lambda_or_comprehension_body(node, frame) and name in frame_locals ) def _loopvar_name(self, node: astroid.Name) -> None: # filter variables according to node's scope astmts = [s for s in node.lookup(node.name)[1] if hasattr(s, "assign_type")] # If this variable usage exists inside a function definition # that exists in the same loop, # the usage is safe because the function will not be defined either if # the variable is not defined. scope = node.scope() if isinstance(scope, nodes.FunctionDef) and any( asmt.scope().parent_of(scope) for asmt in astmts ): return # Filter variables according to their respective scope. Test parent # and statement to avoid #74747. This is not a total fix, which would # introduce a mechanism similar to special attribute lookup in # modules. Also, in order to get correct inference in this case, the # scope lookup rules would need to be changed to return the initial # assignment (which does not exist in code per se) as well as any later # modifications. # pylint: disable-next=too-many-boolean-expressions if ( not astmts or ( astmts[0].parent == astmts[0].root() and astmts[0].parent.parent_of(node) ) or ( astmts[0].is_statement or not isinstance(astmts[0].parent, nodes.Module) and astmts[0].statement(future=_True).parent_of(node) ) ): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement(future=_True).parent_of( stmt ) and not in_for_else_branch(astmts[i].statement(future=_True), stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) != 1: return assign = astmts[0].assign_type() if not ( isinstance(assign, (nodes.For, nodes.Comprehension, nodes.GeneratorExp)) and assign.statement(future=_True) is not node.statement(future=_True) ): return # For functions we can do more by inferring the length of the itered object if not isinstance(assign, nodes.For): self.add_message("undefined-loop-variable", args=node.name, node=node) return try: inferred = next(assign.iter.infer()) except astroid.InferenceError: self.add_message("undefined-loop-variable", args=node.name, node=node) else: if ( isinstance(inferred, astroid.Instance) and inferred.qname() == BUILTIN_RANGE ): # Consider range() objects safe, even if they might not yield any results. return # Consider sequences. sequences = ( nodes.List, nodes.Tuple, nodes.Dict, nodes.Set, astroid.objects.FrozenSet, ) if not isinstance(inferred, sequences): self.add_message("undefined-loop-variable", args=node.name, node=node) return elements = getattr(inferred, "elts", getattr(inferred, "items", [])) if not elements: self.add_message("undefined-loop-variable", args=node.name, node=node) def _check_is_unused(self, name, node, stmt, global_names, nonlocal_names): # Ignore some special names specified by user configuration. if self._is_name_ignored(stmt, name): return # Ignore names that were added dynamically to the Function scope if ( isinstance(node, nodes.FunctionDef) and name == "__class__" and len(node.locals["__class__"]) == 1 and isinstance(node.locals["__class__"][0], nodes.ClassDef) ): return # Ignore names imported by the global statement. if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)): # Detect imports, assigned to global statements. if global_names and _import_name_is_global(stmt, global_names): return argnames = list( itertools.chain(node.argnames(), [arg.name for arg in node.args.kwonlyargs]) ) # Care about functions with unknown argument (builtins) if name in argnames: self._check_unused_arguments(name, node, stmt, argnames) else: if stmt.parent and isinstance( stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple) ): if name in nonlocal_names: return qname = asname = None if isinstance(stmt, (nodes.Import, nodes.ImportFrom)): # Need the complete name, which we don't have in .locals. if len(stmt.names) > 1: import_names = next( (names for names in stmt.names if name in names), None ) else: import_names = stmt.names[0] if import_names: qname, asname = import_names name = asname or qname if _has_locals_call_after_node(stmt, node.scope()): message_name = "possibly-unused-variable" else: if isinstance(stmt, nodes.Import): if asname is not None: msg = f"{qname} imported as {asname}" else: msg = f"import {name}" self.add_message("unused-import", args=msg, node=stmt) return if isinstance(stmt, nodes.ImportFrom): if asname is not None: msg = f"{qname} imported from {stmt.modname} as {asname}" else: msg = f"{name} imported from {stmt.modname}" self.add_message("unused-import", args=msg, node=stmt) return message_name = "unused-variable" if isinstance(stmt, nodes.FunctionDef) and stmt.decorators: return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Special case for exception variable if isinstance(stmt.parent, nodes.ExceptHandler) and any( n.name == name for n in stmt.parent.nodes_of_class(nodes.Name) ): return self.add_message(message_name, args=name, node=stmt) def _is_name_ignored(self, stmt, name): authorized_rgx = self.config.dummy_variables_rgx if ( isinstance(stmt, nodes.AssignName) and isinstance(stmt.parent, nodes.Arguments) or isinstance(stmt, nodes.Arguments) ): regex = self.config.ignored_argument_names else: regex = authorized_rgx return regex and regex.match(name) def _check_unused_arguments(self, name, node, stmt, argnames): is_method = node.is_method() klass = node.parent.frame(future=_True) if is_method and isinstance(klass, nodes.ClassDef): confidence = ( INFERENCE if utils.has_known_bases(klass) else INFERENCE_FAILURE ) else: confidence = HIGH if is_method: # Don't warn for the first argument of a (non static) method if node.type != "staticmethod" and name == argnames[0]: return # Don't warn for argument of an overridden method overridden = overridden_method(klass, node.name) if overridden is not None and name in overridden.argnames(): return if node.name in utils.PYMETHODS and node.name not in ( "__init__", "__new__", ): return # Don't check callback arguments if any( node.name.startswith(cb) or node.name.endswith(cb) for cb in self.config.callbacks ): return # Don't check arguments of singledispatch.register function. if utils.is_registered_in_singledispatch_function(node): return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Don't check protocol classes if utils.is_protocol_class(klass): return self.add_message("unused-argument", args=name, node=stmt, confidence=confidence) def _check_late_binding_closure(self, node: nodes.Name) -> None: """Check whether node is a cell var that is assigned within a containing loop. Special cases where we don't care about the error: 1. When the node's function is immediately called, e.g. (lambda: i)() 2. When the node's function is returned from within the loop, e.g. return lambda: i """ if not self.linter.is_message_enabled("cell-var-from-loop"): return node_scope = node.frame(future=_True) # If node appears in a default argument expression, # look at the next enclosing frame instead if utils.is_default_argument(node, node_scope): node_scope = node_scope.parent.frame(future=_True) # Check if node is a cell var if ( not isinstance(node_scope, (nodes.Lambda, nodes.FunctionDef)) or node.name in node_scope.locals ): return assign_scope, stmts = node.lookup(node.name) if not stmts or not assign_scope.parent_of(node_scope): return if utils.is_comprehension(assign_scope): self.add_message("cell-var-from-loop", node=node, args=node.name) else: # Look for an enclosing For loop. # Currently, we only consider the first assignment assignment_node = stmts[0] maybe_for = assignment_node while maybe_for and not isinstance(maybe_for, nodes.For): if maybe_for is assign_scope: break maybe_for = maybe_for.parent else: if ( maybe_for and maybe_for.parent_of(node_scope) and not utils.is_being_called(node_scope) and node_scope.parent and not isinstance(node_scope.statement(future=_True), nodes.Return) ): self.add_message("cell-var-from-loop", node=node, args=node.name) def _should_ignore_redefined_builtin(self, stmt): if not isinstance(stmt, nodes.ImportFrom): return _False return stmt.modname in self.config.redefining_builtins_modules def _allowed_redefined_builtin(self, name): return name in self.config.allowed_redefined_builtins def _has_homonym_in_upper_function_scope( self, node: nodes.Name, index: int ) -> bool: """Return whether there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function :param node: node to check for :param index: index of the current consumer inside self._to_consume :return: _True if there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function, _False otherwise """ return any( _consumer.scope_type == "function" and node.name in _consumer.to_consume for _consumer in self._to_consume[index - 1 :: -1] ) def _store_type_annotation_node(self, type_annotation): """Given a type annotation, store all the name nodes it refers to.""" if isinstance(type_annotation, nodes.Name): self._type_annotation_names.append(type_annotation.name) return if isinstance(type_annotation, nodes.Attribute): self._store_type_annotation_node(type_annotation.expr) return if not isinstance(type_annotation, nodes.Subscript): return if ( isinstance(type_annotation.value, nodes.Attribute) and isinstance(type_annotation.value.expr, nodes.Name) and type_annotation.value.expr.name == TYPING_MODULE ): self._type_annotation_names.append(TYPING_MODULE) return self._type_annotation_names.extend( annotation.name for annotation in type_annotation.nodes_of_class(nodes.Name) ) def _store_type_annotation_names(self, node): type_annotation = node.type_annotation if not type_annotation: return self._store_type_annotation_node(node.type_annotation) def _check_self_cls_assign(self, node: nodes.Assign) -> None: """Check that self/cls don't get assigned.""" assign_names: Set[Optional[str]] = set() for target in node.targets: if isinstance(target, nodes.AssignName): assign_names.add(target.name) elif isinstance(target, nodes.Tuple): assign_names.update( elt.name for elt in target.elts if isinstance(elt, nodes.AssignName) ) scope = node.scope() nonlocals_with_same_name = any( child for child in scope.body if isinstance(child, nodes.Nonlocal) ) if nonlocals_with_same_name: scope = node.scope().parent.scope() if not ( isinstance(scope, nodes.FunctionDef) and scope.is_method() and "builtins.staticmethod" not in scope.decoratornames() ): return argument_names = scope.argnames() if not argument_names: return self_cls_name = argument_names[0] if self_cls_name in assign_names: self.add_message("self-cls-assignment", node=node, args=(self_cls_name,)) def _check_unpacking(self, inferred, node, targets): """Check for unbalanced tuple unpacking and unpacking non sequences. """ if utils.is_inside_abstract_class(node): return if utils.is_comprehension(node): return if inferred is astroid.Uninferable: return if ( isinstance(inferred.parent, nodes.Arguments) and isinstance(node.value, nodes.Name) and node.value.name == inferred.parent.vararg ): # Variable-length argument, we can't determine the length. return # Attempt to check unpacking is properly balanced values = self._nodes_to_unpack(inferred) if values is not None: if len(targets) != len(values): # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return self.add_message( "unbalanced-tuple-unpacking", node=node, args=( _get_unpacking_extra_info(node, inferred), len(targets), len(values), ), ) # attempt to check unpacking may be possible (ie RHS is iterable) elif not utils.is_iterable(inferred): self.add_message( "unpacking-non-sequence", node=node, args=(_get_unpacking_extra_info(node, inferred),), ) @staticmethod def _nodes_to_unpack(node: nodes.NodeNG) -> Optional[List[nodes.NodeNG]]: """Return the list of values of the `Assign` node.""" if isinstance(node, (nodes.Tuple, nodes.List)): return node.itered() if isinstance(node, astroid.Instance) and any( ancestor.qname() == "typing.NamedTuple" for ancestor in node.ancestors() ): return [i for i in node.values() if isinstance(i, nodes.AssignName)] return None def _check_module_attrs(self, node, module, module_names): """Check that module_names (list of string) are accessible through the given module if the latest access name corresponds to a module, return it """ while module_names: name = module_names.pop(0) if name == "__dict__": module = None break try: module = next(module.getattr(name)[0].infer()) if module is astroid.Uninferable: return None except astroid.NotFoundError: if module.name in self._ignored_modules: return None self.add_message( "no-name-in-module", args=(name, module.name), node=node ) return None except astroid.InferenceError: return None if module_names: modname = module.name if module else "__dict__" self.add_message( "no-name-in-module", node=node, args=(".".join(module_names), modname) ) return None if isinstance(module, nodes.Module): return module return None def _check_all(self, node: nodes.Module, not_consumed): assigned = next(node.igetattr("__all__")) if assigned is astroid.Uninferable: return if not assigned.pytype() in {"builtins.list", "builtins.tuple"}: line, col = assigned.tolineno, assigned.col_offset self.add_message("invalid-all-format", line=line, col_offset=col, node=node) return for elt in getattr(assigned, "elts", ()): try: elt_name = next(elt.infer()) except astroid.InferenceError: continue if elt_name is astroid.Uninferable: continue if not elt_name.parent: continue if not isinstance(elt_name, nodes.Const) or not isinstance( elt_name.value, str ): self.add_message("invalid-all-object", args=elt.as_string(), node=elt) continue elt_name = elt_name.value # If elt is in not_consumed, remove it from not_consumed if elt_name in not_consumed: del not_consumed[elt_name] continue if elt_name not in node.locals: if not node.package: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) else: basename = os.path.splitext(node.file)[0] if os.path.basename(basename) == "__init__": name = node.name + "." + elt_name try: astroid.modutils.file_from_modpath(name.split(".")) except ImportError: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) except SyntaxError: # don't yield a syntax-error warning, # because it will be later yielded # when the file will be checked pass def _check_globals(self, not_consumed): if self._allow_global_unused_variables: return for name, node_lst in not_consumed.items(): for node in node_lst: self.add_message("unused-variable", args=(name,), node=node) def _check_imports(self, not_consumed): local_names = _fix_dot_imports(not_consumed) checked = set() unused_wildcard_imports: DefaultDict[ Tuple[str, nodes.ImportFrom], List[str] ] = collections.defaultdict(list) for name, stmt in local_names: for imports in stmt.names: real_name = imported_name = imports[0] if imported_name == "*": real_name = name as_name = imports[1] if real_name in checked: continue if name not in (real_name, as_name): continue checked.add(real_name) is_type_annotation_import = ( imported_name in self._type_annotation_names or as_name in self._type_annotation_names ) if isinstance(stmt, nodes.Import) or ( isinstance(stmt, nodes.ImportFrom) and not stmt.modname ): if isinstance(stmt, nodes.ImportFrom) and SPECIAL_OBJ.search( imported_name ): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if as_name == "_": continue if as_name is None: msg = f"import {imported_name}" else: msg = f"{imported_name} imported as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) elif isinstance(stmt, nodes.ImportFrom) and stmt.modname != FUTURE: if SPECIAL_OBJ.search(imported_name): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if _is_from_future_import(stmt, name): # Check if the name is in fact loaded from a # __future__ import in another module. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if imported_name == "*": unused_wildcard_imports[(stmt.modname, stmt)].append(name) else: if as_name is None: msg = f"{imported_name} imported from {stmt.modname}" else: msg = f"{imported_name} imported from {stmt.modname} as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) # Construct string for unused-wildcard-import message for module, unused_list in unused_wildcard_imports.items(): if len(unused_list) == 1: arg_string = unused_list[0] else: arg_string = ( f"{', '.join(i for i in unused_list[:-1])} and {unused_list[-1]}" ) self.add_message( "unused-wildcard-import", args=(arg_string, module[0]), node=module[1] ) del self._to_consume def _check_metaclasses(self, node): """Update consumption analysis for metaclasses.""" consumed = [] # [(scope_locals, consumed_key)] for child_node in node.get_children(): if isinstance(child_node, nodes.ClassDef): consumed.extend(self._check_classdef_metaclasses(child_node, node)) # Pop the consumed items, in order to avoid having # unused-import and unused-variable false positives for scope_locals, name in consumed: scope_locals.pop(name, None) def _check_classdef_metaclasses(self, klass, parent_node): if not klass._metaclass: # Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors return [] consumed = [] # [(scope_locals, consumed_key)] metaclass = klass.metaclass() name = None if isinstance(klass._metaclass, nodes.Name): name = klass._metaclass.name elif isinstance(klass._metaclass, nodes.Attribute) and klass._metaclass.expr: attr = klass._metaclass.expr while not isinstance(attr, nodes.Name): attr = attr.expr name = attr.name elif metaclass: name = metaclass.root().name found = _False name = METACLASS_NAME_TRANSFORMS.get(name, name) if name: # check enclosing scopes starting from most local for scope_locals, _, _, _ in self._to_consume[::-1]: found_nodes = scope_locals.get(name, []) for found_node in found_nodes: if found_node.lineno <= klass.lineno: consumed.append((scope_locals, name)) found = _True break # Check parent scope nodes_in_parent_scope = parent_node.locals.get(name, []) for found_node_parent in nodes_in_parent_scope: if found_node_parent.lineno <= klass.lineno: found = _True break if ( not found and not metaclass and not ( name in nodes.Module.scope_attrs or utils.is_builtin(name) or name in self.config.additional_builtins ) ): self.add_message("undefined-variable", node=klass, args=(name,)) return consumed
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,210
visit_dictcomp
def
function
def visit_dictcomp(self, node: nodes.DictComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_dictcomp(self, _: nodes.DictComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_setcomp(self, node: nodes.SetComp) -> None: """Visit setcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_setcomp(self, _: nodes.SetComp) -> None: """Leave setcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Visit function: update consumption analysis variable and check locals.""" self._to_consume.append(NamesConsumer(node, "function")) if not ( self.linter.is_message_enabled("redefined-outer-name") or self.linter.is_message_enabled("redefined-builtin") ): return globs = node.root().globals for name, stmt in node.items(): if name in globs and not isinstance(stmt, nodes.Global): definition = globs[name][0] if ( isinstance(definition, nodes.ImportFrom) and definition.modname == FUTURE ): # It is a __future__ directive, not a symbol. continue # Do not take in account redefined names for the purpose # of type checking.: if any( isinstance(definition.parent, nodes.If) and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS for definition in globs[name] ): continue line = definition.fromlineno if not self._is_name_ignored(stmt, name): self.add_message( "redefined-outer-name", args=(name, line), node=stmt ) elif ( utils.is_builtin(name) and not self._allowed_redefined_builtin(name) and not self._should_ignore_redefined_builtin(stmt) ): # do not print Redefining builtin for additional builtins self.add_message("redefined-builtin", args=name, node=stmt) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Leave function: check function's locals are consumed.""" self._check_metaclasses(node) if node.type_comment_returns: self._store_type_annotation_node(node.type_comment_returns) if node.type_comment_args: for argument_annotation in node.type_comment_args: self._store_type_annotation_node(argument_annotation) not_consumed = self._to_consume.pop().to_consume if not ( self.linter.is_message_enabled("unused-variable") or self.linter.is_message_enabled("possibly-unused-variable") or self.linter.is_message_enabled("unused-argument") ): return # Don't check arguments of function which are only raising an exception. if utils.is_error(node): return # Don't check arguments of abstract methods or within an interface. is_method = node.is_method() if is_method and node.is_abstract(): return global_names = _flattened_scope_names(node.nodes_of_class(nodes.Global)) nonlocal_names = _flattened_scope_names(node.nodes_of_class(nodes.Nonlocal)) for name, stmts in not_consumed.items(): self._check_is_unused(name, node, stmts[0], global_names, nonlocal_names) visit_asyncfunctiondef = visit_functiondef leave_asyncfunctiondef = leave_functiondef @utils.check_messages( "global-variable-undefined", "global-variable-not-assigned", "global-statement", "global-at-module-level", "redefined-builtin", ) def visit_global(self, node: nodes.Global) -> None: """Check names imported exists in the global scope.""" frame = node.frame(future=_True) if isinstance(frame, nodes.Module): self.add_message("global-at-module-level", node=node) return module = frame.root() default_message = _True locals_ = node.scope().locals for name in node.names: try: assign_nodes = module.getattr(name) except astroid.NotFoundError: # unassigned global, skip assign_nodes = [] not_defined_locally_by_import = not any( isinstance(local, nodes.Import) for local in locals_.get(name, ()) ) if ( not utils.is_reassigned_after_current(node, name) and not utils.is_deleted_after_current(node, name) and not_defined_locally_by_import ): self.add_message("global-variable-not-assigned", args=name, node=node) default_message = _False continue for anode in assign_nodes: if ( isinstance(anode, nodes.AssignName) and anode.name in module.special_attributes ): self.add_message("redefined-builtin", args=name, node=node) break if anode.frame(future=_True) is module: # module level assignment break if ( isinstance(anode, (nodes.ClassDef, nodes.FunctionDef)) and anode.parent is module ): # module level function assignment break else: if not_defined_locally_by_import: # global undefined at the module scope self.add_message("global-variable-undefined", args=name, node=node) default_message = _False if default_message: self.add_message("global-statement", node=node) def visit_assignname(self, node: nodes.AssignName) -> None: if isinstance(node.assign_type(), nodes.AugAssign): self.visit_name(node) def visit_delname(self, node: nodes.DelName) -> None: self.visit_name(node) def visit_name(self, node: nodes.Name) -> None: """Don't add the 'utils.check_messages' decorator here! It's important that all 'Name' nodes are visited, otherwise the 'NamesConsumers' won't be correct. """ stmt = node.statement(future=_True) if stmt.fromlineno is None: # name node from an astroid built from live code, skip assert not stmt.root().file.endswith(".py") return self._undefined_and_used_before_checker(node, stmt) if self._is_undefined_loop_variable_enabled: self._loopvar_name(node) @utils.check_messages("redefined-outer-name") def visit_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return for outer_except, outer_except_assign_name in self._except_handler_names_queue: if node.name.name == outer_except_assign_name.name: self.add_message( "redefined-outer-name", args=(outer_except_assign_name.name, outer_except.fromlineno), node=node, ) break self._except_handler_names_queue.append((node, node.name)) @utils.check_messages("redefined-outer-name") def leave_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return self._except_handler_names_queue.pop() def _undefined_and_used_before_checker( self, node: nodes.Name, stmt: nodes.NodeNG ) -> None: frame = stmt.scope() start_index = len(self._to_consume) - 1 # iterates through parent scopes, from the inner to the outer base_scope_type = self._to_consume[start_index].scope_type for i in range(start_index, -1, -1): current_consumer = self._to_consume[i] # Certain nodes shouldn't be checked as they get checked another time if self._should_node_be_skipped(node, current_consumer, i == start_index): continue action, nodes_to_consume = self._check_consumer( node, stmt, frame, current_consumer, i, base_scope_type ) if nodes_to_consume: # Any nodes added to consumed_uncertain by get_next_to_consume() # should be added back so that they are marked as used. # They will have already had a chance to emit used-before-assignment. # We check here instead of before every single return in _check_consumer() nodes_to_consume += current_consumer.consumed_uncertain[node.name] current_consumer.mark_as_consumed(node.name, nodes_to_consume) if action is VariableVisitConsumerAction.CONTINUE: continue if action is VariableVisitConsumerAction.RETURN: return # we have not found the name, if it isn't a builtin, that's an # undefined name ! if ( self._is_undefined_variable_enabled and not ( node.name in nodes.Module.scope_attrs or utils.is_builtin(node.name) or node.name in self.config.additional_builtins or ( node.name == "__class__" and isinstance(frame, nodes.FunctionDef) and frame.is_method() ) ) and not utils.node_ignores_exception(node, NameError) ): self.add_message("undefined-variable", args=node.name, node=node) def _should_node_be_skipped( self, node: nodes.Name, consumer: NamesConsumer, is_start_index: bool ) -> bool: """Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks. """ if consumer.scope_type == "class": # The list of base classes in the class definition is not part # of the class body. # If the current scope is a class scope but it's not the inner # scope, ignore it. This prevents to access this scope instead of # the globals one in function members when there are some common # names. if utils.is_ancestor_name(consumer.node, node) or ( not is_start_index and self._ignore_class_scope(node) ): return _True # Ignore inner class scope for keywords in class definition if isinstance(node.parent, nodes.Keyword) and isinstance( node.parent.parent, nodes.ClassDef ): return _True elif consumer.scope_type == "function" and self._defined_in_function_definition( node, consumer.node ): # If the name node is used as a function default argument's value or as # a decorator, then start from the parent frame of the function instead # of the function frame - and thus open an inner class scope return _True elif consumer.scope_type == "lambda" and utils.is_default_argument( node, consumer.node ): return _True return _False # pylint: disable=too-many-return-statements def _check_consumer( self, node: nodes.Name, stmt: nodes.NodeNG, frame: nodes.LocalsDictNodeNG, current_consumer: NamesConsumer, consumer_level: int, base_scope_type: Any, ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Checks a consumer for conditions that should trigger messages.""" # If the name has already been consumed, only check it's not a loop # variable used outside the loop. # Avoid the case where there are homonyms inside function scope and # comprehension current scope (avoid bug #1731) if node.name in current_consumer.consumed: if utils.is_func_decorator(current_consumer.node) or not ( current_consumer.scope_type == "comprehension" and self._has_homonym_in_upper_function_scope(node, consumer_level) # But don't catch homonyms against the filter of a comprehension, # (like "if x" in "[x for x in expr() if x]") # https://github.com/PyCQA/pylint/issues/5586 and not ( ( isinstance(node.parent.parent, nodes.Comprehension) and node.parent in node.parent.parent.ifs ) # Or homonyms against values to keyword arguments # (like "var" in "[func(arg=var) for var in expr()]") or ( isinstance(node.scope(), nodes.ComprehensionScope) and isinstance(node.parent, (nodes.Call, nodes.Keyword)) ) ) ): self._check_late_binding_closure(node) self._loopvar_name(node) return (VariableVisitConsumerAction.RETURN, None) found_nodes = current_consumer.get_next_to_consume(node) if found_nodes is None: return (VariableVisitConsumerAction.CONTINUE, None) if not found_nodes: if node.name in current_consumer.consumed_uncertain: confidence = CONTROL_FLOW else: confidence = HIGH self.add_message( "used-before-assignment", args=node.name, node=node, confidence=confidence, ) # Mark for consumption any nodes added to consumed_uncertain by # get_next_to_consume() because they might not have executed. return ( VariableVisitConsumerAction.RETURN, current_consumer.consumed_uncertain[node.name], ) self._check_late_binding_closure(node) if not ( self._is_undefined_variable_enabled or self._is_used_before_assignment_enabled ): return (VariableVisitConsumerAction.RETURN, found_nodes) defnode = utils.assign_parent(found_nodes[0]) defstmt = defnode.statement(future=_True) defframe = defstmt.frame(future=_True) # The class reuses itself in the class scope. is_recursive_klass = ( frame is defframe and defframe.parent_of(node) and isinstance(defframe, nodes.ClassDef) and node.name == defframe.name ) if ( is_recursive_klass and utils.get_node_first_ancestor_of_type(node, nodes.Lambda) and ( not utils.is_default_argument(node) or node.scope().parent.scope() is not defframe ) ): # Self-referential class references are fine in lambda's -- # As long as they are not part of the default argument directly # under the scope of the parent self-referring class. # Example of valid default argument: # class MyName3: # myattr = 1 # mylambda3 = lambda: lambda a=MyName3: a # Example of invalid default argument: # class MyName4: # myattr = 1 # mylambda4 = lambda a=MyName4: lambda: a # If the above conditional is _True, # there is no possibility of undefined-variable # Also do not consume class name # (since consuming blocks subsequent checks) # -- quit return (VariableVisitConsumerAction.RETURN, None) ( maybe_before_assign, annotation_return, use_outer_definition, ) = self._is_variable_violation( node, defnode, stmt, defstmt, frame, defframe, base_scope_type, is_recursive_klass, ) if use_outer_definition: return (VariableVisitConsumerAction.CONTINUE, None) if ( maybe_before_assign and not utils.is_defined_before(node) and not astroid.are_exclusive(stmt, defstmt, ("NameError",)) ): # Used and defined in the same place, e.g `x += 1` and `del x` defined_by_stmt = defstmt is stmt and isinstance( node, (nodes.DelName, nodes.AssignName) ) if ( is_recursive_klass or defined_by_stmt or annotation_return or isinstance(defstmt, nodes.Delete) ): if not utils.node_ignores_exception(node, NameError): # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance( stmt, ( nodes.AnnAssign, nodes.FunctionDef, nodes.Arguments, ), ) and node.name in node.root().locals ): if defined_by_stmt: return (VariableVisitConsumerAction.CONTINUE, [node]) return (VariableVisitConsumerAction.CONTINUE, None) elif base_scope_type != "lambda": # E0601 may *not* occurs in lambda scope. # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif base_scope_type == "lambda": # E0601 can occur in class-level scope in lambdas, as in # the following example: # class A: # x = lambda attr: f + attr # f = 42 # We check lineno because doing the following is fine: # class A: # x = 42 # y = lambda attr: x + attr if ( isinstance(frame, nodes.ClassDef) and node.name in frame.locals and stmt.fromlineno <= defstmt.fromlineno ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) elif self._is_only_type_assignment(node, defstmt): if node.scope().locals.get(node.name): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH ) else: self.add_message( "undefined-variable", args=node.name, node=node, confidence=HIGH ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif isinstance(defstmt, nodes.ClassDef): return self._is_first_level_self_reference(node, defstmt, found_nodes) elif isinstance(defnode, nodes.NamedExpr): if isinstance(defnode.parent, nodes.IfExp): if self._is_never_evaluated(defnode, defnode.parent): self.add_message( "undefined-variable", args=node.name, node=node, confidence=INFERENCE, ) return (VariableVisitConsumerAction.RETURN, found_nodes) return (VariableVisitConsumerAction.RETURN, found_nodes) @utils.check_messages("no-name-in-module") def visit_import(self, node: nodes.Import) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return for name, _ in node.names: parts = name.split(".") try: module = next(_infer_name_module(node, parts[0])) except astroid.ResolveError: continue if not isinstance(module, nodes.Module): continue self._check_module_attrs(node, module, parts[1:]) @utils.check_messages("no-name-in-module") def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return name_parts = node.modname.split(".") try: module = node.do_import_module(name_parts[0]) except astroid.AstroidBuildingException: return module = self._check_module_attrs(node, module, name_parts[1:]) if not module: return for name, _ in node.names: if name == "*": continue self._check_module_attrs(node, module, name.split(".")) @utils.check_messages( "unbalanced-tuple-unpacking", "unpacking-non-sequence", "self-cls-assignment" ) def visit_assign(self, node: nodes.Assign) -> None: """Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned. """ self._check_self_cls_assign(node) if not isinstance(node.targets[0], (nodes.Tuple, nodes.List)): return targets = node.targets[0].itered() try: inferred = utils.safe_infer(node.value) if inferred is not None: self._check_unpacking(inferred, node, targets) except astroid.InferenceError: return # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_listcomp(self, _: nodes.ListComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def leave_assign(self, node: nodes.Assign) -> None: self._store_type_annotation_names(node) def leave_with(self, node: nodes.With) -> None: self._store_type_annotation_names(node) def visit_arguments(self, node: nodes.Arguments) -> None: for annotation in node.type_comment_args: self._store_type_annotation_node(annotation) # Relying on other checker's options, which might not have been initialized yet. @astroid.decorators.cachedproperty def _analyse_fallback_blocks(self): return get_global_option(self, "analyse-fallback-blocks", default=_False) @astroid.decorators.cachedproperty def _ignored_modules(self): return get_global_option(self, "ignored-modules", default=[]) @astroid.decorators.cachedproperty def _allow_global_unused_variables(self): return get_global_option(self, "allow-global-unused-variables", default=_True) @staticmethod def _defined_in_function_definition(node, frame): in_annotation_or_default_or_decorator = _False if ( isinstance(frame, nodes.FunctionDef) and node.statement(future=_True) is frame ): in_annotation_or_default_or_decorator = ( ( node in frame.args.annotations or node in frame.args.posonlyargs_annotations or node in frame.args.kwonlyargs_annotations or node is frame.args.varargannotation or node is frame.args.kwargannotation ) or frame.args.parent_of(node) or (frame.decorators and frame.decorators.parent_of(node)) or ( frame.returns and (node is frame.returns or frame.returns.parent_of(node)) ) ) return in_annotation_or_default_or_decorator @staticmethod def _in_lambda_or_comprehension_body( node: nodes.NodeNG, frame: nodes.NodeNG ) -> bool: """Return _True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.""" child = node parent = node.parent while parent is not None: if parent is frame: return _False if isinstance(parent, nodes.Lambda) and child is not parent.args: # Body of lambda should not have access to class attributes. return _True if isinstance(parent, nodes.Comprehension) and child is not parent.iter: # Only iter of list/set/dict/generator comprehension should have access. return _True if isinstance(parent, nodes.ComprehensionScope) and not ( parent.generators and child is parent.generators[0] ): # Body of list/set/dict/generator comprehension should not have access to class attributes. # Furthermore, only the first generator (if multiple) in comprehension should have access. return _True child = parent parent = parent.parent return _False @staticmethod def _is_variable_violation( node: nodes.Name, defnode, stmt: nodes.Statement, defstmt: nodes.Statement, frame, # scope of statement of node defframe, base_scope_type, is_recursive_klass, ) -> Tuple[bool, bool, bool]: # pylint: disable=too-many-nested-blocks maybe_before_assign = _True annotation_return = _False use_outer_definition = _False if frame is not defframe: maybe_before_assign = _detect_global_scope(node, frame, defframe) elif defframe.parent is None: # we are at the module level, check the name is not # defined in builtins if ( node.name in defframe.scope_attrs or astroid.builtin_lookup(node.name)[1] ): maybe_before_assign = _False else: # we are in a local scope, check the name is not # defined in global or builtin scope # skip this lookup if name is assigned later in function scope/lambda # Note: the node.frame() is not the same as the `frame` argument which is # equivalent to frame.statement().scope() forbid_lookup = ( isinstance(frame, nodes.FunctionDef) or isinstance(node.frame(future=_True), nodes.Lambda) ) and _assigned_locally(node) if not forbid_lookup and defframe.root().lookup(node.name)[1]: maybe_before_assign = _False use_outer_definition = stmt == defstmt and not isinstance( defnode, nodes.Comprehension ) # check if we have a nonlocal elif node.name in defframe.locals: maybe_before_assign = not any( isinstance(child, nodes.Nonlocal) and node.name in child.names for child in defframe.get_children() ) if ( base_scope_type == "lambda" and isinstance(frame, nodes.ClassDef) and node.name in frame.locals ): # This rule verifies that if the definition node of the # checked name is an Arguments node and if the name # is used a default value in the arguments defaults # and the actual definition of the variable label # is happening before the Arguments definition. # # bar = None # foo = lambda bar=bar: bar # # In this case, maybe_before_assign should be _False, otherwise # it should be _True. maybe_before_assign = not ( isinstance(defnode, nodes.Arguments) and node in defnode.defaults and frame.locals[node.name][0].fromlineno < defstmt.fromlineno ) elif isinstance(defframe, nodes.ClassDef) and isinstance( frame, nodes.FunctionDef ): # Special rule for function return annotations, # using a name defined earlier in the class containing the function. if node is frame.returns and defframe.parent_of(frame.returns): annotation_return = _True if ( frame.returns.name in defframe.locals and defframe.locals[node.name][0].lineno < frame.lineno ): # Detect class assignments with a name defined earlier in the # class. In this case, no warning should be raised. maybe_before_assign = _False else: maybe_before_assign = _True if isinstance(node.parent, nodes.Arguments): maybe_before_assign = stmt.fromlineno <= defstmt.fromlineno elif is_recursive_klass: maybe_before_assign = _True else: maybe_before_assign = ( maybe_before_assign and stmt.fromlineno <= defstmt.fromlineno ) if maybe_before_assign and stmt.fromlineno == defstmt.fromlineno: if ( isinstance(defframe, nodes.FunctionDef) and frame is defframe and defframe.parent_of(node) and stmt is not defstmt ): # Single statement function, with the statement on the # same line as the function definition maybe_before_assign = _False elif ( isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Expr, nodes.Return, ), ) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) and frame is defframe and defframe.parent_of(node) and stmt is defstmt ): # Single statement if, with assignment expression on same # line as assignment # x = b if (b := _True) else _False maybe_before_assign = _False elif ( isinstance( # pylint: disable=too-many-boolean-expressions defnode, nodes.NamedExpr ) and frame is defframe and defframe.parent_of(stmt) and stmt is defstmt and ( ( defnode.lineno == node.lineno and defnode.col_offset < node.col_offset ) or (defnode.lineno < node.lineno) or ( # Issue in the `ast` module until py39 # Nodes in a multiline string have the same lineno # Could be false-positive without check not PY39_PLUS and defnode.lineno == node.lineno and isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Return, ), ) and isinstance(defstmt.value, nodes.JoinedStr) ) ) ): # Expressions, with assignment expressions # Use only after assignment # b = (c := 2) and c maybe_before_assign = _False # Look for type checking definitions inside a type checking guard. if isinstance(defstmt, (nodes.Import, nodes.ImportFrom)): defstmt_parent = defstmt.parent if ( isinstance(defstmt_parent, nodes.If) and defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS ): # Exempt those definitions that are used inside the type checking # guard or that are defined in both type checking guard branches. used_in_branch = defstmt_parent.parent_of(node) defined_in_or_else = _False for definition in defstmt_parent.orelse: if isinstance(definition, nodes.Assign): defined_in_or_else = any( target.name == node.name for target in definition.targets if isinstance(target, nodes.AssignName) ) if defined_in_or_else: break if not used_in_branch and not defined_in_or_else: maybe_before_assign = _True return maybe_before_assign, annotation_return, use_outer_definition @staticmethod def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: """Check if `defstmt` has the potential to use and assign a name in the same statement. """ if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: # The assignment must happen as part of the first element # e.g. "assert (x:= _True), x" # NOT "assert x, (x:= _True)" value = defstmt.value.elts[0] else: value = defstmt.value if isinstance(value, nodes.IfExp): return _True if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp): return _True return isinstance(value, nodes.Call) and ( any(isinstance(kwarg.value, nodes.IfExp) for kwarg in value.keywords) or any(isinstance(arg, nodes.IfExp) for arg in value.args) ) @staticmethod def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool: """Check if variable only gets assigned a type and never a value.""" if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value: return _False defstmt_frame = defstmt.frame(future=_True) node_frame = node.frame(future=_True) parent = node while parent is not defstmt_frame.parent: parent_scope = parent.scope() local_refs = parent_scope.locals.get(node.name, []) for ref_node in local_refs: # If local ref is in the same frame as our node, but on a later lineno # we don't actually care about this local ref. # Local refs are ordered, so we break. # print(var) # var = 1 # <- irrelevant if defstmt_frame == node_frame and ref_node.lineno > node.lineno: break # If the parent of the local reference is anything but an AnnAssign # Or if the AnnAssign adds a value the variable will now have a value # var = 1 # OR # var: int = 1 if ( not isinstance(ref_node.parent, nodes.AnnAssign) or ref_node.parent.value ): return _False parent = parent_scope.parent return _True @staticmethod def _is_first_level_self_reference( node: nodes.Name, defstmt: nodes.ClassDef, found_nodes: List[nodes.NodeNG] ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Check if a first level method's annotation or default values refers to its own class, and return a consumer action """ if node.frame(future=_True).parent == defstmt and node.statement( future=_True ) == node.frame(future=_True): # Check if used as type annotation # Break if postponed evaluation is enabled if utils.is_node_in_type_annotation_context(node): if not utils.is_postponed_evaluation_enabled(node): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, None) # Check if used as default value by calling the class if isinstance(node.parent, nodes.Call) and isinstance( node.parent.parent, nodes.Arguments ): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, found_nodes) @staticmethod def _is_never_evaluated( defnode: nodes.NamedExpr, defnode_parent: nodes.IfExp ) -> bool: """Check if a NamedExpr is inside a side of if ... else that never gets evaluated """ inferred_test = utils.safe_infer(defnode_parent.test) if isinstance(inferred_test, nodes.Const): if inferred_test.value is _True and defnode == defnode_parent.orelse: return _True if inferred_test.value is _False and defnode == defnode_parent.body: return _True return _False def _ignore_class_scope(self, node): """Return _True if the node is in a local class scope, as an assignment. :param node: Node considered :type node: astroid.Node :return: _True if the node is in a local class scope, as an assignment. _False otherwise. :rtype: bool """ # Detect if we are in a local class scope, as an assignment. # For example, the following is fair game. # # class A: # b = 1 # c = lambda b=b: b * b # # class B: # tp = 1 # def func(self, arg: tp): # ... # class C: # tp = 2 # def func(self, arg=tp): # ... # class C: # class Tp: # pass # class D(Tp): # ... name = node.name frame = node.statement(future=_True).scope() in_annotation_or_default_or_decorator = self._defined_in_function_definition( node, frame ) in_ancestor_list = utils.is_ancestor_name(frame, node) if in_annotation_or_default_or_decorator or in_ancestor_list: frame_locals = frame.parent.scope().locals else: frame_locals = frame.locals return not ( (isinstance(frame, nodes.ClassDef) or in_annotation_or_default_or_decorator) and not self._in_lambda_or_comprehension_body(node, frame) and name in frame_locals ) def _loopvar_name(self, node: astroid.Name) -> None: # filter variables according to node's scope astmts = [s for s in node.lookup(node.name)[1] if hasattr(s, "assign_type")] # If this variable usage exists inside a function definition # that exists in the same loop, # the usage is safe because the function will not be defined either if # the variable is not defined. scope = node.scope() if isinstance(scope, nodes.FunctionDef) and any( asmt.scope().parent_of(scope) for asmt in astmts ): return # Filter variables according to their respective scope. Test parent # and statement to avoid #74747. This is not a total fix, which would # introduce a mechanism similar to special attribute lookup in # modules. Also, in order to get correct inference in this case, the # scope lookup rules would need to be changed to return the initial # assignment (which does not exist in code per se) as well as any later # modifications. # pylint: disable-next=too-many-boolean-expressions if ( not astmts or ( astmts[0].parent == astmts[0].root() and astmts[0].parent.parent_of(node) ) or ( astmts[0].is_statement or not isinstance(astmts[0].parent, nodes.Module) and astmts[0].statement(future=_True).parent_of(node) ) ): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement(future=_True).parent_of( stmt ) and not in_for_else_branch(astmts[i].statement(future=_True), stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) != 1: return assign = astmts[0].assign_type() if not ( isinstance(assign, (nodes.For, nodes.Comprehension, nodes.GeneratorExp)) and assign.statement(future=_True) is not node.statement(future=_True) ): return # For functions we can do more by inferring the length of the itered object if not isinstance(assign, nodes.For): self.add_message("undefined-loop-variable", args=node.name, node=node) return try: inferred = next(assign.iter.infer()) except astroid.InferenceError: self.add_message("undefined-loop-variable", args=node.name, node=node) else: if ( isinstance(inferred, astroid.Instance) and inferred.qname() == BUILTIN_RANGE ): # Consider range() objects safe, even if they might not yield any results. return # Consider sequences. sequences = ( nodes.List, nodes.Tuple, nodes.Dict, nodes.Set, astroid.objects.FrozenSet, ) if not isinstance(inferred, sequences): self.add_message("undefined-loop-variable", args=node.name, node=node) return elements = getattr(inferred, "elts", getattr(inferred, "items", [])) if not elements: self.add_message("undefined-loop-variable", args=node.name, node=node) def _check_is_unused(self, name, node, stmt, global_names, nonlocal_names): # Ignore some special names specified by user configuration. if self._is_name_ignored(stmt, name): return # Ignore names that were added dynamically to the Function scope if ( isinstance(node, nodes.FunctionDef) and name == "__class__" and len(node.locals["__class__"]) == 1 and isinstance(node.locals["__class__"][0], nodes.ClassDef) ): return # Ignore names imported by the global statement. if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)): # Detect imports, assigned to global statements. if global_names and _import_name_is_global(stmt, global_names): return argnames = list( itertools.chain(node.argnames(), [arg.name for arg in node.args.kwonlyargs]) ) # Care about functions with unknown argument (builtins) if name in argnames: self._check_unused_arguments(name, node, stmt, argnames) else: if stmt.parent and isinstance( stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple) ): if name in nonlocal_names: return qname = asname = None if isinstance(stmt, (nodes.Import, nodes.ImportFrom)): # Need the complete name, which we don't have in .locals. if len(stmt.names) > 1: import_names = next( (names for names in stmt.names if name in names), None ) else: import_names = stmt.names[0] if import_names: qname, asname = import_names name = asname or qname if _has_locals_call_after_node(stmt, node.scope()): message_name = "possibly-unused-variable" else: if isinstance(stmt, nodes.Import): if asname is not None: msg = f"{qname} imported as {asname}" else: msg = f"import {name}" self.add_message("unused-import", args=msg, node=stmt) return if isinstance(stmt, nodes.ImportFrom): if asname is not None: msg = f"{qname} imported from {stmt.modname} as {asname}" else: msg = f"{name} imported from {stmt.modname}" self.add_message("unused-import", args=msg, node=stmt) return message_name = "unused-variable" if isinstance(stmt, nodes.FunctionDef) and stmt.decorators: return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Special case for exception variable if isinstance(stmt.parent, nodes.ExceptHandler) and any( n.name == name for n in stmt.parent.nodes_of_class(nodes.Name) ): return self.add_message(message_name, args=name, node=stmt) def _is_name_ignored(self, stmt, name): authorized_rgx = self.config.dummy_variables_rgx if ( isinstance(stmt, nodes.AssignName) and isinstance(stmt.parent, nodes.Arguments) or isinstance(stmt, nodes.Arguments) ): regex = self.config.ignored_argument_names else: regex = authorized_rgx return regex and regex.match(name) def _check_unused_arguments(self, name, node, stmt, argnames): is_method = node.is_method() klass = node.parent.frame(future=_True) if is_method and isinstance(klass, nodes.ClassDef): confidence = ( INFERENCE if utils.has_known_bases(klass) else INFERENCE_FAILURE ) else: confidence = HIGH if is_method: # Don't warn for the first argument of a (non static) method if node.type != "staticmethod" and name == argnames[0]: return # Don't warn for argument of an overridden method overridden = overridden_method(klass, node.name) if overridden is not None and name in overridden.argnames(): return if node.name in utils.PYMETHODS and node.name not in ( "__init__", "__new__", ): return # Don't check callback arguments if any( node.name.startswith(cb) or node.name.endswith(cb) for cb in self.config.callbacks ): return # Don't check arguments of singledispatch.register function. if utils.is_registered_in_singledispatch_function(node): return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Don't check protocol classes if utils.is_protocol_class(klass): return self.add_message("unused-argument", args=name, node=stmt, confidence=confidence) def _check_late_binding_closure(self, node: nodes.Name) -> None: """Check whether node is a cell var that is assigned within a containing loop. Special cases where we don't care about the error: 1. When the node's function is immediately called, e.g. (lambda: i)() 2. When the node's function is returned from within the loop, e.g. return lambda: i """ if not self.linter.is_message_enabled("cell-var-from-loop"): return node_scope = node.frame(future=_True) # If node appears in a default argument expression, # look at the next enclosing frame instead if utils.is_default_argument(node, node_scope): node_scope = node_scope.parent.frame(future=_True) # Check if node is a cell var if ( not isinstance(node_scope, (nodes.Lambda, nodes.FunctionDef)) or node.name in node_scope.locals ): return assign_scope, stmts = node.lookup(node.name) if not stmts or not assign_scope.parent_of(node_scope): return if utils.is_comprehension(assign_scope): self.add_message("cell-var-from-loop", node=node, args=node.name) else: # Look for an enclosing For loop. # Currently, we only consider the first assignment assignment_node = stmts[0] maybe_for = assignment_node while maybe_for and not isinstance(maybe_for, nodes.For): if maybe_for is assign_scope: break maybe_for = maybe_for.parent else: if ( maybe_for and maybe_for.parent_of(node_scope) and not utils.is_being_called(node_scope) and node_scope.parent and not isinstance(node_scope.statement(future=_True), nodes.Return) ): self.add_message("cell-var-from-loop", node=node, args=node.name) def _should_ignore_redefined_builtin(self, stmt): if not isinstance(stmt, nodes.ImportFrom): return _False return stmt.modname in self.config.redefining_builtins_modules def _allowed_redefined_builtin(self, name): return name in self.config.allowed_redefined_builtins def _has_homonym_in_upper_function_scope( self, node: nodes.Name, index: int ) -> bool: """Return whether there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function :param node: node to check for :param index: index of the current consumer inside self._to_consume :return: _True if there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function, _False otherwise """ return any( _consumer.scope_type == "function" and node.name in _consumer.to_consume for _consumer in self._to_consume[index - 1 :: -1] ) def _store_type_annotation_node(self, type_annotation): """Given a type annotation, store all the name nodes it refers to.""" if isinstance(type_annotation, nodes.Name): self._type_annotation_names.append(type_annotation.name) return if isinstance(type_annotation, nodes.Attribute): self._store_type_annotation_node(type_annotation.expr) return if not isinstance(type_annotation, nodes.Subscript): return if ( isinstance(type_annotation.value, nodes.Attribute) and isinstance(type_annotation.value.expr, nodes.Name) and type_annotation.value.expr.name == TYPING_MODULE ): self._type_annotation_names.append(TYPING_MODULE) return self._type_annotation_names.extend( annotation.name for annotation in type_annotation.nodes_of_class(nodes.Name) ) def _store_type_annotation_names(self, node): type_annotation = node.type_annotation if not type_annotation: return self._store_type_annotation_node(node.type_annotation) def _check_self_cls_assign(self, node: nodes.Assign) -> None: """Check that self/cls don't get assigned.""" assign_names: Set[Optional[str]] = set() for target in node.targets: if isinstance(target, nodes.AssignName): assign_names.add(target.name) elif isinstance(target, nodes.Tuple): assign_names.update( elt.name for elt in target.elts if isinstance(elt, nodes.AssignName) ) scope = node.scope() nonlocals_with_same_name = any( child for child in scope.body if isinstance(child, nodes.Nonlocal) ) if nonlocals_with_same_name: scope = node.scope().parent.scope() if not ( isinstance(scope, nodes.FunctionDef) and scope.is_method() and "builtins.staticmethod" not in scope.decoratornames() ): return argument_names = scope.argnames() if not argument_names: return self_cls_name = argument_names[0] if self_cls_name in assign_names: self.add_message("self-cls-assignment", node=node, args=(self_cls_name,)) def _check_unpacking(self, inferred, node, targets): """Check for unbalanced tuple unpacking and unpacking non sequences. """ if utils.is_inside_abstract_class(node): return if utils.is_comprehension(node): return if inferred is astroid.Uninferable: return if ( isinstance(inferred.parent, nodes.Arguments) and isinstance(node.value, nodes.Name) and node.value.name == inferred.parent.vararg ): # Variable-length argument, we can't determine the length. return # Attempt to check unpacking is properly balanced values = self._nodes_to_unpack(inferred) if values is not None: if len(targets) != len(values): # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return self.add_message( "unbalanced-tuple-unpacking", node=node, args=( _get_unpacking_extra_info(node, inferred), len(targets), len(values), ), ) # attempt to check unpacking may be possible (ie RHS is iterable) elif not utils.is_iterable(inferred): self.add_message( "unpacking-non-sequence", node=node, args=(_get_unpacking_extra_info(node, inferred),), ) @staticmethod def _nodes_to_unpack(node: nodes.NodeNG) -> Optional[List[nodes.NodeNG]]: """Return the list of values of the `Assign` node.""" if isinstance(node, (nodes.Tuple, nodes.List)): return node.itered() if isinstance(node, astroid.Instance) and any( ancestor.qname() == "typing.NamedTuple" for ancestor in node.ancestors() ): return [i for i in node.values() if isinstance(i, nodes.AssignName)] return None def _check_module_attrs(self, node, module, module_names): """Check that module_names (list of string) are accessible through the given module if the latest access name corresponds to a module, return it """ while module_names: name = module_names.pop(0) if name == "__dict__": module = None break try: module = next(module.getattr(name)[0].infer()) if module is astroid.Uninferable: return None except astroid.NotFoundError: if module.name in self._ignored_modules: return None self.add_message( "no-name-in-module", args=(name, module.name), node=node ) return None except astroid.InferenceError: return None if module_names: modname = module.name if module else "__dict__" self.add_message( "no-name-in-module", node=node, args=(".".join(module_names), modname) ) return None if isinstance(module, nodes.Module): return module return None def _check_all(self, node: nodes.Module, not_consumed): assigned = next(node.igetattr("__all__")) if assigned is astroid.Uninferable: return if not assigned.pytype() in {"builtins.list", "builtins.tuple"}: line, col = assigned.tolineno, assigned.col_offset self.add_message("invalid-all-format", line=line, col_offset=col, node=node) return for elt in getattr(assigned, "elts", ()): try: elt_name = next(elt.infer()) except astroid.InferenceError: continue if elt_name is astroid.Uninferable: continue if not elt_name.parent: continue if not isinstance(elt_name, nodes.Const) or not isinstance( elt_name.value, str ): self.add_message("invalid-all-object", args=elt.as_string(), node=elt) continue elt_name = elt_name.value # If elt is in not_consumed, remove it from not_consumed if elt_name in not_consumed: del not_consumed[elt_name] continue if elt_name not in node.locals: if not node.package: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) else: basename = os.path.splitext(node.file)[0] if os.path.basename(basename) == "__init__": name = node.name + "." + elt_name try: astroid.modutils.file_from_modpath(name.split(".")) except ImportError: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) except SyntaxError: # don't yield a syntax-error warning, # because it will be later yielded # when the file will be checked pass def _check_globals(self, not_consumed): if self._allow_global_unused_variables: return for name, node_lst in not_consumed.items(): for node in node_lst: self.add_message("unused-variable", args=(name,), node=node) def _check_imports(self, not_consumed): local_names = _fix_dot_imports(not_consumed) checked = set() unused_wildcard_imports: DefaultDict[ Tuple[str, nodes.ImportFrom], List[str] ] = collections.defaultdict(list) for name, stmt in local_names: for imports in stmt.names: real_name = imported_name = imports[0] if imported_name == "*": real_name = name as_name = imports[1] if real_name in checked: continue if name not in (real_name, as_name): continue checked.add(real_name) is_type_annotation_import = ( imported_name in self._type_annotation_names or as_name in self._type_annotation_names ) if isinstance(stmt, nodes.Import) or ( isinstance(stmt, nodes.ImportFrom) and not stmt.modname ): if isinstance(stmt, nodes.ImportFrom) and SPECIAL_OBJ.search( imported_name ): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if as_name == "_": continue if as_name is None: msg = f"import {imported_name}" else: msg = f"{imported_name} imported as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) elif isinstance(stmt, nodes.ImportFrom) and stmt.modname != FUTURE: if SPECIAL_OBJ.search(imported_name): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if _is_from_future_import(stmt, name): # Check if the name is in fact loaded from a # __future__ import in another module. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if imported_name == "*": unused_wildcard_imports[(stmt.modname, stmt)].append(name) else: if as_name is None: msg = f"{imported_name} imported from {stmt.modname}" else: msg = f"{imported_name} imported from {stmt.modname} as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) # Construct string for unused-wildcard-import message for module, unused_list in unused_wildcard_imports.items(): if len(unused_list) == 1: arg_string = unused_list[0] else: arg_string = ( f"{', '.join(i for i in unused_list[:-1])} and {unused_list[-1]}" ) self.add_message( "unused-wildcard-import", args=(arg_string, module[0]), node=module[1] ) del self._to_consume def _check_metaclasses(self, node): """Update consumption analysis for metaclasses.""" consumed = [] # [(scope_locals, consumed_key)] for child_node in node.get_children(): if isinstance(child_node, nodes.ClassDef): consumed.extend(self._check_classdef_metaclasses(child_node, node)) # Pop the consumed items, in order to avoid having # unused-import and unused-variable false positives for scope_locals, name in consumed: scope_locals.pop(name, None) def _check_classdef_metaclasses(self, klass, parent_node): if not klass._metaclass: # Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors return [] consumed = [] # [(scope_locals, consumed_key)] metaclass = klass.metaclass() name = None if isinstance(klass._metaclass, nodes.Name): name = klass._metaclass.name elif isinstance(klass._metaclass, nodes.Attribute) and klass._metaclass.expr: attr = klass._metaclass.expr while not isinstance(attr, nodes.Name): attr = attr.expr name = attr.name elif metaclass: name = metaclass.root().name found = _False name = METACLASS_NAME_TRANSFORMS.get(name, name) if name: # check enclosing scopes starting from most local for scope_locals, _, _, _ in self._to_consume[::-1]: found_nodes = scope_locals.get(name, []) for found_node in found_nodes: if found_node.lineno <= klass.lineno: consumed.append((scope_locals, name)) found = _True break # Check parent scope nodes_in_parent_scope = parent_node.locals.get(name, []) for found_node_parent in nodes_in_parent_scope: if found_node_parent.lineno <= klass.lineno: found = _True break if ( not found and not metaclass and not ( name in nodes.Module.scope_attrs or utils.is_builtin(name) or name in self.config.additional_builtins ) ): self.add_message("undefined-variable", node=klass, args=(name,)) return consumed
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,212
NamesConsumer
ref
function
self._to_consume.append(NamesConsumer(node, "comprehension"))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,214
leave_dictcomp
def
function
def leave_dictcomp(self, _: nodes.DictComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_setcomp(self, node: nodes.SetComp) -> None: """Visit setcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_setcomp(self, _: nodes.SetComp) -> None: """Leave setcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Visit function: update consumption analysis variable and check locals.""" self._to_consume.append(NamesConsumer(node, "function")) if not ( self.linter.is_message_enabled("redefined-outer-name") or self.linter.is_message_enabled("redefined-builtin") ): return globs = node.root().globals for name, stmt in node.items(): if name in globs and not isinstance(stmt, nodes.Global): definition = globs[name][0] if ( isinstance(definition, nodes.ImportFrom) and definition.modname == FUTURE ): # It is a __future__ directive, not a symbol. continue # Do not take in account redefined names for the purpose # of type checking.: if any( isinstance(definition.parent, nodes.If) and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS for definition in globs[name] ): continue line = definition.fromlineno if not self._is_name_ignored(stmt, name): self.add_message( "redefined-outer-name", args=(name, line), node=stmt ) elif ( utils.is_builtin(name) and not self._allowed_redefined_builtin(name) and not self._should_ignore_redefined_builtin(stmt) ): # do not print Redefining builtin for additional builtins self.add_message("redefined-builtin", args=name, node=stmt) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Leave function: check function's locals are consumed.""" self._check_metaclasses(node) if node.type_comment_returns: self._store_type_annotation_node(node.type_comment_returns) if node.type_comment_args: for argument_annotation in node.type_comment_args: self._store_type_annotation_node(argument_annotation) not_consumed = self._to_consume.pop().to_consume if not ( self.linter.is_message_enabled("unused-variable") or self.linter.is_message_enabled("possibly-unused-variable") or self.linter.is_message_enabled("unused-argument") ): return # Don't check arguments of function which are only raising an exception. if utils.is_error(node): return # Don't check arguments of abstract methods or within an interface. is_method = node.is_method() if is_method and node.is_abstract(): return global_names = _flattened_scope_names(node.nodes_of_class(nodes.Global)) nonlocal_names = _flattened_scope_names(node.nodes_of_class(nodes.Nonlocal)) for name, stmts in not_consumed.items(): self._check_is_unused(name, node, stmts[0], global_names, nonlocal_names) visit_asyncfunctiondef = visit_functiondef leave_asyncfunctiondef = leave_functiondef @utils.check_messages( "global-variable-undefined", "global-variable-not-assigned", "global-statement", "global-at-module-level", "redefined-builtin", ) def visit_global(self, node: nodes.Global) -> None: """Check names imported exists in the global scope.""" frame = node.frame(future=_True) if isinstance(frame, nodes.Module): self.add_message("global-at-module-level", node=node) return module = frame.root() default_message = _True locals_ = node.scope().locals for name in node.names: try: assign_nodes = module.getattr(name) except astroid.NotFoundError: # unassigned global, skip assign_nodes = [] not_defined_locally_by_import = not any( isinstance(local, nodes.Import) for local in locals_.get(name, ()) ) if ( not utils.is_reassigned_after_current(node, name) and not utils.is_deleted_after_current(node, name) and not_defined_locally_by_import ): self.add_message("global-variable-not-assigned", args=name, node=node) default_message = _False continue for anode in assign_nodes: if ( isinstance(anode, nodes.AssignName) and anode.name in module.special_attributes ): self.add_message("redefined-builtin", args=name, node=node) break if anode.frame(future=_True) is module: # module level assignment break if ( isinstance(anode, (nodes.ClassDef, nodes.FunctionDef)) and anode.parent is module ): # module level function assignment break else: if not_defined_locally_by_import: # global undefined at the module scope self.add_message("global-variable-undefined", args=name, node=node) default_message = _False if default_message: self.add_message("global-statement", node=node) def visit_assignname(self, node: nodes.AssignName) -> None: if isinstance(node.assign_type(), nodes.AugAssign): self.visit_name(node) def visit_delname(self, node: nodes.DelName) -> None: self.visit_name(node) def visit_name(self, node: nodes.Name) -> None: """Don't add the 'utils.check_messages' decorator here! It's important that all 'Name' nodes are visited, otherwise the 'NamesConsumers' won't be correct. """ stmt = node.statement(future=_True) if stmt.fromlineno is None: # name node from an astroid built from live code, skip assert not stmt.root().file.endswith(".py") return self._undefined_and_used_before_checker(node, stmt) if self._is_undefined_loop_variable_enabled: self._loopvar_name(node) @utils.check_messages("redefined-outer-name") def visit_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return for outer_except, outer_except_assign_name in self._except_handler_names_queue: if node.name.name == outer_except_assign_name.name: self.add_message( "redefined-outer-name", args=(outer_except_assign_name.name, outer_except.fromlineno), node=node, ) break self._except_handler_names_queue.append((node, node.name)) @utils.check_messages("redefined-outer-name") def leave_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return self._except_handler_names_queue.pop() def _undefined_and_used_before_checker( self, node: nodes.Name, stmt: nodes.NodeNG ) -> None: frame = stmt.scope() start_index = len(self._to_consume) - 1 # iterates through parent scopes, from the inner to the outer base_scope_type = self._to_consume[start_index].scope_type for i in range(start_index, -1, -1): current_consumer = self._to_consume[i] # Certain nodes shouldn't be checked as they get checked another time if self._should_node_be_skipped(node, current_consumer, i == start_index): continue action, nodes_to_consume = self._check_consumer( node, stmt, frame, current_consumer, i, base_scope_type ) if nodes_to_consume: # Any nodes added to consumed_uncertain by get_next_to_consume() # should be added back so that they are marked as used. # They will have already had a chance to emit used-before-assignment. # We check here instead of before every single return in _check_consumer() nodes_to_consume += current_consumer.consumed_uncertain[node.name] current_consumer.mark_as_consumed(node.name, nodes_to_consume) if action is VariableVisitConsumerAction.CONTINUE: continue if action is VariableVisitConsumerAction.RETURN: return # we have not found the name, if it isn't a builtin, that's an # undefined name ! if ( self._is_undefined_variable_enabled and not ( node.name in nodes.Module.scope_attrs or utils.is_builtin(node.name) or node.name in self.config.additional_builtins or ( node.name == "__class__" and isinstance(frame, nodes.FunctionDef) and frame.is_method() ) ) and not utils.node_ignores_exception(node, NameError) ): self.add_message("undefined-variable", args=node.name, node=node) def _should_node_be_skipped( self, node: nodes.Name, consumer: NamesConsumer, is_start_index: bool ) -> bool: """Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks. """ if consumer.scope_type == "class": # The list of base classes in the class definition is not part # of the class body. # If the current scope is a class scope but it's not the inner # scope, ignore it. This prevents to access this scope instead of # the globals one in function members when there are some common # names. if utils.is_ancestor_name(consumer.node, node) or ( not is_start_index and self._ignore_class_scope(node) ): return _True # Ignore inner class scope for keywords in class definition if isinstance(node.parent, nodes.Keyword) and isinstance( node.parent.parent, nodes.ClassDef ): return _True elif consumer.scope_type == "function" and self._defined_in_function_definition( node, consumer.node ): # If the name node is used as a function default argument's value or as # a decorator, then start from the parent frame of the function instead # of the function frame - and thus open an inner class scope return _True elif consumer.scope_type == "lambda" and utils.is_default_argument( node, consumer.node ): return _True return _False # pylint: disable=too-many-return-statements def _check_consumer( self, node: nodes.Name, stmt: nodes.NodeNG, frame: nodes.LocalsDictNodeNG, current_consumer: NamesConsumer, consumer_level: int, base_scope_type: Any, ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Checks a consumer for conditions that should trigger messages.""" # If the name has already been consumed, only check it's not a loop # variable used outside the loop. # Avoid the case where there are homonyms inside function scope and # comprehension current scope (avoid bug #1731) if node.name in current_consumer.consumed: if utils.is_func_decorator(current_consumer.node) or not ( current_consumer.scope_type == "comprehension" and self._has_homonym_in_upper_function_scope(node, consumer_level) # But don't catch homonyms against the filter of a comprehension, # (like "if x" in "[x for x in expr() if x]") # https://github.com/PyCQA/pylint/issues/5586 and not ( ( isinstance(node.parent.parent, nodes.Comprehension) and node.parent in node.parent.parent.ifs ) # Or homonyms against values to keyword arguments # (like "var" in "[func(arg=var) for var in expr()]") or ( isinstance(node.scope(), nodes.ComprehensionScope) and isinstance(node.parent, (nodes.Call, nodes.Keyword)) ) ) ): self._check_late_binding_closure(node) self._loopvar_name(node) return (VariableVisitConsumerAction.RETURN, None) found_nodes = current_consumer.get_next_to_consume(node) if found_nodes is None: return (VariableVisitConsumerAction.CONTINUE, None) if not found_nodes: if node.name in current_consumer.consumed_uncertain: confidence = CONTROL_FLOW else: confidence = HIGH self.add_message( "used-before-assignment", args=node.name, node=node, confidence=confidence, ) # Mark for consumption any nodes added to consumed_uncertain by # get_next_to_consume() because they might not have executed. return ( VariableVisitConsumerAction.RETURN, current_consumer.consumed_uncertain[node.name], ) self._check_late_binding_closure(node) if not ( self._is_undefined_variable_enabled or self._is_used_before_assignment_enabled ): return (VariableVisitConsumerAction.RETURN, found_nodes) defnode = utils.assign_parent(found_nodes[0]) defstmt = defnode.statement(future=_True) defframe = defstmt.frame(future=_True) # The class reuses itself in the class scope. is_recursive_klass = ( frame is defframe and defframe.parent_of(node) and isinstance(defframe, nodes.ClassDef) and node.name == defframe.name ) if ( is_recursive_klass and utils.get_node_first_ancestor_of_type(node, nodes.Lambda) and ( not utils.is_default_argument(node) or node.scope().parent.scope() is not defframe ) ): # Self-referential class references are fine in lambda's -- # As long as they are not part of the default argument directly # under the scope of the parent self-referring class. # Example of valid default argument: # class MyName3: # myattr = 1 # mylambda3 = lambda: lambda a=MyName3: a # Example of invalid default argument: # class MyName4: # myattr = 1 # mylambda4 = lambda a=MyName4: lambda: a # If the above conditional is _True, # there is no possibility of undefined-variable # Also do not consume class name # (since consuming blocks subsequent checks) # -- quit return (VariableVisitConsumerAction.RETURN, None) ( maybe_before_assign, annotation_return, use_outer_definition, ) = self._is_variable_violation( node, defnode, stmt, defstmt, frame, defframe, base_scope_type, is_recursive_klass, ) if use_outer_definition: return (VariableVisitConsumerAction.CONTINUE, None) if ( maybe_before_assign and not utils.is_defined_before(node) and not astroid.are_exclusive(stmt, defstmt, ("NameError",)) ): # Used and defined in the same place, e.g `x += 1` and `del x` defined_by_stmt = defstmt is stmt and isinstance( node, (nodes.DelName, nodes.AssignName) ) if ( is_recursive_klass or defined_by_stmt or annotation_return or isinstance(defstmt, nodes.Delete) ): if not utils.node_ignores_exception(node, NameError): # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance( stmt, ( nodes.AnnAssign, nodes.FunctionDef, nodes.Arguments, ), ) and node.name in node.root().locals ): if defined_by_stmt: return (VariableVisitConsumerAction.CONTINUE, [node]) return (VariableVisitConsumerAction.CONTINUE, None) elif base_scope_type != "lambda": # E0601 may *not* occurs in lambda scope. # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif base_scope_type == "lambda": # E0601 can occur in class-level scope in lambdas, as in # the following example: # class A: # x = lambda attr: f + attr # f = 42 # We check lineno because doing the following is fine: # class A: # x = 42 # y = lambda attr: x + attr if ( isinstance(frame, nodes.ClassDef) and node.name in frame.locals and stmt.fromlineno <= defstmt.fromlineno ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) elif self._is_only_type_assignment(node, defstmt): if node.scope().locals.get(node.name): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH ) else: self.add_message( "undefined-variable", args=node.name, node=node, confidence=HIGH ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif isinstance(defstmt, nodes.ClassDef): return self._is_first_level_self_reference(node, defstmt, found_nodes) elif isinstance(defnode, nodes.NamedExpr): if isinstance(defnode.parent, nodes.IfExp): if self._is_never_evaluated(defnode, defnode.parent): self.add_message( "undefined-variable", args=node.name, node=node, confidence=INFERENCE, ) return (VariableVisitConsumerAction.RETURN, found_nodes) return (VariableVisitConsumerAction.RETURN, found_nodes) @utils.check_messages("no-name-in-module") def visit_import(self, node: nodes.Import) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return for name, _ in node.names: parts = name.split(".") try: module = next(_infer_name_module(node, parts[0])) except astroid.ResolveError: continue if not isinstance(module, nodes.Module): continue self._check_module_attrs(node, module, parts[1:]) @utils.check_messages("no-name-in-module") def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return name_parts = node.modname.split(".") try: module = node.do_import_module(name_parts[0]) except astroid.AstroidBuildingException: return module = self._check_module_attrs(node, module, name_parts[1:]) if not module: return for name, _ in node.names: if name == "*": continue self._check_module_attrs(node, module, name.split(".")) @utils.check_messages( "unbalanced-tuple-unpacking", "unpacking-non-sequence", "self-cls-assignment" ) def visit_assign(self, node: nodes.Assign) -> None: """Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned. """ self._check_self_cls_assign(node) if not isinstance(node.targets[0], (nodes.Tuple, nodes.List)): return targets = node.targets[0].itered() try: inferred = utils.safe_infer(node.value) if inferred is not None: self._check_unpacking(inferred, node, targets) except astroid.InferenceError: return # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_listcomp(self, _: nodes.ListComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def leave_assign(self, node: nodes.Assign) -> None: self._store_type_annotation_names(node) def leave_with(self, node: nodes.With) -> None: self._store_type_annotation_names(node) def visit_arguments(self, node: nodes.Arguments) -> None: for annotation in node.type_comment_args: self._store_type_annotation_node(annotation) # Relying on other checker's options, which might not have been initialized yet. @astroid.decorators.cachedproperty def _analyse_fallback_blocks(self): return get_global_option(self, "analyse-fallback-blocks", default=_False) @astroid.decorators.cachedproperty def _ignored_modules(self): return get_global_option(self, "ignored-modules", default=[]) @astroid.decorators.cachedproperty def _allow_global_unused_variables(self): return get_global_option(self, "allow-global-unused-variables", default=_True) @staticmethod def _defined_in_function_definition(node, frame): in_annotation_or_default_or_decorator = _False if ( isinstance(frame, nodes.FunctionDef) and node.statement(future=_True) is frame ): in_annotation_or_default_or_decorator = ( ( node in frame.args.annotations or node in frame.args.posonlyargs_annotations or node in frame.args.kwonlyargs_annotations or node is frame.args.varargannotation or node is frame.args.kwargannotation ) or frame.args.parent_of(node) or (frame.decorators and frame.decorators.parent_of(node)) or ( frame.returns and (node is frame.returns or frame.returns.parent_of(node)) ) ) return in_annotation_or_default_or_decorator @staticmethod def _in_lambda_or_comprehension_body( node: nodes.NodeNG, frame: nodes.NodeNG ) -> bool: """Return _True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.""" child = node parent = node.parent while parent is not None: if parent is frame: return _False if isinstance(parent, nodes.Lambda) and child is not parent.args: # Body of lambda should not have access to class attributes. return _True if isinstance(parent, nodes.Comprehension) and child is not parent.iter: # Only iter of list/set/dict/generator comprehension should have access. return _True if isinstance(parent, nodes.ComprehensionScope) and not ( parent.generators and child is parent.generators[0] ): # Body of list/set/dict/generator comprehension should not have access to class attributes. # Furthermore, only the first generator (if multiple) in comprehension should have access. return _True child = parent parent = parent.parent return _False @staticmethod def _is_variable_violation( node: nodes.Name, defnode, stmt: nodes.Statement, defstmt: nodes.Statement, frame, # scope of statement of node defframe, base_scope_type, is_recursive_klass, ) -> Tuple[bool, bool, bool]: # pylint: disable=too-many-nested-blocks maybe_before_assign = _True annotation_return = _False use_outer_definition = _False if frame is not defframe: maybe_before_assign = _detect_global_scope(node, frame, defframe) elif defframe.parent is None: # we are at the module level, check the name is not # defined in builtins if ( node.name in defframe.scope_attrs or astroid.builtin_lookup(node.name)[1] ): maybe_before_assign = _False else: # we are in a local scope, check the name is not # defined in global or builtin scope # skip this lookup if name is assigned later in function scope/lambda # Note: the node.frame() is not the same as the `frame` argument which is # equivalent to frame.statement().scope() forbid_lookup = ( isinstance(frame, nodes.FunctionDef) or isinstance(node.frame(future=_True), nodes.Lambda) ) and _assigned_locally(node) if not forbid_lookup and defframe.root().lookup(node.name)[1]: maybe_before_assign = _False use_outer_definition = stmt == defstmt and not isinstance( defnode, nodes.Comprehension ) # check if we have a nonlocal elif node.name in defframe.locals: maybe_before_assign = not any( isinstance(child, nodes.Nonlocal) and node.name in child.names for child in defframe.get_children() ) if ( base_scope_type == "lambda" and isinstance(frame, nodes.ClassDef) and node.name in frame.locals ): # This rule verifies that if the definition node of the # checked name is an Arguments node and if the name # is used a default value in the arguments defaults # and the actual definition of the variable label # is happening before the Arguments definition. # # bar = None # foo = lambda bar=bar: bar # # In this case, maybe_before_assign should be _False, otherwise # it should be _True. maybe_before_assign = not ( isinstance(defnode, nodes.Arguments) and node in defnode.defaults and frame.locals[node.name][0].fromlineno < defstmt.fromlineno ) elif isinstance(defframe, nodes.ClassDef) and isinstance( frame, nodes.FunctionDef ): # Special rule for function return annotations, # using a name defined earlier in the class containing the function. if node is frame.returns and defframe.parent_of(frame.returns): annotation_return = _True if ( frame.returns.name in defframe.locals and defframe.locals[node.name][0].lineno < frame.lineno ): # Detect class assignments with a name defined earlier in the # class. In this case, no warning should be raised. maybe_before_assign = _False else: maybe_before_assign = _True if isinstance(node.parent, nodes.Arguments): maybe_before_assign = stmt.fromlineno <= defstmt.fromlineno elif is_recursive_klass: maybe_before_assign = _True else: maybe_before_assign = ( maybe_before_assign and stmt.fromlineno <= defstmt.fromlineno ) if maybe_before_assign and stmt.fromlineno == defstmt.fromlineno: if ( isinstance(defframe, nodes.FunctionDef) and frame is defframe and defframe.parent_of(node) and stmt is not defstmt ): # Single statement function, with the statement on the # same line as the function definition maybe_before_assign = _False elif ( isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Expr, nodes.Return, ), ) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) and frame is defframe and defframe.parent_of(node) and stmt is defstmt ): # Single statement if, with assignment expression on same # line as assignment # x = b if (b := _True) else _False maybe_before_assign = _False elif ( isinstance( # pylint: disable=too-many-boolean-expressions defnode, nodes.NamedExpr ) and frame is defframe and defframe.parent_of(stmt) and stmt is defstmt and ( ( defnode.lineno == node.lineno and defnode.col_offset < node.col_offset ) or (defnode.lineno < node.lineno) or ( # Issue in the `ast` module until py39 # Nodes in a multiline string have the same lineno # Could be false-positive without check not PY39_PLUS and defnode.lineno == node.lineno and isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Return, ), ) and isinstance(defstmt.value, nodes.JoinedStr) ) ) ): # Expressions, with assignment expressions # Use only after assignment # b = (c := 2) and c maybe_before_assign = _False # Look for type checking definitions inside a type checking guard. if isinstance(defstmt, (nodes.Import, nodes.ImportFrom)): defstmt_parent = defstmt.parent if ( isinstance(defstmt_parent, nodes.If) and defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS ): # Exempt those definitions that are used inside the type checking # guard or that are defined in both type checking guard branches. used_in_branch = defstmt_parent.parent_of(node) defined_in_or_else = _False for definition in defstmt_parent.orelse: if isinstance(definition, nodes.Assign): defined_in_or_else = any( target.name == node.name for target in definition.targets if isinstance(target, nodes.AssignName) ) if defined_in_or_else: break if not used_in_branch and not defined_in_or_else: maybe_before_assign = _True return maybe_before_assign, annotation_return, use_outer_definition @staticmethod def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: """Check if `defstmt` has the potential to use and assign a name in the same statement. """ if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: # The assignment must happen as part of the first element # e.g. "assert (x:= _True), x" # NOT "assert x, (x:= _True)" value = defstmt.value.elts[0] else: value = defstmt.value if isinstance(value, nodes.IfExp): return _True if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp): return _True return isinstance(value, nodes.Call) and ( any(isinstance(kwarg.value, nodes.IfExp) for kwarg in value.keywords) or any(isinstance(arg, nodes.IfExp) for arg in value.args) ) @staticmethod def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool: """Check if variable only gets assigned a type and never a value.""" if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value: return _False defstmt_frame = defstmt.frame(future=_True) node_frame = node.frame(future=_True) parent = node while parent is not defstmt_frame.parent: parent_scope = parent.scope() local_refs = parent_scope.locals.get(node.name, []) for ref_node in local_refs: # If local ref is in the same frame as our node, but on a later lineno # we don't actually care about this local ref. # Local refs are ordered, so we break. # print(var) # var = 1 # <- irrelevant if defstmt_frame == node_frame and ref_node.lineno > node.lineno: break # If the parent of the local reference is anything but an AnnAssign # Or if the AnnAssign adds a value the variable will now have a value # var = 1 # OR # var: int = 1 if ( not isinstance(ref_node.parent, nodes.AnnAssign) or ref_node.parent.value ): return _False parent = parent_scope.parent return _True @staticmethod def _is_first_level_self_reference( node: nodes.Name, defstmt: nodes.ClassDef, found_nodes: List[nodes.NodeNG] ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Check if a first level method's annotation or default values refers to its own class, and return a consumer action """ if node.frame(future=_True).parent == defstmt and node.statement( future=_True ) == node.frame(future=_True): # Check if used as type annotation # Break if postponed evaluation is enabled if utils.is_node_in_type_annotation_context(node): if not utils.is_postponed_evaluation_enabled(node): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, None) # Check if used as default value by calling the class if isinstance(node.parent, nodes.Call) and isinstance( node.parent.parent, nodes.Arguments ): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, found_nodes) @staticmethod def _is_never_evaluated( defnode: nodes.NamedExpr, defnode_parent: nodes.IfExp ) -> bool: """Check if a NamedExpr is inside a side of if ... else that never gets evaluated """ inferred_test = utils.safe_infer(defnode_parent.test) if isinstance(inferred_test, nodes.Const): if inferred_test.value is _True and defnode == defnode_parent.orelse: return _True if inferred_test.value is _False and defnode == defnode_parent.body: return _True return _False def _ignore_class_scope(self, node): """Return _True if the node is in a local class scope, as an assignment. :param node: Node considered :type node: astroid.Node :return: _True if the node is in a local class scope, as an assignment. _False otherwise. :rtype: bool """ # Detect if we are in a local class scope, as an assignment. # For example, the following is fair game. # # class A: # b = 1 # c = lambda b=b: b * b # # class B: # tp = 1 # def func(self, arg: tp): # ... # class C: # tp = 2 # def func(self, arg=tp): # ... # class C: # class Tp: # pass # class D(Tp): # ... name = node.name frame = node.statement(future=_True).scope() in_annotation_or_default_or_decorator = self._defined_in_function_definition( node, frame ) in_ancestor_list = utils.is_ancestor_name(frame, node) if in_annotation_or_default_or_decorator or in_ancestor_list: frame_locals = frame.parent.scope().locals else: frame_locals = frame.locals return not ( (isinstance(frame, nodes.ClassDef) or in_annotation_or_default_or_decorator) and not self._in_lambda_or_comprehension_body(node, frame) and name in frame_locals ) def _loopvar_name(self, node: astroid.Name) -> None: # filter variables according to node's scope astmts = [s for s in node.lookup(node.name)[1] if hasattr(s, "assign_type")] # If this variable usage exists inside a function definition # that exists in the same loop, # the usage is safe because the function will not be defined either if # the variable is not defined. scope = node.scope() if isinstance(scope, nodes.FunctionDef) and any( asmt.scope().parent_of(scope) for asmt in astmts ): return # Filter variables according to their respective scope. Test parent # and statement to avoid #74747. This is not a total fix, which would # introduce a mechanism similar to special attribute lookup in # modules. Also, in order to get correct inference in this case, the # scope lookup rules would need to be changed to return the initial # assignment (which does not exist in code per se) as well as any later # modifications. # pylint: disable-next=too-many-boolean-expressions if ( not astmts or ( astmts[0].parent == astmts[0].root() and astmts[0].parent.parent_of(node) ) or ( astmts[0].is_statement or not isinstance(astmts[0].parent, nodes.Module) and astmts[0].statement(future=_True).parent_of(node) ) ): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement(future=_True).parent_of( stmt ) and not in_for_else_branch(astmts[i].statement(future=_True), stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) != 1: return assign = astmts[0].assign_type() if not ( isinstance(assign, (nodes.For, nodes.Comprehension, nodes.GeneratorExp)) and assign.statement(future=_True) is not node.statement(future=_True) ): return # For functions we can do more by inferring the length of the itered object if not isinstance(assign, nodes.For): self.add_message("undefined-loop-variable", args=node.name, node=node) return try: inferred = next(assign.iter.infer()) except astroid.InferenceError: self.add_message("undefined-loop-variable", args=node.name, node=node) else: if ( isinstance(inferred, astroid.Instance) and inferred.qname() == BUILTIN_RANGE ): # Consider range() objects safe, even if they might not yield any results. return # Consider sequences. sequences = ( nodes.List, nodes.Tuple, nodes.Dict, nodes.Set, astroid.objects.FrozenSet, ) if not isinstance(inferred, sequences): self.add_message("undefined-loop-variable", args=node.name, node=node) return elements = getattr(inferred, "elts", getattr(inferred, "items", [])) if not elements: self.add_message("undefined-loop-variable", args=node.name, node=node) def _check_is_unused(self, name, node, stmt, global_names, nonlocal_names): # Ignore some special names specified by user configuration. if self._is_name_ignored(stmt, name): return # Ignore names that were added dynamically to the Function scope if ( isinstance(node, nodes.FunctionDef) and name == "__class__" and len(node.locals["__class__"]) == 1 and isinstance(node.locals["__class__"][0], nodes.ClassDef) ): return # Ignore names imported by the global statement. if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)): # Detect imports, assigned to global statements. if global_names and _import_name_is_global(stmt, global_names): return argnames = list( itertools.chain(node.argnames(), [arg.name for arg in node.args.kwonlyargs]) ) # Care about functions with unknown argument (builtins) if name in argnames: self._check_unused_arguments(name, node, stmt, argnames) else: if stmt.parent and isinstance( stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple) ): if name in nonlocal_names: return qname = asname = None if isinstance(stmt, (nodes.Import, nodes.ImportFrom)): # Need the complete name, which we don't have in .locals. if len(stmt.names) > 1: import_names = next( (names for names in stmt.names if name in names), None ) else: import_names = stmt.names[0] if import_names: qname, asname = import_names name = asname or qname if _has_locals_call_after_node(stmt, node.scope()): message_name = "possibly-unused-variable" else: if isinstance(stmt, nodes.Import): if asname is not None: msg = f"{qname} imported as {asname}" else: msg = f"import {name}" self.add_message("unused-import", args=msg, node=stmt) return if isinstance(stmt, nodes.ImportFrom): if asname is not None: msg = f"{qname} imported from {stmt.modname} as {asname}" else: msg = f"{name} imported from {stmt.modname}" self.add_message("unused-import", args=msg, node=stmt) return message_name = "unused-variable" if isinstance(stmt, nodes.FunctionDef) and stmt.decorators: return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Special case for exception variable if isinstance(stmt.parent, nodes.ExceptHandler) and any( n.name == name for n in stmt.parent.nodes_of_class(nodes.Name) ): return self.add_message(message_name, args=name, node=stmt) def _is_name_ignored(self, stmt, name): authorized_rgx = self.config.dummy_variables_rgx if ( isinstance(stmt, nodes.AssignName) and isinstance(stmt.parent, nodes.Arguments) or isinstance(stmt, nodes.Arguments) ): regex = self.config.ignored_argument_names else: regex = authorized_rgx return regex and regex.match(name) def _check_unused_arguments(self, name, node, stmt, argnames): is_method = node.is_method() klass = node.parent.frame(future=_True) if is_method and isinstance(klass, nodes.ClassDef): confidence = ( INFERENCE if utils.has_known_bases(klass) else INFERENCE_FAILURE ) else: confidence = HIGH if is_method: # Don't warn for the first argument of a (non static) method if node.type != "staticmethod" and name == argnames[0]: return # Don't warn for argument of an overridden method overridden = overridden_method(klass, node.name) if overridden is not None and name in overridden.argnames(): return if node.name in utils.PYMETHODS and node.name not in ( "__init__", "__new__", ): return # Don't check callback arguments if any( node.name.startswith(cb) or node.name.endswith(cb) for cb in self.config.callbacks ): return # Don't check arguments of singledispatch.register function. if utils.is_registered_in_singledispatch_function(node): return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Don't check protocol classes if utils.is_protocol_class(klass): return self.add_message("unused-argument", args=name, node=stmt, confidence=confidence) def _check_late_binding_closure(self, node: nodes.Name) -> None: """Check whether node is a cell var that is assigned within a containing loop. Special cases where we don't care about the error: 1. When the node's function is immediately called, e.g. (lambda: i)() 2. When the node's function is returned from within the loop, e.g. return lambda: i """ if not self.linter.is_message_enabled("cell-var-from-loop"): return node_scope = node.frame(future=_True) # If node appears in a default argument expression, # look at the next enclosing frame instead if utils.is_default_argument(node, node_scope): node_scope = node_scope.parent.frame(future=_True) # Check if node is a cell var if ( not isinstance(node_scope, (nodes.Lambda, nodes.FunctionDef)) or node.name in node_scope.locals ): return assign_scope, stmts = node.lookup(node.name) if not stmts or not assign_scope.parent_of(node_scope): return if utils.is_comprehension(assign_scope): self.add_message("cell-var-from-loop", node=node, args=node.name) else: # Look for an enclosing For loop. # Currently, we only consider the first assignment assignment_node = stmts[0] maybe_for = assignment_node while maybe_for and not isinstance(maybe_for, nodes.For): if maybe_for is assign_scope: break maybe_for = maybe_for.parent else: if ( maybe_for and maybe_for.parent_of(node_scope) and not utils.is_being_called(node_scope) and node_scope.parent and not isinstance(node_scope.statement(future=_True), nodes.Return) ): self.add_message("cell-var-from-loop", node=node, args=node.name) def _should_ignore_redefined_builtin(self, stmt): if not isinstance(stmt, nodes.ImportFrom): return _False return stmt.modname in self.config.redefining_builtins_modules def _allowed_redefined_builtin(self, name): return name in self.config.allowed_redefined_builtins def _has_homonym_in_upper_function_scope( self, node: nodes.Name, index: int ) -> bool: """Return whether there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function :param node: node to check for :param index: index of the current consumer inside self._to_consume :return: _True if there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function, _False otherwise """ return any( _consumer.scope_type == "function" and node.name in _consumer.to_consume for _consumer in self._to_consume[index - 1 :: -1] ) def _store_type_annotation_node(self, type_annotation): """Given a type annotation, store all the name nodes it refers to.""" if isinstance(type_annotation, nodes.Name): self._type_annotation_names.append(type_annotation.name) return if isinstance(type_annotation, nodes.Attribute): self._store_type_annotation_node(type_annotation.expr) return if not isinstance(type_annotation, nodes.Subscript): return if ( isinstance(type_annotation.value, nodes.Attribute) and isinstance(type_annotation.value.expr, nodes.Name) and type_annotation.value.expr.name == TYPING_MODULE ): self._type_annotation_names.append(TYPING_MODULE) return self._type_annotation_names.extend( annotation.name for annotation in type_annotation.nodes_of_class(nodes.Name) ) def _store_type_annotation_names(self, node): type_annotation = node.type_annotation if not type_annotation: return self._store_type_annotation_node(node.type_annotation) def _check_self_cls_assign(self, node: nodes.Assign) -> None: """Check that self/cls don't get assigned.""" assign_names: Set[Optional[str]] = set() for target in node.targets: if isinstance(target, nodes.AssignName): assign_names.add(target.name) elif isinstance(target, nodes.Tuple): assign_names.update( elt.name for elt in target.elts if isinstance(elt, nodes.AssignName) ) scope = node.scope() nonlocals_with_same_name = any( child for child in scope.body if isinstance(child, nodes.Nonlocal) ) if nonlocals_with_same_name: scope = node.scope().parent.scope() if not ( isinstance(scope, nodes.FunctionDef) and scope.is_method() and "builtins.staticmethod" not in scope.decoratornames() ): return argument_names = scope.argnames() if not argument_names: return self_cls_name = argument_names[0] if self_cls_name in assign_names: self.add_message("self-cls-assignment", node=node, args=(self_cls_name,)) def _check_unpacking(self, inferred, node, targets): """Check for unbalanced tuple unpacking and unpacking non sequences. """ if utils.is_inside_abstract_class(node): return if utils.is_comprehension(node): return if inferred is astroid.Uninferable: return if ( isinstance(inferred.parent, nodes.Arguments) and isinstance(node.value, nodes.Name) and node.value.name == inferred.parent.vararg ): # Variable-length argument, we can't determine the length. return # Attempt to check unpacking is properly balanced values = self._nodes_to_unpack(inferred) if values is not None: if len(targets) != len(values): # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return self.add_message( "unbalanced-tuple-unpacking", node=node, args=( _get_unpacking_extra_info(node, inferred), len(targets), len(values), ), ) # attempt to check unpacking may be possible (ie RHS is iterable) elif not utils.is_iterable(inferred): self.add_message( "unpacking-non-sequence", node=node, args=(_get_unpacking_extra_info(node, inferred),), ) @staticmethod def _nodes_to_unpack(node: nodes.NodeNG) -> Optional[List[nodes.NodeNG]]: """Return the list of values of the `Assign` node.""" if isinstance(node, (nodes.Tuple, nodes.List)): return node.itered() if isinstance(node, astroid.Instance) and any( ancestor.qname() == "typing.NamedTuple" for ancestor in node.ancestors() ): return [i for i in node.values() if isinstance(i, nodes.AssignName)] return None def _check_module_attrs(self, node, module, module_names): """Check that module_names (list of string) are accessible through the given module if the latest access name corresponds to a module, return it """ while module_names: name = module_names.pop(0) if name == "__dict__": module = None break try: module = next(module.getattr(name)[0].infer()) if module is astroid.Uninferable: return None except astroid.NotFoundError: if module.name in self._ignored_modules: return None self.add_message( "no-name-in-module", args=(name, module.name), node=node ) return None except astroid.InferenceError: return None if module_names: modname = module.name if module else "__dict__" self.add_message( "no-name-in-module", node=node, args=(".".join(module_names), modname) ) return None if isinstance(module, nodes.Module): return module return None def _check_all(self, node: nodes.Module, not_consumed): assigned = next(node.igetattr("__all__")) if assigned is astroid.Uninferable: return if not assigned.pytype() in {"builtins.list", "builtins.tuple"}: line, col = assigned.tolineno, assigned.col_offset self.add_message("invalid-all-format", line=line, col_offset=col, node=node) return for elt in getattr(assigned, "elts", ()): try: elt_name = next(elt.infer()) except astroid.InferenceError: continue if elt_name is astroid.Uninferable: continue if not elt_name.parent: continue if not isinstance(elt_name, nodes.Const) or not isinstance( elt_name.value, str ): self.add_message("invalid-all-object", args=elt.as_string(), node=elt) continue elt_name = elt_name.value # If elt is in not_consumed, remove it from not_consumed if elt_name in not_consumed: del not_consumed[elt_name] continue if elt_name not in node.locals: if not node.package: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) else: basename = os.path.splitext(node.file)[0] if os.path.basename(basename) == "__init__": name = node.name + "." + elt_name try: astroid.modutils.file_from_modpath(name.split(".")) except ImportError: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) except SyntaxError: # don't yield a syntax-error warning, # because it will be later yielded # when the file will be checked pass def _check_globals(self, not_consumed): if self._allow_global_unused_variables: return for name, node_lst in not_consumed.items(): for node in node_lst: self.add_message("unused-variable", args=(name,), node=node) def _check_imports(self, not_consumed): local_names = _fix_dot_imports(not_consumed) checked = set() unused_wildcard_imports: DefaultDict[ Tuple[str, nodes.ImportFrom], List[str] ] = collections.defaultdict(list) for name, stmt in local_names: for imports in stmt.names: real_name = imported_name = imports[0] if imported_name == "*": real_name = name as_name = imports[1] if real_name in checked: continue if name not in (real_name, as_name): continue checked.add(real_name) is_type_annotation_import = ( imported_name in self._type_annotation_names or as_name in self._type_annotation_names ) if isinstance(stmt, nodes.Import) or ( isinstance(stmt, nodes.ImportFrom) and not stmt.modname ): if isinstance(stmt, nodes.ImportFrom) and SPECIAL_OBJ.search( imported_name ): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if as_name == "_": continue if as_name is None: msg = f"import {imported_name}" else: msg = f"{imported_name} imported as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) elif isinstance(stmt, nodes.ImportFrom) and stmt.modname != FUTURE: if SPECIAL_OBJ.search(imported_name): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if _is_from_future_import(stmt, name): # Check if the name is in fact loaded from a # __future__ import in another module. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if imported_name == "*": unused_wildcard_imports[(stmt.modname, stmt)].append(name) else: if as_name is None: msg = f"{imported_name} imported from {stmt.modname}" else: msg = f"{imported_name} imported from {stmt.modname} as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) # Construct string for unused-wildcard-import message for module, unused_list in unused_wildcard_imports.items(): if len(unused_list) == 1: arg_string = unused_list[0] else: arg_string = ( f"{', '.join(i for i in unused_list[:-1])} and {unused_list[-1]}" ) self.add_message( "unused-wildcard-import", args=(arg_string, module[0]), node=module[1] ) del self._to_consume def _check_metaclasses(self, node): """Update consumption analysis for metaclasses.""" consumed = [] # [(scope_locals, consumed_key)] for child_node in node.get_children(): if isinstance(child_node, nodes.ClassDef): consumed.extend(self._check_classdef_metaclasses(child_node, node)) # Pop the consumed items, in order to avoid having # unused-import and unused-variable false positives for scope_locals, name in consumed: scope_locals.pop(name, None) def _check_classdef_metaclasses(self, klass, parent_node): if not klass._metaclass: # Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors return [] consumed = [] # [(scope_locals, consumed_key)] metaclass = klass.metaclass() name = None if isinstance(klass._metaclass, nodes.Name): name = klass._metaclass.name elif isinstance(klass._metaclass, nodes.Attribute) and klass._metaclass.expr: attr = klass._metaclass.expr while not isinstance(attr, nodes.Name): attr = attr.expr name = attr.name elif metaclass: name = metaclass.root().name found = _False name = METACLASS_NAME_TRANSFORMS.get(name, name) if name: # check enclosing scopes starting from most local for scope_locals, _, _, _ in self._to_consume[::-1]: found_nodes = scope_locals.get(name, []) for found_node in found_nodes: if found_node.lineno <= klass.lineno: consumed.append((scope_locals, name)) found = _True break # Check parent scope nodes_in_parent_scope = parent_node.locals.get(name, []) for found_node_parent in nodes_in_parent_scope: if found_node_parent.lineno <= klass.lineno: found = _True break if ( not found and not metaclass and not ( name in nodes.Module.scope_attrs or utils.is_builtin(name) or name in self.config.additional_builtins ) ): self.add_message("undefined-variable", node=klass, args=(name,)) return consumed
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,219
visit_setcomp
def
function
def visit_setcomp(self, node: nodes.SetComp) -> None: """Visit setcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_setcomp(self, _: nodes.SetComp) -> None: """Leave setcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Visit function: update consumption analysis variable and check locals.""" self._to_consume.append(NamesConsumer(node, "function")) if not ( self.linter.is_message_enabled("redefined-outer-name") or self.linter.is_message_enabled("redefined-builtin") ): return globs = node.root().globals for name, stmt in node.items(): if name in globs and not isinstance(stmt, nodes.Global): definition = globs[name][0] if ( isinstance(definition, nodes.ImportFrom) and definition.modname == FUTURE ): # It is a __future__ directive, not a symbol. continue # Do not take in account redefined names for the purpose # of type checking.: if any( isinstance(definition.parent, nodes.If) and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS for definition in globs[name] ): continue line = definition.fromlineno if not self._is_name_ignored(stmt, name): self.add_message( "redefined-outer-name", args=(name, line), node=stmt ) elif ( utils.is_builtin(name) and not self._allowed_redefined_builtin(name) and not self._should_ignore_redefined_builtin(stmt) ): # do not print Redefining builtin for additional builtins self.add_message("redefined-builtin", args=name, node=stmt) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Leave function: check function's locals are consumed.""" self._check_metaclasses(node) if node.type_comment_returns: self._store_type_annotation_node(node.type_comment_returns) if node.type_comment_args: for argument_annotation in node.type_comment_args: self._store_type_annotation_node(argument_annotation) not_consumed = self._to_consume.pop().to_consume if not ( self.linter.is_message_enabled("unused-variable") or self.linter.is_message_enabled("possibly-unused-variable") or self.linter.is_message_enabled("unused-argument") ): return # Don't check arguments of function which are only raising an exception. if utils.is_error(node): return # Don't check arguments of abstract methods or within an interface. is_method = node.is_method() if is_method and node.is_abstract(): return global_names = _flattened_scope_names(node.nodes_of_class(nodes.Global)) nonlocal_names = _flattened_scope_names(node.nodes_of_class(nodes.Nonlocal)) for name, stmts in not_consumed.items(): self._check_is_unused(name, node, stmts[0], global_names, nonlocal_names) visit_asyncfunctiondef = visit_functiondef leave_asyncfunctiondef = leave_functiondef @utils.check_messages( "global-variable-undefined", "global-variable-not-assigned", "global-statement", "global-at-module-level", "redefined-builtin", ) def visit_global(self, node: nodes.Global) -> None: """Check names imported exists in the global scope.""" frame = node.frame(future=_True) if isinstance(frame, nodes.Module): self.add_message("global-at-module-level", node=node) return module = frame.root() default_message = _True locals_ = node.scope().locals for name in node.names: try: assign_nodes = module.getattr(name) except astroid.NotFoundError: # unassigned global, skip assign_nodes = [] not_defined_locally_by_import = not any( isinstance(local, nodes.Import) for local in locals_.get(name, ()) ) if ( not utils.is_reassigned_after_current(node, name) and not utils.is_deleted_after_current(node, name) and not_defined_locally_by_import ): self.add_message("global-variable-not-assigned", args=name, node=node) default_message = _False continue for anode in assign_nodes: if ( isinstance(anode, nodes.AssignName) and anode.name in module.special_attributes ): self.add_message("redefined-builtin", args=name, node=node) break if anode.frame(future=_True) is module: # module level assignment break if ( isinstance(anode, (nodes.ClassDef, nodes.FunctionDef)) and anode.parent is module ): # module level function assignment break else: if not_defined_locally_by_import: # global undefined at the module scope self.add_message("global-variable-undefined", args=name, node=node) default_message = _False if default_message: self.add_message("global-statement", node=node) def visit_assignname(self, node: nodes.AssignName) -> None: if isinstance(node.assign_type(), nodes.AugAssign): self.visit_name(node) def visit_delname(self, node: nodes.DelName) -> None: self.visit_name(node) def visit_name(self, node: nodes.Name) -> None: """Don't add the 'utils.check_messages' decorator here! It's important that all 'Name' nodes are visited, otherwise the 'NamesConsumers' won't be correct. """ stmt = node.statement(future=_True) if stmt.fromlineno is None: # name node from an astroid built from live code, skip assert not stmt.root().file.endswith(".py") return self._undefined_and_used_before_checker(node, stmt) if self._is_undefined_loop_variable_enabled: self._loopvar_name(node) @utils.check_messages("redefined-outer-name") def visit_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return for outer_except, outer_except_assign_name in self._except_handler_names_queue: if node.name.name == outer_except_assign_name.name: self.add_message( "redefined-outer-name", args=(outer_except_assign_name.name, outer_except.fromlineno), node=node, ) break self._except_handler_names_queue.append((node, node.name)) @utils.check_messages("redefined-outer-name") def leave_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return self._except_handler_names_queue.pop() def _undefined_and_used_before_checker( self, node: nodes.Name, stmt: nodes.NodeNG ) -> None: frame = stmt.scope() start_index = len(self._to_consume) - 1 # iterates through parent scopes, from the inner to the outer base_scope_type = self._to_consume[start_index].scope_type for i in range(start_index, -1, -1): current_consumer = self._to_consume[i] # Certain nodes shouldn't be checked as they get checked another time if self._should_node_be_skipped(node, current_consumer, i == start_index): continue action, nodes_to_consume = self._check_consumer( node, stmt, frame, current_consumer, i, base_scope_type ) if nodes_to_consume: # Any nodes added to consumed_uncertain by get_next_to_consume() # should be added back so that they are marked as used. # They will have already had a chance to emit used-before-assignment. # We check here instead of before every single return in _check_consumer() nodes_to_consume += current_consumer.consumed_uncertain[node.name] current_consumer.mark_as_consumed(node.name, nodes_to_consume) if action is VariableVisitConsumerAction.CONTINUE: continue if action is VariableVisitConsumerAction.RETURN: return # we have not found the name, if it isn't a builtin, that's an # undefined name ! if ( self._is_undefined_variable_enabled and not ( node.name in nodes.Module.scope_attrs or utils.is_builtin(node.name) or node.name in self.config.additional_builtins or ( node.name == "__class__" and isinstance(frame, nodes.FunctionDef) and frame.is_method() ) ) and not utils.node_ignores_exception(node, NameError) ): self.add_message("undefined-variable", args=node.name, node=node) def _should_node_be_skipped( self, node: nodes.Name, consumer: NamesConsumer, is_start_index: bool ) -> bool: """Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks. """ if consumer.scope_type == "class": # The list of base classes in the class definition is not part # of the class body. # If the current scope is a class scope but it's not the inner # scope, ignore it. This prevents to access this scope instead of # the globals one in function members when there are some common # names. if utils.is_ancestor_name(consumer.node, node) or ( not is_start_index and self._ignore_class_scope(node) ): return _True # Ignore inner class scope for keywords in class definition if isinstance(node.parent, nodes.Keyword) and isinstance( node.parent.parent, nodes.ClassDef ): return _True elif consumer.scope_type == "function" and self._defined_in_function_definition( node, consumer.node ): # If the name node is used as a function default argument's value or as # a decorator, then start from the parent frame of the function instead # of the function frame - and thus open an inner class scope return _True elif consumer.scope_type == "lambda" and utils.is_default_argument( node, consumer.node ): return _True return _False # pylint: disable=too-many-return-statements def _check_consumer( self, node: nodes.Name, stmt: nodes.NodeNG, frame: nodes.LocalsDictNodeNG, current_consumer: NamesConsumer, consumer_level: int, base_scope_type: Any, ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Checks a consumer for conditions that should trigger messages.""" # If the name has already been consumed, only check it's not a loop # variable used outside the loop. # Avoid the case where there are homonyms inside function scope and # comprehension current scope (avoid bug #1731) if node.name in current_consumer.consumed: if utils.is_func_decorator(current_consumer.node) or not ( current_consumer.scope_type == "comprehension" and self._has_homonym_in_upper_function_scope(node, consumer_level) # But don't catch homonyms against the filter of a comprehension, # (like "if x" in "[x for x in expr() if x]") # https://github.com/PyCQA/pylint/issues/5586 and not ( ( isinstance(node.parent.parent, nodes.Comprehension) and node.parent in node.parent.parent.ifs ) # Or homonyms against values to keyword arguments # (like "var" in "[func(arg=var) for var in expr()]") or ( isinstance(node.scope(), nodes.ComprehensionScope) and isinstance(node.parent, (nodes.Call, nodes.Keyword)) ) ) ): self._check_late_binding_closure(node) self._loopvar_name(node) return (VariableVisitConsumerAction.RETURN, None) found_nodes = current_consumer.get_next_to_consume(node) if found_nodes is None: return (VariableVisitConsumerAction.CONTINUE, None) if not found_nodes: if node.name in current_consumer.consumed_uncertain: confidence = CONTROL_FLOW else: confidence = HIGH self.add_message( "used-before-assignment", args=node.name, node=node, confidence=confidence, ) # Mark for consumption any nodes added to consumed_uncertain by # get_next_to_consume() because they might not have executed. return ( VariableVisitConsumerAction.RETURN, current_consumer.consumed_uncertain[node.name], ) self._check_late_binding_closure(node) if not ( self._is_undefined_variable_enabled or self._is_used_before_assignment_enabled ): return (VariableVisitConsumerAction.RETURN, found_nodes) defnode = utils.assign_parent(found_nodes[0]) defstmt = defnode.statement(future=_True) defframe = defstmt.frame(future=_True) # The class reuses itself in the class scope. is_recursive_klass = ( frame is defframe and defframe.parent_of(node) and isinstance(defframe, nodes.ClassDef) and node.name == defframe.name ) if ( is_recursive_klass and utils.get_node_first_ancestor_of_type(node, nodes.Lambda) and ( not utils.is_default_argument(node) or node.scope().parent.scope() is not defframe ) ): # Self-referential class references are fine in lambda's -- # As long as they are not part of the default argument directly # under the scope of the parent self-referring class. # Example of valid default argument: # class MyName3: # myattr = 1 # mylambda3 = lambda: lambda a=MyName3: a # Example of invalid default argument: # class MyName4: # myattr = 1 # mylambda4 = lambda a=MyName4: lambda: a # If the above conditional is _True, # there is no possibility of undefined-variable # Also do not consume class name # (since consuming blocks subsequent checks) # -- quit return (VariableVisitConsumerAction.RETURN, None) ( maybe_before_assign, annotation_return, use_outer_definition, ) = self._is_variable_violation( node, defnode, stmt, defstmt, frame, defframe, base_scope_type, is_recursive_klass, ) if use_outer_definition: return (VariableVisitConsumerAction.CONTINUE, None) if ( maybe_before_assign and not utils.is_defined_before(node) and not astroid.are_exclusive(stmt, defstmt, ("NameError",)) ): # Used and defined in the same place, e.g `x += 1` and `del x` defined_by_stmt = defstmt is stmt and isinstance( node, (nodes.DelName, nodes.AssignName) ) if ( is_recursive_klass or defined_by_stmt or annotation_return or isinstance(defstmt, nodes.Delete) ): if not utils.node_ignores_exception(node, NameError): # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance( stmt, ( nodes.AnnAssign, nodes.FunctionDef, nodes.Arguments, ), ) and node.name in node.root().locals ): if defined_by_stmt: return (VariableVisitConsumerAction.CONTINUE, [node]) return (VariableVisitConsumerAction.CONTINUE, None) elif base_scope_type != "lambda": # E0601 may *not* occurs in lambda scope. # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif base_scope_type == "lambda": # E0601 can occur in class-level scope in lambdas, as in # the following example: # class A: # x = lambda attr: f + attr # f = 42 # We check lineno because doing the following is fine: # class A: # x = 42 # y = lambda attr: x + attr if ( isinstance(frame, nodes.ClassDef) and node.name in frame.locals and stmt.fromlineno <= defstmt.fromlineno ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) elif self._is_only_type_assignment(node, defstmt): if node.scope().locals.get(node.name): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH ) else: self.add_message( "undefined-variable", args=node.name, node=node, confidence=HIGH ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif isinstance(defstmt, nodes.ClassDef): return self._is_first_level_self_reference(node, defstmt, found_nodes) elif isinstance(defnode, nodes.NamedExpr): if isinstance(defnode.parent, nodes.IfExp): if self._is_never_evaluated(defnode, defnode.parent): self.add_message( "undefined-variable", args=node.name, node=node, confidence=INFERENCE, ) return (VariableVisitConsumerAction.RETURN, found_nodes) return (VariableVisitConsumerAction.RETURN, found_nodes) @utils.check_messages("no-name-in-module") def visit_import(self, node: nodes.Import) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return for name, _ in node.names: parts = name.split(".") try: module = next(_infer_name_module(node, parts[0])) except astroid.ResolveError: continue if not isinstance(module, nodes.Module): continue self._check_module_attrs(node, module, parts[1:]) @utils.check_messages("no-name-in-module") def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return name_parts = node.modname.split(".") try: module = node.do_import_module(name_parts[0]) except astroid.AstroidBuildingException: return module = self._check_module_attrs(node, module, name_parts[1:]) if not module: return for name, _ in node.names: if name == "*": continue self._check_module_attrs(node, module, name.split(".")) @utils.check_messages( "unbalanced-tuple-unpacking", "unpacking-non-sequence", "self-cls-assignment" ) def visit_assign(self, node: nodes.Assign) -> None: """Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned. """ self._check_self_cls_assign(node) if not isinstance(node.targets[0], (nodes.Tuple, nodes.List)): return targets = node.targets[0].itered() try: inferred = utils.safe_infer(node.value) if inferred is not None: self._check_unpacking(inferred, node, targets) except astroid.InferenceError: return # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_listcomp(self, _: nodes.ListComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def leave_assign(self, node: nodes.Assign) -> None: self._store_type_annotation_names(node) def leave_with(self, node: nodes.With) -> None: self._store_type_annotation_names(node) def visit_arguments(self, node: nodes.Arguments) -> None: for annotation in node.type_comment_args: self._store_type_annotation_node(annotation) # Relying on other checker's options, which might not have been initialized yet. @astroid.decorators.cachedproperty def _analyse_fallback_blocks(self): return get_global_option(self, "analyse-fallback-blocks", default=_False) @astroid.decorators.cachedproperty def _ignored_modules(self): return get_global_option(self, "ignored-modules", default=[]) @astroid.decorators.cachedproperty def _allow_global_unused_variables(self): return get_global_option(self, "allow-global-unused-variables", default=_True) @staticmethod def _defined_in_function_definition(node, frame): in_annotation_or_default_or_decorator = _False if ( isinstance(frame, nodes.FunctionDef) and node.statement(future=_True) is frame ): in_annotation_or_default_or_decorator = ( ( node in frame.args.annotations or node in frame.args.posonlyargs_annotations or node in frame.args.kwonlyargs_annotations or node is frame.args.varargannotation or node is frame.args.kwargannotation ) or frame.args.parent_of(node) or (frame.decorators and frame.decorators.parent_of(node)) or ( frame.returns and (node is frame.returns or frame.returns.parent_of(node)) ) ) return in_annotation_or_default_or_decorator @staticmethod def _in_lambda_or_comprehension_body( node: nodes.NodeNG, frame: nodes.NodeNG ) -> bool: """Return _True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.""" child = node parent = node.parent while parent is not None: if parent is frame: return _False if isinstance(parent, nodes.Lambda) and child is not parent.args: # Body of lambda should not have access to class attributes. return _True if isinstance(parent, nodes.Comprehension) and child is not parent.iter: # Only iter of list/set/dict/generator comprehension should have access. return _True if isinstance(parent, nodes.ComprehensionScope) and not ( parent.generators and child is parent.generators[0] ): # Body of list/set/dict/generator comprehension should not have access to class attributes. # Furthermore, only the first generator (if multiple) in comprehension should have access. return _True child = parent parent = parent.parent return _False @staticmethod def _is_variable_violation( node: nodes.Name, defnode, stmt: nodes.Statement, defstmt: nodes.Statement, frame, # scope of statement of node defframe, base_scope_type, is_recursive_klass, ) -> Tuple[bool, bool, bool]: # pylint: disable=too-many-nested-blocks maybe_before_assign = _True annotation_return = _False use_outer_definition = _False if frame is not defframe: maybe_before_assign = _detect_global_scope(node, frame, defframe) elif defframe.parent is None: # we are at the module level, check the name is not # defined in builtins if ( node.name in defframe.scope_attrs or astroid.builtin_lookup(node.name)[1] ): maybe_before_assign = _False else: # we are in a local scope, check the name is not # defined in global or builtin scope # skip this lookup if name is assigned later in function scope/lambda # Note: the node.frame() is not the same as the `frame` argument which is # equivalent to frame.statement().scope() forbid_lookup = ( isinstance(frame, nodes.FunctionDef) or isinstance(node.frame(future=_True), nodes.Lambda) ) and _assigned_locally(node) if not forbid_lookup and defframe.root().lookup(node.name)[1]: maybe_before_assign = _False use_outer_definition = stmt == defstmt and not isinstance( defnode, nodes.Comprehension ) # check if we have a nonlocal elif node.name in defframe.locals: maybe_before_assign = not any( isinstance(child, nodes.Nonlocal) and node.name in child.names for child in defframe.get_children() ) if ( base_scope_type == "lambda" and isinstance(frame, nodes.ClassDef) and node.name in frame.locals ): # This rule verifies that if the definition node of the # checked name is an Arguments node and if the name # is used a default value in the arguments defaults # and the actual definition of the variable label # is happening before the Arguments definition. # # bar = None # foo = lambda bar=bar: bar # # In this case, maybe_before_assign should be _False, otherwise # it should be _True. maybe_before_assign = not ( isinstance(defnode, nodes.Arguments) and node in defnode.defaults and frame.locals[node.name][0].fromlineno < defstmt.fromlineno ) elif isinstance(defframe, nodes.ClassDef) and isinstance( frame, nodes.FunctionDef ): # Special rule for function return annotations, # using a name defined earlier in the class containing the function. if node is frame.returns and defframe.parent_of(frame.returns): annotation_return = _True if ( frame.returns.name in defframe.locals and defframe.locals[node.name][0].lineno < frame.lineno ): # Detect class assignments with a name defined earlier in the # class. In this case, no warning should be raised. maybe_before_assign = _False else: maybe_before_assign = _True if isinstance(node.parent, nodes.Arguments): maybe_before_assign = stmt.fromlineno <= defstmt.fromlineno elif is_recursive_klass: maybe_before_assign = _True else: maybe_before_assign = ( maybe_before_assign and stmt.fromlineno <= defstmt.fromlineno ) if maybe_before_assign and stmt.fromlineno == defstmt.fromlineno: if ( isinstance(defframe, nodes.FunctionDef) and frame is defframe and defframe.parent_of(node) and stmt is not defstmt ): # Single statement function, with the statement on the # same line as the function definition maybe_before_assign = _False elif ( isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Expr, nodes.Return, ), ) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) and frame is defframe and defframe.parent_of(node) and stmt is defstmt ): # Single statement if, with assignment expression on same # line as assignment # x = b if (b := _True) else _False maybe_before_assign = _False elif ( isinstance( # pylint: disable=too-many-boolean-expressions defnode, nodes.NamedExpr ) and frame is defframe and defframe.parent_of(stmt) and stmt is defstmt and ( ( defnode.lineno == node.lineno and defnode.col_offset < node.col_offset ) or (defnode.lineno < node.lineno) or ( # Issue in the `ast` module until py39 # Nodes in a multiline string have the same lineno # Could be false-positive without check not PY39_PLUS and defnode.lineno == node.lineno and isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Return, ), ) and isinstance(defstmt.value, nodes.JoinedStr) ) ) ): # Expressions, with assignment expressions # Use only after assignment # b = (c := 2) and c maybe_before_assign = _False # Look for type checking definitions inside a type checking guard. if isinstance(defstmt, (nodes.Import, nodes.ImportFrom)): defstmt_parent = defstmt.parent if ( isinstance(defstmt_parent, nodes.If) and defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS ): # Exempt those definitions that are used inside the type checking # guard or that are defined in both type checking guard branches. used_in_branch = defstmt_parent.parent_of(node) defined_in_or_else = _False for definition in defstmt_parent.orelse: if isinstance(definition, nodes.Assign): defined_in_or_else = any( target.name == node.name for target in definition.targets if isinstance(target, nodes.AssignName) ) if defined_in_or_else: break if not used_in_branch and not defined_in_or_else: maybe_before_assign = _True return maybe_before_assign, annotation_return, use_outer_definition @staticmethod def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: """Check if `defstmt` has the potential to use and assign a name in the same statement. """ if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: # The assignment must happen as part of the first element # e.g. "assert (x:= _True), x" # NOT "assert x, (x:= _True)" value = defstmt.value.elts[0] else: value = defstmt.value if isinstance(value, nodes.IfExp): return _True if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp): return _True return isinstance(value, nodes.Call) and ( any(isinstance(kwarg.value, nodes.IfExp) for kwarg in value.keywords) or any(isinstance(arg, nodes.IfExp) for arg in value.args) ) @staticmethod def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool: """Check if variable only gets assigned a type and never a value.""" if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value: return _False defstmt_frame = defstmt.frame(future=_True) node_frame = node.frame(future=_True) parent = node while parent is not defstmt_frame.parent: parent_scope = parent.scope() local_refs = parent_scope.locals.get(node.name, []) for ref_node in local_refs: # If local ref is in the same frame as our node, but on a later lineno # we don't actually care about this local ref. # Local refs are ordered, so we break. # print(var) # var = 1 # <- irrelevant if defstmt_frame == node_frame and ref_node.lineno > node.lineno: break # If the parent of the local reference is anything but an AnnAssign # Or if the AnnAssign adds a value the variable will now have a value # var = 1 # OR # var: int = 1 if ( not isinstance(ref_node.parent, nodes.AnnAssign) or ref_node.parent.value ): return _False parent = parent_scope.parent return _True @staticmethod def _is_first_level_self_reference( node: nodes.Name, defstmt: nodes.ClassDef, found_nodes: List[nodes.NodeNG] ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Check if a first level method's annotation or default values refers to its own class, and return a consumer action """ if node.frame(future=_True).parent == defstmt and node.statement( future=_True ) == node.frame(future=_True): # Check if used as type annotation # Break if postponed evaluation is enabled if utils.is_node_in_type_annotation_context(node): if not utils.is_postponed_evaluation_enabled(node): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, None) # Check if used as default value by calling the class if isinstance(node.parent, nodes.Call) and isinstance( node.parent.parent, nodes.Arguments ): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, found_nodes) @staticmethod def _is_never_evaluated( defnode: nodes.NamedExpr, defnode_parent: nodes.IfExp ) -> bool: """Check if a NamedExpr is inside a side of if ... else that never gets evaluated """ inferred_test = utils.safe_infer(defnode_parent.test) if isinstance(inferred_test, nodes.Const): if inferred_test.value is _True and defnode == defnode_parent.orelse: return _True if inferred_test.value is _False and defnode == defnode_parent.body: return _True return _False def _ignore_class_scope(self, node): """Return _True if the node is in a local class scope, as an assignment. :param node: Node considered :type node: astroid.Node :return: _True if the node is in a local class scope, as an assignment. _False otherwise. :rtype: bool """ # Detect if we are in a local class scope, as an assignment. # For example, the following is fair game. # # class A: # b = 1 # c = lambda b=b: b * b # # class B: # tp = 1 # def func(self, arg: tp): # ... # class C: # tp = 2 # def func(self, arg=tp): # ... # class C: # class Tp: # pass # class D(Tp): # ... name = node.name frame = node.statement(future=_True).scope() in_annotation_or_default_or_decorator = self._defined_in_function_definition( node, frame ) in_ancestor_list = utils.is_ancestor_name(frame, node) if in_annotation_or_default_or_decorator or in_ancestor_list: frame_locals = frame.parent.scope().locals else: frame_locals = frame.locals return not ( (isinstance(frame, nodes.ClassDef) or in_annotation_or_default_or_decorator) and not self._in_lambda_or_comprehension_body(node, frame) and name in frame_locals ) def _loopvar_name(self, node: astroid.Name) -> None: # filter variables according to node's scope astmts = [s for s in node.lookup(node.name)[1] if hasattr(s, "assign_type")] # If this variable usage exists inside a function definition # that exists in the same loop, # the usage is safe because the function will not be defined either if # the variable is not defined. scope = node.scope() if isinstance(scope, nodes.FunctionDef) and any( asmt.scope().parent_of(scope) for asmt in astmts ): return # Filter variables according to their respective scope. Test parent # and statement to avoid #74747. This is not a total fix, which would # introduce a mechanism similar to special attribute lookup in # modules. Also, in order to get correct inference in this case, the # scope lookup rules would need to be changed to return the initial # assignment (which does not exist in code per se) as well as any later # modifications. # pylint: disable-next=too-many-boolean-expressions if ( not astmts or ( astmts[0].parent == astmts[0].root() and astmts[0].parent.parent_of(node) ) or ( astmts[0].is_statement or not isinstance(astmts[0].parent, nodes.Module) and astmts[0].statement(future=_True).parent_of(node) ) ): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement(future=_True).parent_of( stmt ) and not in_for_else_branch(astmts[i].statement(future=_True), stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) != 1: return assign = astmts[0].assign_type() if not ( isinstance(assign, (nodes.For, nodes.Comprehension, nodes.GeneratorExp)) and assign.statement(future=_True) is not node.statement(future=_True) ): return # For functions we can do more by inferring the length of the itered object if not isinstance(assign, nodes.For): self.add_message("undefined-loop-variable", args=node.name, node=node) return try: inferred = next(assign.iter.infer()) except astroid.InferenceError: self.add_message("undefined-loop-variable", args=node.name, node=node) else: if ( isinstance(inferred, astroid.Instance) and inferred.qname() == BUILTIN_RANGE ): # Consider range() objects safe, even if they might not yield any results. return # Consider sequences. sequences = ( nodes.List, nodes.Tuple, nodes.Dict, nodes.Set, astroid.objects.FrozenSet, ) if not isinstance(inferred, sequences): self.add_message("undefined-loop-variable", args=node.name, node=node) return elements = getattr(inferred, "elts", getattr(inferred, "items", [])) if not elements: self.add_message("undefined-loop-variable", args=node.name, node=node) def _check_is_unused(self, name, node, stmt, global_names, nonlocal_names): # Ignore some special names specified by user configuration. if self._is_name_ignored(stmt, name): return # Ignore names that were added dynamically to the Function scope if ( isinstance(node, nodes.FunctionDef) and name == "__class__" and len(node.locals["__class__"]) == 1 and isinstance(node.locals["__class__"][0], nodes.ClassDef) ): return # Ignore names imported by the global statement. if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)): # Detect imports, assigned to global statements. if global_names and _import_name_is_global(stmt, global_names): return argnames = list( itertools.chain(node.argnames(), [arg.name for arg in node.args.kwonlyargs]) ) # Care about functions with unknown argument (builtins) if name in argnames: self._check_unused_arguments(name, node, stmt, argnames) else: if stmt.parent and isinstance( stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple) ): if name in nonlocal_names: return qname = asname = None if isinstance(stmt, (nodes.Import, nodes.ImportFrom)): # Need the complete name, which we don't have in .locals. if len(stmt.names) > 1: import_names = next( (names for names in stmt.names if name in names), None ) else: import_names = stmt.names[0] if import_names: qname, asname = import_names name = asname or qname if _has_locals_call_after_node(stmt, node.scope()): message_name = "possibly-unused-variable" else: if isinstance(stmt, nodes.Import): if asname is not None: msg = f"{qname} imported as {asname}" else: msg = f"import {name}" self.add_message("unused-import", args=msg, node=stmt) return if isinstance(stmt, nodes.ImportFrom): if asname is not None: msg = f"{qname} imported from {stmt.modname} as {asname}" else: msg = f"{name} imported from {stmt.modname}" self.add_message("unused-import", args=msg, node=stmt) return message_name = "unused-variable" if isinstance(stmt, nodes.FunctionDef) and stmt.decorators: return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Special case for exception variable if isinstance(stmt.parent, nodes.ExceptHandler) and any( n.name == name for n in stmt.parent.nodes_of_class(nodes.Name) ): return self.add_message(message_name, args=name, node=stmt) def _is_name_ignored(self, stmt, name): authorized_rgx = self.config.dummy_variables_rgx if ( isinstance(stmt, nodes.AssignName) and isinstance(stmt.parent, nodes.Arguments) or isinstance(stmt, nodes.Arguments) ): regex = self.config.ignored_argument_names else: regex = authorized_rgx return regex and regex.match(name) def _check_unused_arguments(self, name, node, stmt, argnames): is_method = node.is_method() klass = node.parent.frame(future=_True) if is_method and isinstance(klass, nodes.ClassDef): confidence = ( INFERENCE if utils.has_known_bases(klass) else INFERENCE_FAILURE ) else: confidence = HIGH if is_method: # Don't warn for the first argument of a (non static) method if node.type != "staticmethod" and name == argnames[0]: return # Don't warn for argument of an overridden method overridden = overridden_method(klass, node.name) if overridden is not None and name in overridden.argnames(): return if node.name in utils.PYMETHODS and node.name not in ( "__init__", "__new__", ): return # Don't check callback arguments if any( node.name.startswith(cb) or node.name.endswith(cb) for cb in self.config.callbacks ): return # Don't check arguments of singledispatch.register function. if utils.is_registered_in_singledispatch_function(node): return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Don't check protocol classes if utils.is_protocol_class(klass): return self.add_message("unused-argument", args=name, node=stmt, confidence=confidence) def _check_late_binding_closure(self, node: nodes.Name) -> None: """Check whether node is a cell var that is assigned within a containing loop. Special cases where we don't care about the error: 1. When the node's function is immediately called, e.g. (lambda: i)() 2. When the node's function is returned from within the loop, e.g. return lambda: i """ if not self.linter.is_message_enabled("cell-var-from-loop"): return node_scope = node.frame(future=_True) # If node appears in a default argument expression, # look at the next enclosing frame instead if utils.is_default_argument(node, node_scope): node_scope = node_scope.parent.frame(future=_True) # Check if node is a cell var if ( not isinstance(node_scope, (nodes.Lambda, nodes.FunctionDef)) or node.name in node_scope.locals ): return assign_scope, stmts = node.lookup(node.name) if not stmts or not assign_scope.parent_of(node_scope): return if utils.is_comprehension(assign_scope): self.add_message("cell-var-from-loop", node=node, args=node.name) else: # Look for an enclosing For loop. # Currently, we only consider the first assignment assignment_node = stmts[0] maybe_for = assignment_node while maybe_for and not isinstance(maybe_for, nodes.For): if maybe_for is assign_scope: break maybe_for = maybe_for.parent else: if ( maybe_for and maybe_for.parent_of(node_scope) and not utils.is_being_called(node_scope) and node_scope.parent and not isinstance(node_scope.statement(future=_True), nodes.Return) ): self.add_message("cell-var-from-loop", node=node, args=node.name) def _should_ignore_redefined_builtin(self, stmt): if not isinstance(stmt, nodes.ImportFrom): return _False return stmt.modname in self.config.redefining_builtins_modules def _allowed_redefined_builtin(self, name): return name in self.config.allowed_redefined_builtins def _has_homonym_in_upper_function_scope( self, node: nodes.Name, index: int ) -> bool: """Return whether there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function :param node: node to check for :param index: index of the current consumer inside self._to_consume :return: _True if there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function, _False otherwise """ return any( _consumer.scope_type == "function" and node.name in _consumer.to_consume for _consumer in self._to_consume[index - 1 :: -1] ) def _store_type_annotation_node(self, type_annotation): """Given a type annotation, store all the name nodes it refers to.""" if isinstance(type_annotation, nodes.Name): self._type_annotation_names.append(type_annotation.name) return if isinstance(type_annotation, nodes.Attribute): self._store_type_annotation_node(type_annotation.expr) return if not isinstance(type_annotation, nodes.Subscript): return if ( isinstance(type_annotation.value, nodes.Attribute) and isinstance(type_annotation.value.expr, nodes.Name) and type_annotation.value.expr.name == TYPING_MODULE ): self._type_annotation_names.append(TYPING_MODULE) return self._type_annotation_names.extend( annotation.name for annotation in type_annotation.nodes_of_class(nodes.Name) ) def _store_type_annotation_names(self, node): type_annotation = node.type_annotation if not type_annotation: return self._store_type_annotation_node(node.type_annotation) def _check_self_cls_assign(self, node: nodes.Assign) -> None: """Check that self/cls don't get assigned.""" assign_names: Set[Optional[str]] = set() for target in node.targets: if isinstance(target, nodes.AssignName): assign_names.add(target.name) elif isinstance(target, nodes.Tuple): assign_names.update( elt.name for elt in target.elts if isinstance(elt, nodes.AssignName) ) scope = node.scope() nonlocals_with_same_name = any( child for child in scope.body if isinstance(child, nodes.Nonlocal) ) if nonlocals_with_same_name: scope = node.scope().parent.scope() if not ( isinstance(scope, nodes.FunctionDef) and scope.is_method() and "builtins.staticmethod" not in scope.decoratornames() ): return argument_names = scope.argnames() if not argument_names: return self_cls_name = argument_names[0] if self_cls_name in assign_names: self.add_message("self-cls-assignment", node=node, args=(self_cls_name,)) def _check_unpacking(self, inferred, node, targets): """Check for unbalanced tuple unpacking and unpacking non sequences. """ if utils.is_inside_abstract_class(node): return if utils.is_comprehension(node): return if inferred is astroid.Uninferable: return if ( isinstance(inferred.parent, nodes.Arguments) and isinstance(node.value, nodes.Name) and node.value.name == inferred.parent.vararg ): # Variable-length argument, we can't determine the length. return # Attempt to check unpacking is properly balanced values = self._nodes_to_unpack(inferred) if values is not None: if len(targets) != len(values): # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return self.add_message( "unbalanced-tuple-unpacking", node=node, args=( _get_unpacking_extra_info(node, inferred), len(targets), len(values), ), ) # attempt to check unpacking may be possible (ie RHS is iterable) elif not utils.is_iterable(inferred): self.add_message( "unpacking-non-sequence", node=node, args=(_get_unpacking_extra_info(node, inferred),), ) @staticmethod def _nodes_to_unpack(node: nodes.NodeNG) -> Optional[List[nodes.NodeNG]]: """Return the list of values of the `Assign` node.""" if isinstance(node, (nodes.Tuple, nodes.List)): return node.itered() if isinstance(node, astroid.Instance) and any( ancestor.qname() == "typing.NamedTuple" for ancestor in node.ancestors() ): return [i for i in node.values() if isinstance(i, nodes.AssignName)] return None def _check_module_attrs(self, node, module, module_names): """Check that module_names (list of string) are accessible through the given module if the latest access name corresponds to a module, return it """ while module_names: name = module_names.pop(0) if name == "__dict__": module = None break try: module = next(module.getattr(name)[0].infer()) if module is astroid.Uninferable: return None except astroid.NotFoundError: if module.name in self._ignored_modules: return None self.add_message( "no-name-in-module", args=(name, module.name), node=node ) return None except astroid.InferenceError: return None if module_names: modname = module.name if module else "__dict__" self.add_message( "no-name-in-module", node=node, args=(".".join(module_names), modname) ) return None if isinstance(module, nodes.Module): return module return None def _check_all(self, node: nodes.Module, not_consumed): assigned = next(node.igetattr("__all__")) if assigned is astroid.Uninferable: return if not assigned.pytype() in {"builtins.list", "builtins.tuple"}: line, col = assigned.tolineno, assigned.col_offset self.add_message("invalid-all-format", line=line, col_offset=col, node=node) return for elt in getattr(assigned, "elts", ()): try: elt_name = next(elt.infer()) except astroid.InferenceError: continue if elt_name is astroid.Uninferable: continue if not elt_name.parent: continue if not isinstance(elt_name, nodes.Const) or not isinstance( elt_name.value, str ): self.add_message("invalid-all-object", args=elt.as_string(), node=elt) continue elt_name = elt_name.value # If elt is in not_consumed, remove it from not_consumed if elt_name in not_consumed: del not_consumed[elt_name] continue if elt_name not in node.locals: if not node.package: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) else: basename = os.path.splitext(node.file)[0] if os.path.basename(basename) == "__init__": name = node.name + "." + elt_name try: astroid.modutils.file_from_modpath(name.split(".")) except ImportError: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) except SyntaxError: # don't yield a syntax-error warning, # because it will be later yielded # when the file will be checked pass def _check_globals(self, not_consumed): if self._allow_global_unused_variables: return for name, node_lst in not_consumed.items(): for node in node_lst: self.add_message("unused-variable", args=(name,), node=node) def _check_imports(self, not_consumed): local_names = _fix_dot_imports(not_consumed) checked = set() unused_wildcard_imports: DefaultDict[ Tuple[str, nodes.ImportFrom], List[str] ] = collections.defaultdict(list) for name, stmt in local_names: for imports in stmt.names: real_name = imported_name = imports[0] if imported_name == "*": real_name = name as_name = imports[1] if real_name in checked: continue if name not in (real_name, as_name): continue checked.add(real_name) is_type_annotation_import = ( imported_name in self._type_annotation_names or as_name in self._type_annotation_names ) if isinstance(stmt, nodes.Import) or ( isinstance(stmt, nodes.ImportFrom) and not stmt.modname ): if isinstance(stmt, nodes.ImportFrom) and SPECIAL_OBJ.search( imported_name ): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if as_name == "_": continue if as_name is None: msg = f"import {imported_name}" else: msg = f"{imported_name} imported as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) elif isinstance(stmt, nodes.ImportFrom) and stmt.modname != FUTURE: if SPECIAL_OBJ.search(imported_name): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if _is_from_future_import(stmt, name): # Check if the name is in fact loaded from a # __future__ import in another module. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if imported_name == "*": unused_wildcard_imports[(stmt.modname, stmt)].append(name) else: if as_name is None: msg = f"{imported_name} imported from {stmt.modname}" else: msg = f"{imported_name} imported from {stmt.modname} as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) # Construct string for unused-wildcard-import message for module, unused_list in unused_wildcard_imports.items(): if len(unused_list) == 1: arg_string = unused_list[0] else: arg_string = ( f"{', '.join(i for i in unused_list[:-1])} and {unused_list[-1]}" ) self.add_message( "unused-wildcard-import", args=(arg_string, module[0]), node=module[1] ) del self._to_consume def _check_metaclasses(self, node): """Update consumption analysis for metaclasses.""" consumed = [] # [(scope_locals, consumed_key)] for child_node in node.get_children(): if isinstance(child_node, nodes.ClassDef): consumed.extend(self._check_classdef_metaclasses(child_node, node)) # Pop the consumed items, in order to avoid having # unused-import and unused-variable false positives for scope_locals, name in consumed: scope_locals.pop(name, None) def _check_classdef_metaclasses(self, klass, parent_node): if not klass._metaclass: # Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors return [] consumed = [] # [(scope_locals, consumed_key)] metaclass = klass.metaclass() name = None if isinstance(klass._metaclass, nodes.Name): name = klass._metaclass.name elif isinstance(klass._metaclass, nodes.Attribute) and klass._metaclass.expr: attr = klass._metaclass.expr while not isinstance(attr, nodes.Name): attr = attr.expr name = attr.name elif metaclass: name = metaclass.root().name found = _False name = METACLASS_NAME_TRANSFORMS.get(name, name) if name: # check enclosing scopes starting from most local for scope_locals, _, _, _ in self._to_consume[::-1]: found_nodes = scope_locals.get(name, []) for found_node in found_nodes: if found_node.lineno <= klass.lineno: consumed.append((scope_locals, name)) found = _True break # Check parent scope nodes_in_parent_scope = parent_node.locals.get(name, []) for found_node_parent in nodes_in_parent_scope: if found_node_parent.lineno <= klass.lineno: found = _True break if ( not found and not metaclass and not ( name in nodes.Module.scope_attrs or utils.is_builtin(name) or name in self.config.additional_builtins ) ): self.add_message("undefined-variable", node=klass, args=(name,)) return consumed
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,221
NamesConsumer
ref
function
self._to_consume.append(NamesConsumer(node, "comprehension"))
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,223
leave_setcomp
def
function
def leave_setcomp(self, _: nodes.SetComp) -> None: """Leave setcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Visit function: update consumption analysis variable and check locals.""" self._to_consume.append(NamesConsumer(node, "function")) if not ( self.linter.is_message_enabled("redefined-outer-name") or self.linter.is_message_enabled("redefined-builtin") ): return globs = node.root().globals for name, stmt in node.items(): if name in globs and not isinstance(stmt, nodes.Global): definition = globs[name][0] if ( isinstance(definition, nodes.ImportFrom) and definition.modname == FUTURE ): # It is a __future__ directive, not a symbol. continue # Do not take in account redefined names for the purpose # of type checking.: if any( isinstance(definition.parent, nodes.If) and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS for definition in globs[name] ): continue line = definition.fromlineno if not self._is_name_ignored(stmt, name): self.add_message( "redefined-outer-name", args=(name, line), node=stmt ) elif ( utils.is_builtin(name) and not self._allowed_redefined_builtin(name) and not self._should_ignore_redefined_builtin(stmt) ): # do not print Redefining builtin for additional builtins self.add_message("redefined-builtin", args=name, node=stmt) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Leave function: check function's locals are consumed.""" self._check_metaclasses(node) if node.type_comment_returns: self._store_type_annotation_node(node.type_comment_returns) if node.type_comment_args: for argument_annotation in node.type_comment_args: self._store_type_annotation_node(argument_annotation) not_consumed = self._to_consume.pop().to_consume if not ( self.linter.is_message_enabled("unused-variable") or self.linter.is_message_enabled("possibly-unused-variable") or self.linter.is_message_enabled("unused-argument") ): return # Don't check arguments of function which are only raising an exception. if utils.is_error(node): return # Don't check arguments of abstract methods or within an interface. is_method = node.is_method() if is_method and node.is_abstract(): return global_names = _flattened_scope_names(node.nodes_of_class(nodes.Global)) nonlocal_names = _flattened_scope_names(node.nodes_of_class(nodes.Nonlocal)) for name, stmts in not_consumed.items(): self._check_is_unused(name, node, stmts[0], global_names, nonlocal_names) visit_asyncfunctiondef = visit_functiondef leave_asyncfunctiondef = leave_functiondef @utils.check_messages( "global-variable-undefined", "global-variable-not-assigned", "global-statement", "global-at-module-level", "redefined-builtin", ) def visit_global(self, node: nodes.Global) -> None: """Check names imported exists in the global scope.""" frame = node.frame(future=_True) if isinstance(frame, nodes.Module): self.add_message("global-at-module-level", node=node) return module = frame.root() default_message = _True locals_ = node.scope().locals for name in node.names: try: assign_nodes = module.getattr(name) except astroid.NotFoundError: # unassigned global, skip assign_nodes = [] not_defined_locally_by_import = not any( isinstance(local, nodes.Import) for local in locals_.get(name, ()) ) if ( not utils.is_reassigned_after_current(node, name) and not utils.is_deleted_after_current(node, name) and not_defined_locally_by_import ): self.add_message("global-variable-not-assigned", args=name, node=node) default_message = _False continue for anode in assign_nodes: if ( isinstance(anode, nodes.AssignName) and anode.name in module.special_attributes ): self.add_message("redefined-builtin", args=name, node=node) break if anode.frame(future=_True) is module: # module level assignment break if ( isinstance(anode, (nodes.ClassDef, nodes.FunctionDef)) and anode.parent is module ): # module level function assignment break else: if not_defined_locally_by_import: # global undefined at the module scope self.add_message("global-variable-undefined", args=name, node=node) default_message = _False if default_message: self.add_message("global-statement", node=node) def visit_assignname(self, node: nodes.AssignName) -> None: if isinstance(node.assign_type(), nodes.AugAssign): self.visit_name(node) def visit_delname(self, node: nodes.DelName) -> None: self.visit_name(node) def visit_name(self, node: nodes.Name) -> None: """Don't add the 'utils.check_messages' decorator here! It's important that all 'Name' nodes are visited, otherwise the 'NamesConsumers' won't be correct. """ stmt = node.statement(future=_True) if stmt.fromlineno is None: # name node from an astroid built from live code, skip assert not stmt.root().file.endswith(".py") return self._undefined_and_used_before_checker(node, stmt) if self._is_undefined_loop_variable_enabled: self._loopvar_name(node) @utils.check_messages("redefined-outer-name") def visit_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return for outer_except, outer_except_assign_name in self._except_handler_names_queue: if node.name.name == outer_except_assign_name.name: self.add_message( "redefined-outer-name", args=(outer_except_assign_name.name, outer_except.fromlineno), node=node, ) break self._except_handler_names_queue.append((node, node.name)) @utils.check_messages("redefined-outer-name") def leave_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return self._except_handler_names_queue.pop() def _undefined_and_used_before_checker( self, node: nodes.Name, stmt: nodes.NodeNG ) -> None: frame = stmt.scope() start_index = len(self._to_consume) - 1 # iterates through parent scopes, from the inner to the outer base_scope_type = self._to_consume[start_index].scope_type for i in range(start_index, -1, -1): current_consumer = self._to_consume[i] # Certain nodes shouldn't be checked as they get checked another time if self._should_node_be_skipped(node, current_consumer, i == start_index): continue action, nodes_to_consume = self._check_consumer( node, stmt, frame, current_consumer, i, base_scope_type ) if nodes_to_consume: # Any nodes added to consumed_uncertain by get_next_to_consume() # should be added back so that they are marked as used. # They will have already had a chance to emit used-before-assignment. # We check here instead of before every single return in _check_consumer() nodes_to_consume += current_consumer.consumed_uncertain[node.name] current_consumer.mark_as_consumed(node.name, nodes_to_consume) if action is VariableVisitConsumerAction.CONTINUE: continue if action is VariableVisitConsumerAction.RETURN: return # we have not found the name, if it isn't a builtin, that's an # undefined name ! if ( self._is_undefined_variable_enabled and not ( node.name in nodes.Module.scope_attrs or utils.is_builtin(node.name) or node.name in self.config.additional_builtins or ( node.name == "__class__" and isinstance(frame, nodes.FunctionDef) and frame.is_method() ) ) and not utils.node_ignores_exception(node, NameError) ): self.add_message("undefined-variable", args=node.name, node=node) def _should_node_be_skipped( self, node: nodes.Name, consumer: NamesConsumer, is_start_index: bool ) -> bool: """Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks. """ if consumer.scope_type == "class": # The list of base classes in the class definition is not part # of the class body. # If the current scope is a class scope but it's not the inner # scope, ignore it. This prevents to access this scope instead of # the globals one in function members when there are some common # names. if utils.is_ancestor_name(consumer.node, node) or ( not is_start_index and self._ignore_class_scope(node) ): return _True # Ignore inner class scope for keywords in class definition if isinstance(node.parent, nodes.Keyword) and isinstance( node.parent.parent, nodes.ClassDef ): return _True elif consumer.scope_type == "function" and self._defined_in_function_definition( node, consumer.node ): # If the name node is used as a function default argument's value or as # a decorator, then start from the parent frame of the function instead # of the function frame - and thus open an inner class scope return _True elif consumer.scope_type == "lambda" and utils.is_default_argument( node, consumer.node ): return _True return _False # pylint: disable=too-many-return-statements def _check_consumer( self, node: nodes.Name, stmt: nodes.NodeNG, frame: nodes.LocalsDictNodeNG, current_consumer: NamesConsumer, consumer_level: int, base_scope_type: Any, ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Checks a consumer for conditions that should trigger messages.""" # If the name has already been consumed, only check it's not a loop # variable used outside the loop. # Avoid the case where there are homonyms inside function scope and # comprehension current scope (avoid bug #1731) if node.name in current_consumer.consumed: if utils.is_func_decorator(current_consumer.node) or not ( current_consumer.scope_type == "comprehension" and self._has_homonym_in_upper_function_scope(node, consumer_level) # But don't catch homonyms against the filter of a comprehension, # (like "if x" in "[x for x in expr() if x]") # https://github.com/PyCQA/pylint/issues/5586 and not ( ( isinstance(node.parent.parent, nodes.Comprehension) and node.parent in node.parent.parent.ifs ) # Or homonyms against values to keyword arguments # (like "var" in "[func(arg=var) for var in expr()]") or ( isinstance(node.scope(), nodes.ComprehensionScope) and isinstance(node.parent, (nodes.Call, nodes.Keyword)) ) ) ): self._check_late_binding_closure(node) self._loopvar_name(node) return (VariableVisitConsumerAction.RETURN, None) found_nodes = current_consumer.get_next_to_consume(node) if found_nodes is None: return (VariableVisitConsumerAction.CONTINUE, None) if not found_nodes: if node.name in current_consumer.consumed_uncertain: confidence = CONTROL_FLOW else: confidence = HIGH self.add_message( "used-before-assignment", args=node.name, node=node, confidence=confidence, ) # Mark for consumption any nodes added to consumed_uncertain by # get_next_to_consume() because they might not have executed. return ( VariableVisitConsumerAction.RETURN, current_consumer.consumed_uncertain[node.name], ) self._check_late_binding_closure(node) if not ( self._is_undefined_variable_enabled or self._is_used_before_assignment_enabled ): return (VariableVisitConsumerAction.RETURN, found_nodes) defnode = utils.assign_parent(found_nodes[0]) defstmt = defnode.statement(future=_True) defframe = defstmt.frame(future=_True) # The class reuses itself in the class scope. is_recursive_klass = ( frame is defframe and defframe.parent_of(node) and isinstance(defframe, nodes.ClassDef) and node.name == defframe.name ) if ( is_recursive_klass and utils.get_node_first_ancestor_of_type(node, nodes.Lambda) and ( not utils.is_default_argument(node) or node.scope().parent.scope() is not defframe ) ): # Self-referential class references are fine in lambda's -- # As long as they are not part of the default argument directly # under the scope of the parent self-referring class. # Example of valid default argument: # class MyName3: # myattr = 1 # mylambda3 = lambda: lambda a=MyName3: a # Example of invalid default argument: # class MyName4: # myattr = 1 # mylambda4 = lambda a=MyName4: lambda: a # If the above conditional is _True, # there is no possibility of undefined-variable # Also do not consume class name # (since consuming blocks subsequent checks) # -- quit return (VariableVisitConsumerAction.RETURN, None) ( maybe_before_assign, annotation_return, use_outer_definition, ) = self._is_variable_violation( node, defnode, stmt, defstmt, frame, defframe, base_scope_type, is_recursive_klass, ) if use_outer_definition: return (VariableVisitConsumerAction.CONTINUE, None) if ( maybe_before_assign and not utils.is_defined_before(node) and not astroid.are_exclusive(stmt, defstmt, ("NameError",)) ): # Used and defined in the same place, e.g `x += 1` and `del x` defined_by_stmt = defstmt is stmt and isinstance( node, (nodes.DelName, nodes.AssignName) ) if ( is_recursive_klass or defined_by_stmt or annotation_return or isinstance(defstmt, nodes.Delete) ): if not utils.node_ignores_exception(node, NameError): # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance( stmt, ( nodes.AnnAssign, nodes.FunctionDef, nodes.Arguments, ), ) and node.name in node.root().locals ): if defined_by_stmt: return (VariableVisitConsumerAction.CONTINUE, [node]) return (VariableVisitConsumerAction.CONTINUE, None) elif base_scope_type != "lambda": # E0601 may *not* occurs in lambda scope. # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif base_scope_type == "lambda": # E0601 can occur in class-level scope in lambdas, as in # the following example: # class A: # x = lambda attr: f + attr # f = 42 # We check lineno because doing the following is fine: # class A: # x = 42 # y = lambda attr: x + attr if ( isinstance(frame, nodes.ClassDef) and node.name in frame.locals and stmt.fromlineno <= defstmt.fromlineno ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) elif self._is_only_type_assignment(node, defstmt): if node.scope().locals.get(node.name): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH ) else: self.add_message( "undefined-variable", args=node.name, node=node, confidence=HIGH ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif isinstance(defstmt, nodes.ClassDef): return self._is_first_level_self_reference(node, defstmt, found_nodes) elif isinstance(defnode, nodes.NamedExpr): if isinstance(defnode.parent, nodes.IfExp): if self._is_never_evaluated(defnode, defnode.parent): self.add_message( "undefined-variable", args=node.name, node=node, confidence=INFERENCE, ) return (VariableVisitConsumerAction.RETURN, found_nodes) return (VariableVisitConsumerAction.RETURN, found_nodes) @utils.check_messages("no-name-in-module") def visit_import(self, node: nodes.Import) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return for name, _ in node.names: parts = name.split(".") try: module = next(_infer_name_module(node, parts[0])) except astroid.ResolveError: continue if not isinstance(module, nodes.Module): continue self._check_module_attrs(node, module, parts[1:]) @utils.check_messages("no-name-in-module") def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return name_parts = node.modname.split(".") try: module = node.do_import_module(name_parts[0]) except astroid.AstroidBuildingException: return module = self._check_module_attrs(node, module, name_parts[1:]) if not module: return for name, _ in node.names: if name == "*": continue self._check_module_attrs(node, module, name.split(".")) @utils.check_messages( "unbalanced-tuple-unpacking", "unpacking-non-sequence", "self-cls-assignment" ) def visit_assign(self, node: nodes.Assign) -> None: """Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned. """ self._check_self_cls_assign(node) if not isinstance(node.targets[0], (nodes.Tuple, nodes.List)): return targets = node.targets[0].itered() try: inferred = utils.safe_infer(node.value) if inferred is not None: self._check_unpacking(inferred, node, targets) except astroid.InferenceError: return # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_listcomp(self, _: nodes.ListComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def leave_assign(self, node: nodes.Assign) -> None: self._store_type_annotation_names(node) def leave_with(self, node: nodes.With) -> None: self._store_type_annotation_names(node) def visit_arguments(self, node: nodes.Arguments) -> None: for annotation in node.type_comment_args: self._store_type_annotation_node(annotation) # Relying on other checker's options, which might not have been initialized yet. @astroid.decorators.cachedproperty def _analyse_fallback_blocks(self): return get_global_option(self, "analyse-fallback-blocks", default=_False) @astroid.decorators.cachedproperty def _ignored_modules(self): return get_global_option(self, "ignored-modules", default=[]) @astroid.decorators.cachedproperty def _allow_global_unused_variables(self): return get_global_option(self, "allow-global-unused-variables", default=_True) @staticmethod def _defined_in_function_definition(node, frame): in_annotation_or_default_or_decorator = _False if ( isinstance(frame, nodes.FunctionDef) and node.statement(future=_True) is frame ): in_annotation_or_default_or_decorator = ( ( node in frame.args.annotations or node in frame.args.posonlyargs_annotations or node in frame.args.kwonlyargs_annotations or node is frame.args.varargannotation or node is frame.args.kwargannotation ) or frame.args.parent_of(node) or (frame.decorators and frame.decorators.parent_of(node)) or ( frame.returns and (node is frame.returns or frame.returns.parent_of(node)) ) ) return in_annotation_or_default_or_decorator @staticmethod def _in_lambda_or_comprehension_body( node: nodes.NodeNG, frame: nodes.NodeNG ) -> bool: """Return _True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.""" child = node parent = node.parent while parent is not None: if parent is frame: return _False if isinstance(parent, nodes.Lambda) and child is not parent.args: # Body of lambda should not have access to class attributes. return _True if isinstance(parent, nodes.Comprehension) and child is not parent.iter: # Only iter of list/set/dict/generator comprehension should have access. return _True if isinstance(parent, nodes.ComprehensionScope) and not ( parent.generators and child is parent.generators[0] ): # Body of list/set/dict/generator comprehension should not have access to class attributes. # Furthermore, only the first generator (if multiple) in comprehension should have access. return _True child = parent parent = parent.parent return _False @staticmethod def _is_variable_violation( node: nodes.Name, defnode, stmt: nodes.Statement, defstmt: nodes.Statement, frame, # scope of statement of node defframe, base_scope_type, is_recursive_klass, ) -> Tuple[bool, bool, bool]: # pylint: disable=too-many-nested-blocks maybe_before_assign = _True annotation_return = _False use_outer_definition = _False if frame is not defframe: maybe_before_assign = _detect_global_scope(node, frame, defframe) elif defframe.parent is None: # we are at the module level, check the name is not # defined in builtins if ( node.name in defframe.scope_attrs or astroid.builtin_lookup(node.name)[1] ): maybe_before_assign = _False else: # we are in a local scope, check the name is not # defined in global or builtin scope # skip this lookup if name is assigned later in function scope/lambda # Note: the node.frame() is not the same as the `frame` argument which is # equivalent to frame.statement().scope() forbid_lookup = ( isinstance(frame, nodes.FunctionDef) or isinstance(node.frame(future=_True), nodes.Lambda) ) and _assigned_locally(node) if not forbid_lookup and defframe.root().lookup(node.name)[1]: maybe_before_assign = _False use_outer_definition = stmt == defstmt and not isinstance( defnode, nodes.Comprehension ) # check if we have a nonlocal elif node.name in defframe.locals: maybe_before_assign = not any( isinstance(child, nodes.Nonlocal) and node.name in child.names for child in defframe.get_children() ) if ( base_scope_type == "lambda" and isinstance(frame, nodes.ClassDef) and node.name in frame.locals ): # This rule verifies that if the definition node of the # checked name is an Arguments node and if the name # is used a default value in the arguments defaults # and the actual definition of the variable label # is happening before the Arguments definition. # # bar = None # foo = lambda bar=bar: bar # # In this case, maybe_before_assign should be _False, otherwise # it should be _True. maybe_before_assign = not ( isinstance(defnode, nodes.Arguments) and node in defnode.defaults and frame.locals[node.name][0].fromlineno < defstmt.fromlineno ) elif isinstance(defframe, nodes.ClassDef) and isinstance( frame, nodes.FunctionDef ): # Special rule for function return annotations, # using a name defined earlier in the class containing the function. if node is frame.returns and defframe.parent_of(frame.returns): annotation_return = _True if ( frame.returns.name in defframe.locals and defframe.locals[node.name][0].lineno < frame.lineno ): # Detect class assignments with a name defined earlier in the # class. In this case, no warning should be raised. maybe_before_assign = _False else: maybe_before_assign = _True if isinstance(node.parent, nodes.Arguments): maybe_before_assign = stmt.fromlineno <= defstmt.fromlineno elif is_recursive_klass: maybe_before_assign = _True else: maybe_before_assign = ( maybe_before_assign and stmt.fromlineno <= defstmt.fromlineno ) if maybe_before_assign and stmt.fromlineno == defstmt.fromlineno: if ( isinstance(defframe, nodes.FunctionDef) and frame is defframe and defframe.parent_of(node) and stmt is not defstmt ): # Single statement function, with the statement on the # same line as the function definition maybe_before_assign = _False elif ( isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Expr, nodes.Return, ), ) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) and frame is defframe and defframe.parent_of(node) and stmt is defstmt ): # Single statement if, with assignment expression on same # line as assignment # x = b if (b := _True) else _False maybe_before_assign = _False elif ( isinstance( # pylint: disable=too-many-boolean-expressions defnode, nodes.NamedExpr ) and frame is defframe and defframe.parent_of(stmt) and stmt is defstmt and ( ( defnode.lineno == node.lineno and defnode.col_offset < node.col_offset ) or (defnode.lineno < node.lineno) or ( # Issue in the `ast` module until py39 # Nodes in a multiline string have the same lineno # Could be false-positive without check not PY39_PLUS and defnode.lineno == node.lineno and isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Return, ), ) and isinstance(defstmt.value, nodes.JoinedStr) ) ) ): # Expressions, with assignment expressions # Use only after assignment # b = (c := 2) and c maybe_before_assign = _False # Look for type checking definitions inside a type checking guard. if isinstance(defstmt, (nodes.Import, nodes.ImportFrom)): defstmt_parent = defstmt.parent if ( isinstance(defstmt_parent, nodes.If) and defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS ): # Exempt those definitions that are used inside the type checking # guard or that are defined in both type checking guard branches. used_in_branch = defstmt_parent.parent_of(node) defined_in_or_else = _False for definition in defstmt_parent.orelse: if isinstance(definition, nodes.Assign): defined_in_or_else = any( target.name == node.name for target in definition.targets if isinstance(target, nodes.AssignName) ) if defined_in_or_else: break if not used_in_branch and not defined_in_or_else: maybe_before_assign = _True return maybe_before_assign, annotation_return, use_outer_definition @staticmethod def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: """Check if `defstmt` has the potential to use and assign a name in the same statement. """ if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: # The assignment must happen as part of the first element # e.g. "assert (x:= _True), x" # NOT "assert x, (x:= _True)" value = defstmt.value.elts[0] else: value = defstmt.value if isinstance(value, nodes.IfExp): return _True if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp): return _True return isinstance(value, nodes.Call) and ( any(isinstance(kwarg.value, nodes.IfExp) for kwarg in value.keywords) or any(isinstance(arg, nodes.IfExp) for arg in value.args) ) @staticmethod def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool: """Check if variable only gets assigned a type and never a value.""" if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value: return _False defstmt_frame = defstmt.frame(future=_True) node_frame = node.frame(future=_True) parent = node while parent is not defstmt_frame.parent: parent_scope = parent.scope() local_refs = parent_scope.locals.get(node.name, []) for ref_node in local_refs: # If local ref is in the same frame as our node, but on a later lineno # we don't actually care about this local ref. # Local refs are ordered, so we break. # print(var) # var = 1 # <- irrelevant if defstmt_frame == node_frame and ref_node.lineno > node.lineno: break # If the parent of the local reference is anything but an AnnAssign # Or if the AnnAssign adds a value the variable will now have a value # var = 1 # OR # var: int = 1 if ( not isinstance(ref_node.parent, nodes.AnnAssign) or ref_node.parent.value ): return _False parent = parent_scope.parent return _True @staticmethod def _is_first_level_self_reference( node: nodes.Name, defstmt: nodes.ClassDef, found_nodes: List[nodes.NodeNG] ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Check if a first level method's annotation or default values refers to its own class, and return a consumer action """ if node.frame(future=_True).parent == defstmt and node.statement( future=_True ) == node.frame(future=_True): # Check if used as type annotation # Break if postponed evaluation is enabled if utils.is_node_in_type_annotation_context(node): if not utils.is_postponed_evaluation_enabled(node): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, None) # Check if used as default value by calling the class if isinstance(node.parent, nodes.Call) and isinstance( node.parent.parent, nodes.Arguments ): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, found_nodes) @staticmethod def _is_never_evaluated( defnode: nodes.NamedExpr, defnode_parent: nodes.IfExp ) -> bool: """Check if a NamedExpr is inside a side of if ... else that never gets evaluated """ inferred_test = utils.safe_infer(defnode_parent.test) if isinstance(inferred_test, nodes.Const): if inferred_test.value is _True and defnode == defnode_parent.orelse: return _True if inferred_test.value is _False and defnode == defnode_parent.body: return _True return _False def _ignore_class_scope(self, node): """Return _True if the node is in a local class scope, as an assignment. :param node: Node considered :type node: astroid.Node :return: _True if the node is in a local class scope, as an assignment. _False otherwise. :rtype: bool """ # Detect if we are in a local class scope, as an assignment. # For example, the following is fair game. # # class A: # b = 1 # c = lambda b=b: b * b # # class B: # tp = 1 # def func(self, arg: tp): # ... # class C: # tp = 2 # def func(self, arg=tp): # ... # class C: # class Tp: # pass # class D(Tp): # ... name = node.name frame = node.statement(future=_True).scope() in_annotation_or_default_or_decorator = self._defined_in_function_definition( node, frame ) in_ancestor_list = utils.is_ancestor_name(frame, node) if in_annotation_or_default_or_decorator or in_ancestor_list: frame_locals = frame.parent.scope().locals else: frame_locals = frame.locals return not ( (isinstance(frame, nodes.ClassDef) or in_annotation_or_default_or_decorator) and not self._in_lambda_or_comprehension_body(node, frame) and name in frame_locals ) def _loopvar_name(self, node: astroid.Name) -> None: # filter variables according to node's scope astmts = [s for s in node.lookup(node.name)[1] if hasattr(s, "assign_type")] # If this variable usage exists inside a function definition # that exists in the same loop, # the usage is safe because the function will not be defined either if # the variable is not defined. scope = node.scope() if isinstance(scope, nodes.FunctionDef) and any( asmt.scope().parent_of(scope) for asmt in astmts ): return # Filter variables according to their respective scope. Test parent # and statement to avoid #74747. This is not a total fix, which would # introduce a mechanism similar to special attribute lookup in # modules. Also, in order to get correct inference in this case, the # scope lookup rules would need to be changed to return the initial # assignment (which does not exist in code per se) as well as any later # modifications. # pylint: disable-next=too-many-boolean-expressions if ( not astmts or ( astmts[0].parent == astmts[0].root() and astmts[0].parent.parent_of(node) ) or ( astmts[0].is_statement or not isinstance(astmts[0].parent, nodes.Module) and astmts[0].statement(future=_True).parent_of(node) ) ): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement(future=_True).parent_of( stmt ) and not in_for_else_branch(astmts[i].statement(future=_True), stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) != 1: return assign = astmts[0].assign_type() if not ( isinstance(assign, (nodes.For, nodes.Comprehension, nodes.GeneratorExp)) and assign.statement(future=_True) is not node.statement(future=_True) ): return # For functions we can do more by inferring the length of the itered object if not isinstance(assign, nodes.For): self.add_message("undefined-loop-variable", args=node.name, node=node) return try: inferred = next(assign.iter.infer()) except astroid.InferenceError: self.add_message("undefined-loop-variable", args=node.name, node=node) else: if ( isinstance(inferred, astroid.Instance) and inferred.qname() == BUILTIN_RANGE ): # Consider range() objects safe, even if they might not yield any results. return # Consider sequences. sequences = ( nodes.List, nodes.Tuple, nodes.Dict, nodes.Set, astroid.objects.FrozenSet, ) if not isinstance(inferred, sequences): self.add_message("undefined-loop-variable", args=node.name, node=node) return elements = getattr(inferred, "elts", getattr(inferred, "items", [])) if not elements: self.add_message("undefined-loop-variable", args=node.name, node=node) def _check_is_unused(self, name, node, stmt, global_names, nonlocal_names): # Ignore some special names specified by user configuration. if self._is_name_ignored(stmt, name): return # Ignore names that were added dynamically to the Function scope if ( isinstance(node, nodes.FunctionDef) and name == "__class__" and len(node.locals["__class__"]) == 1 and isinstance(node.locals["__class__"][0], nodes.ClassDef) ): return # Ignore names imported by the global statement. if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)): # Detect imports, assigned to global statements. if global_names and _import_name_is_global(stmt, global_names): return argnames = list( itertools.chain(node.argnames(), [arg.name for arg in node.args.kwonlyargs]) ) # Care about functions with unknown argument (builtins) if name in argnames: self._check_unused_arguments(name, node, stmt, argnames) else: if stmt.parent and isinstance( stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple) ): if name in nonlocal_names: return qname = asname = None if isinstance(stmt, (nodes.Import, nodes.ImportFrom)): # Need the complete name, which we don't have in .locals. if len(stmt.names) > 1: import_names = next( (names for names in stmt.names if name in names), None ) else: import_names = stmt.names[0] if import_names: qname, asname = import_names name = asname or qname if _has_locals_call_after_node(stmt, node.scope()): message_name = "possibly-unused-variable" else: if isinstance(stmt, nodes.Import): if asname is not None: msg = f"{qname} imported as {asname}" else: msg = f"import {name}" self.add_message("unused-import", args=msg, node=stmt) return if isinstance(stmt, nodes.ImportFrom): if asname is not None: msg = f"{qname} imported from {stmt.modname} as {asname}" else: msg = f"{name} imported from {stmt.modname}" self.add_message("unused-import", args=msg, node=stmt) return message_name = "unused-variable" if isinstance(stmt, nodes.FunctionDef) and stmt.decorators: return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Special case for exception variable if isinstance(stmt.parent, nodes.ExceptHandler) and any( n.name == name for n in stmt.parent.nodes_of_class(nodes.Name) ): return self.add_message(message_name, args=name, node=stmt) def _is_name_ignored(self, stmt, name): authorized_rgx = self.config.dummy_variables_rgx if ( isinstance(stmt, nodes.AssignName) and isinstance(stmt.parent, nodes.Arguments) or isinstance(stmt, nodes.Arguments) ): regex = self.config.ignored_argument_names else: regex = authorized_rgx return regex and regex.match(name) def _check_unused_arguments(self, name, node, stmt, argnames): is_method = node.is_method() klass = node.parent.frame(future=_True) if is_method and isinstance(klass, nodes.ClassDef): confidence = ( INFERENCE if utils.has_known_bases(klass) else INFERENCE_FAILURE ) else: confidence = HIGH if is_method: # Don't warn for the first argument of a (non static) method if node.type != "staticmethod" and name == argnames[0]: return # Don't warn for argument of an overridden method overridden = overridden_method(klass, node.name) if overridden is not None and name in overridden.argnames(): return if node.name in utils.PYMETHODS and node.name not in ( "__init__", "__new__", ): return # Don't check callback arguments if any( node.name.startswith(cb) or node.name.endswith(cb) for cb in self.config.callbacks ): return # Don't check arguments of singledispatch.register function. if utils.is_registered_in_singledispatch_function(node): return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Don't check protocol classes if utils.is_protocol_class(klass): return self.add_message("unused-argument", args=name, node=stmt, confidence=confidence) def _check_late_binding_closure(self, node: nodes.Name) -> None: """Check whether node is a cell var that is assigned within a containing loop. Special cases where we don't care about the error: 1. When the node's function is immediately called, e.g. (lambda: i)() 2. When the node's function is returned from within the loop, e.g. return lambda: i """ if not self.linter.is_message_enabled("cell-var-from-loop"): return node_scope = node.frame(future=_True) # If node appears in a default argument expression, # look at the next enclosing frame instead if utils.is_default_argument(node, node_scope): node_scope = node_scope.parent.frame(future=_True) # Check if node is a cell var if ( not isinstance(node_scope, (nodes.Lambda, nodes.FunctionDef)) or node.name in node_scope.locals ): return assign_scope, stmts = node.lookup(node.name) if not stmts or not assign_scope.parent_of(node_scope): return if utils.is_comprehension(assign_scope): self.add_message("cell-var-from-loop", node=node, args=node.name) else: # Look for an enclosing For loop. # Currently, we only consider the first assignment assignment_node = stmts[0] maybe_for = assignment_node while maybe_for and not isinstance(maybe_for, nodes.For): if maybe_for is assign_scope: break maybe_for = maybe_for.parent else: if ( maybe_for and maybe_for.parent_of(node_scope) and not utils.is_being_called(node_scope) and node_scope.parent and not isinstance(node_scope.statement(future=_True), nodes.Return) ): self.add_message("cell-var-from-loop", node=node, args=node.name) def _should_ignore_redefined_builtin(self, stmt): if not isinstance(stmt, nodes.ImportFrom): return _False return stmt.modname in self.config.redefining_builtins_modules def _allowed_redefined_builtin(self, name): return name in self.config.allowed_redefined_builtins def _has_homonym_in_upper_function_scope( self, node: nodes.Name, index: int ) -> bool: """Return whether there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function :param node: node to check for :param index: index of the current consumer inside self._to_consume :return: _True if there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function, _False otherwise """ return any( _consumer.scope_type == "function" and node.name in _consumer.to_consume for _consumer in self._to_consume[index - 1 :: -1] ) def _store_type_annotation_node(self, type_annotation): """Given a type annotation, store all the name nodes it refers to.""" if isinstance(type_annotation, nodes.Name): self._type_annotation_names.append(type_annotation.name) return if isinstance(type_annotation, nodes.Attribute): self._store_type_annotation_node(type_annotation.expr) return if not isinstance(type_annotation, nodes.Subscript): return if ( isinstance(type_annotation.value, nodes.Attribute) and isinstance(type_annotation.value.expr, nodes.Name) and type_annotation.value.expr.name == TYPING_MODULE ): self._type_annotation_names.append(TYPING_MODULE) return self._type_annotation_names.extend( annotation.name for annotation in type_annotation.nodes_of_class(nodes.Name) ) def _store_type_annotation_names(self, node): type_annotation = node.type_annotation if not type_annotation: return self._store_type_annotation_node(node.type_annotation) def _check_self_cls_assign(self, node: nodes.Assign) -> None: """Check that self/cls don't get assigned.""" assign_names: Set[Optional[str]] = set() for target in node.targets: if isinstance(target, nodes.AssignName): assign_names.add(target.name) elif isinstance(target, nodes.Tuple): assign_names.update( elt.name for elt in target.elts if isinstance(elt, nodes.AssignName) ) scope = node.scope() nonlocals_with_same_name = any( child for child in scope.body if isinstance(child, nodes.Nonlocal) ) if nonlocals_with_same_name: scope = node.scope().parent.scope() if not ( isinstance(scope, nodes.FunctionDef) and scope.is_method() and "builtins.staticmethod" not in scope.decoratornames() ): return argument_names = scope.argnames() if not argument_names: return self_cls_name = argument_names[0] if self_cls_name in assign_names: self.add_message("self-cls-assignment", node=node, args=(self_cls_name,)) def _check_unpacking(self, inferred, node, targets): """Check for unbalanced tuple unpacking and unpacking non sequences. """ if utils.is_inside_abstract_class(node): return if utils.is_comprehension(node): return if inferred is astroid.Uninferable: return if ( isinstance(inferred.parent, nodes.Arguments) and isinstance(node.value, nodes.Name) and node.value.name == inferred.parent.vararg ): # Variable-length argument, we can't determine the length. return # Attempt to check unpacking is properly balanced values = self._nodes_to_unpack(inferred) if values is not None: if len(targets) != len(values): # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return self.add_message( "unbalanced-tuple-unpacking", node=node, args=( _get_unpacking_extra_info(node, inferred), len(targets), len(values), ), ) # attempt to check unpacking may be possible (ie RHS is iterable) elif not utils.is_iterable(inferred): self.add_message( "unpacking-non-sequence", node=node, args=(_get_unpacking_extra_info(node, inferred),), ) @staticmethod def _nodes_to_unpack(node: nodes.NodeNG) -> Optional[List[nodes.NodeNG]]: """Return the list of values of the `Assign` node.""" if isinstance(node, (nodes.Tuple, nodes.List)): return node.itered() if isinstance(node, astroid.Instance) and any( ancestor.qname() == "typing.NamedTuple" for ancestor in node.ancestors() ): return [i for i in node.values() if isinstance(i, nodes.AssignName)] return None def _check_module_attrs(self, node, module, module_names): """Check that module_names (list of string) are accessible through the given module if the latest access name corresponds to a module, return it """ while module_names: name = module_names.pop(0) if name == "__dict__": module = None break try: module = next(module.getattr(name)[0].infer()) if module is astroid.Uninferable: return None except astroid.NotFoundError: if module.name in self._ignored_modules: return None self.add_message( "no-name-in-module", args=(name, module.name), node=node ) return None except astroid.InferenceError: return None if module_names: modname = module.name if module else "__dict__" self.add_message( "no-name-in-module", node=node, args=(".".join(module_names), modname) ) return None if isinstance(module, nodes.Module): return module return None def _check_all(self, node: nodes.Module, not_consumed): assigned = next(node.igetattr("__all__")) if assigned is astroid.Uninferable: return if not assigned.pytype() in {"builtins.list", "builtins.tuple"}: line, col = assigned.tolineno, assigned.col_offset self.add_message("invalid-all-format", line=line, col_offset=col, node=node) return for elt in getattr(assigned, "elts", ()): try: elt_name = next(elt.infer()) except astroid.InferenceError: continue if elt_name is astroid.Uninferable: continue if not elt_name.parent: continue if not isinstance(elt_name, nodes.Const) or not isinstance( elt_name.value, str ): self.add_message("invalid-all-object", args=elt.as_string(), node=elt) continue elt_name = elt_name.value # If elt is in not_consumed, remove it from not_consumed if elt_name in not_consumed: del not_consumed[elt_name] continue if elt_name not in node.locals: if not node.package: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) else: basename = os.path.splitext(node.file)[0] if os.path.basename(basename) == "__init__": name = node.name + "." + elt_name try: astroid.modutils.file_from_modpath(name.split(".")) except ImportError: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) except SyntaxError: # don't yield a syntax-error warning, # because it will be later yielded # when the file will be checked pass def _check_globals(self, not_consumed): if self._allow_global_unused_variables: return for name, node_lst in not_consumed.items(): for node in node_lst: self.add_message("unused-variable", args=(name,), node=node) def _check_imports(self, not_consumed): local_names = _fix_dot_imports(not_consumed) checked = set() unused_wildcard_imports: DefaultDict[ Tuple[str, nodes.ImportFrom], List[str] ] = collections.defaultdict(list) for name, stmt in local_names: for imports in stmt.names: real_name = imported_name = imports[0] if imported_name == "*": real_name = name as_name = imports[1] if real_name in checked: continue if name not in (real_name, as_name): continue checked.add(real_name) is_type_annotation_import = ( imported_name in self._type_annotation_names or as_name in self._type_annotation_names ) if isinstance(stmt, nodes.Import) or ( isinstance(stmt, nodes.ImportFrom) and not stmt.modname ): if isinstance(stmt, nodes.ImportFrom) and SPECIAL_OBJ.search( imported_name ): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if as_name == "_": continue if as_name is None: msg = f"import {imported_name}" else: msg = f"{imported_name} imported as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) elif isinstance(stmt, nodes.ImportFrom) and stmt.modname != FUTURE: if SPECIAL_OBJ.search(imported_name): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if _is_from_future_import(stmt, name): # Check if the name is in fact loaded from a # __future__ import in another module. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if imported_name == "*": unused_wildcard_imports[(stmt.modname, stmt)].append(name) else: if as_name is None: msg = f"{imported_name} imported from {stmt.modname}" else: msg = f"{imported_name} imported from {stmt.modname} as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) # Construct string for unused-wildcard-import message for module, unused_list in unused_wildcard_imports.items(): if len(unused_list) == 1: arg_string = unused_list[0] else: arg_string = ( f"{', '.join(i for i in unused_list[:-1])} and {unused_list[-1]}" ) self.add_message( "unused-wildcard-import", args=(arg_string, module[0]), node=module[1] ) del self._to_consume def _check_metaclasses(self, node): """Update consumption analysis for metaclasses.""" consumed = [] # [(scope_locals, consumed_key)] for child_node in node.get_children(): if isinstance(child_node, nodes.ClassDef): consumed.extend(self._check_classdef_metaclasses(child_node, node)) # Pop the consumed items, in order to avoid having # unused-import and unused-variable false positives for scope_locals, name in consumed: scope_locals.pop(name, None) def _check_classdef_metaclasses(self, klass, parent_node): if not klass._metaclass: # Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors return [] consumed = [] # [(scope_locals, consumed_key)] metaclass = klass.metaclass() name = None if isinstance(klass._metaclass, nodes.Name): name = klass._metaclass.name elif isinstance(klass._metaclass, nodes.Attribute) and klass._metaclass.expr: attr = klass._metaclass.expr while not isinstance(attr, nodes.Name): attr = attr.expr name = attr.name elif metaclass: name = metaclass.root().name found = _False name = METACLASS_NAME_TRANSFORMS.get(name, name) if name: # check enclosing scopes starting from most local for scope_locals, _, _, _ in self._to_consume[::-1]: found_nodes = scope_locals.get(name, []) for found_node in found_nodes: if found_node.lineno <= klass.lineno: consumed.append((scope_locals, name)) found = _True break # Check parent scope nodes_in_parent_scope = parent_node.locals.get(name, []) for found_node_parent in nodes_in_parent_scope: if found_node_parent.lineno <= klass.lineno: found = _True break if ( not found and not metaclass and not ( name in nodes.Module.scope_attrs or utils.is_builtin(name) or name in self.config.additional_builtins ) ): self.add_message("undefined-variable", node=klass, args=(name,)) return consumed
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/variables.py
pylint/checkers/variables.py
1,228
visit_functiondef
def
function
def visit_functiondef(self, node: nodes.FunctionDef) -> None: """Visit function: update consumption analysis variable and check locals.""" self._to_consume.append(NamesConsumer(node, "function")) if not ( self.linter.is_message_enabled("redefined-outer-name") or self.linter.is_message_enabled("redefined-builtin") ): return globs = node.root().globals for name, stmt in node.items(): if name in globs and not isinstance(stmt, nodes.Global): definition = globs[name][0] if ( isinstance(definition, nodes.ImportFrom) and definition.modname == FUTURE ): # It is a __future__ directive, not a symbol. continue # Do not take in account redefined names for the purpose # of type checking.: if any( isinstance(definition.parent, nodes.If) and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS for definition in globs[name] ): continue line = definition.fromlineno if not self._is_name_ignored(stmt, name): self.add_message( "redefined-outer-name", args=(name, line), node=stmt ) elif ( utils.is_builtin(name) and not self._allowed_redefined_builtin(name) and not self._should_ignore_redefined_builtin(stmt) ): # do not print Redefining builtin for additional builtins self.add_message("redefined-builtin", args=name, node=stmt) def leave_functiondef(self, node: nodes.FunctionDef) -> None: """Leave function: check function's locals are consumed.""" self._check_metaclasses(node) if node.type_comment_returns: self._store_type_annotation_node(node.type_comment_returns) if node.type_comment_args: for argument_annotation in node.type_comment_args: self._store_type_annotation_node(argument_annotation) not_consumed = self._to_consume.pop().to_consume if not ( self.linter.is_message_enabled("unused-variable") or self.linter.is_message_enabled("possibly-unused-variable") or self.linter.is_message_enabled("unused-argument") ): return # Don't check arguments of function which are only raising an exception. if utils.is_error(node): return # Don't check arguments of abstract methods or within an interface. is_method = node.is_method() if is_method and node.is_abstract(): return global_names = _flattened_scope_names(node.nodes_of_class(nodes.Global)) nonlocal_names = _flattened_scope_names(node.nodes_of_class(nodes.Nonlocal)) for name, stmts in not_consumed.items(): self._check_is_unused(name, node, stmts[0], global_names, nonlocal_names) visit_asyncfunctiondef = visit_functiondef leave_asyncfunctiondef = leave_functiondef @utils.check_messages( "global-variable-undefined", "global-variable-not-assigned", "global-statement", "global-at-module-level", "redefined-builtin", ) def visit_global(self, node: nodes.Global) -> None: """Check names imported exists in the global scope.""" frame = node.frame(future=_True) if isinstance(frame, nodes.Module): self.add_message("global-at-module-level", node=node) return module = frame.root() default_message = _True locals_ = node.scope().locals for name in node.names: try: assign_nodes = module.getattr(name) except astroid.NotFoundError: # unassigned global, skip assign_nodes = [] not_defined_locally_by_import = not any( isinstance(local, nodes.Import) for local in locals_.get(name, ()) ) if ( not utils.is_reassigned_after_current(node, name) and not utils.is_deleted_after_current(node, name) and not_defined_locally_by_import ): self.add_message("global-variable-not-assigned", args=name, node=node) default_message = _False continue for anode in assign_nodes: if ( isinstance(anode, nodes.AssignName) and anode.name in module.special_attributes ): self.add_message("redefined-builtin", args=name, node=node) break if anode.frame(future=_True) is module: # module level assignment break if ( isinstance(anode, (nodes.ClassDef, nodes.FunctionDef)) and anode.parent is module ): # module level function assignment break else: if not_defined_locally_by_import: # global undefined at the module scope self.add_message("global-variable-undefined", args=name, node=node) default_message = _False if default_message: self.add_message("global-statement", node=node) def visit_assignname(self, node: nodes.AssignName) -> None: if isinstance(node.assign_type(), nodes.AugAssign): self.visit_name(node) def visit_delname(self, node: nodes.DelName) -> None: self.visit_name(node) def visit_name(self, node: nodes.Name) -> None: """Don't add the 'utils.check_messages' decorator here! It's important that all 'Name' nodes are visited, otherwise the 'NamesConsumers' won't be correct. """ stmt = node.statement(future=_True) if stmt.fromlineno is None: # name node from an astroid built from live code, skip assert not stmt.root().file.endswith(".py") return self._undefined_and_used_before_checker(node, stmt) if self._is_undefined_loop_variable_enabled: self._loopvar_name(node) @utils.check_messages("redefined-outer-name") def visit_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return for outer_except, outer_except_assign_name in self._except_handler_names_queue: if node.name.name == outer_except_assign_name.name: self.add_message( "redefined-outer-name", args=(outer_except_assign_name.name, outer_except.fromlineno), node=node, ) break self._except_handler_names_queue.append((node, node.name)) @utils.check_messages("redefined-outer-name") def leave_excepthandler(self, node: nodes.ExceptHandler) -> None: if not node.name or not isinstance(node.name, nodes.AssignName): return self._except_handler_names_queue.pop() def _undefined_and_used_before_checker( self, node: nodes.Name, stmt: nodes.NodeNG ) -> None: frame = stmt.scope() start_index = len(self._to_consume) - 1 # iterates through parent scopes, from the inner to the outer base_scope_type = self._to_consume[start_index].scope_type for i in range(start_index, -1, -1): current_consumer = self._to_consume[i] # Certain nodes shouldn't be checked as they get checked another time if self._should_node_be_skipped(node, current_consumer, i == start_index): continue action, nodes_to_consume = self._check_consumer( node, stmt, frame, current_consumer, i, base_scope_type ) if nodes_to_consume: # Any nodes added to consumed_uncertain by get_next_to_consume() # should be added back so that they are marked as used. # They will have already had a chance to emit used-before-assignment. # We check here instead of before every single return in _check_consumer() nodes_to_consume += current_consumer.consumed_uncertain[node.name] current_consumer.mark_as_consumed(node.name, nodes_to_consume) if action is VariableVisitConsumerAction.CONTINUE: continue if action is VariableVisitConsumerAction.RETURN: return # we have not found the name, if it isn't a builtin, that's an # undefined name ! if ( self._is_undefined_variable_enabled and not ( node.name in nodes.Module.scope_attrs or utils.is_builtin(node.name) or node.name in self.config.additional_builtins or ( node.name == "__class__" and isinstance(frame, nodes.FunctionDef) and frame.is_method() ) ) and not utils.node_ignores_exception(node, NameError) ): self.add_message("undefined-variable", args=node.name, node=node) def _should_node_be_skipped( self, node: nodes.Name, consumer: NamesConsumer, is_start_index: bool ) -> bool: """Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks. """ if consumer.scope_type == "class": # The list of base classes in the class definition is not part # of the class body. # If the current scope is a class scope but it's not the inner # scope, ignore it. This prevents to access this scope instead of # the globals one in function members when there are some common # names. if utils.is_ancestor_name(consumer.node, node) or ( not is_start_index and self._ignore_class_scope(node) ): return _True # Ignore inner class scope for keywords in class definition if isinstance(node.parent, nodes.Keyword) and isinstance( node.parent.parent, nodes.ClassDef ): return _True elif consumer.scope_type == "function" and self._defined_in_function_definition( node, consumer.node ): # If the name node is used as a function default argument's value or as # a decorator, then start from the parent frame of the function instead # of the function frame - and thus open an inner class scope return _True elif consumer.scope_type == "lambda" and utils.is_default_argument( node, consumer.node ): return _True return _False # pylint: disable=too-many-return-statements def _check_consumer( self, node: nodes.Name, stmt: nodes.NodeNG, frame: nodes.LocalsDictNodeNG, current_consumer: NamesConsumer, consumer_level: int, base_scope_type: Any, ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Checks a consumer for conditions that should trigger messages.""" # If the name has already been consumed, only check it's not a loop # variable used outside the loop. # Avoid the case where there are homonyms inside function scope and # comprehension current scope (avoid bug #1731) if node.name in current_consumer.consumed: if utils.is_func_decorator(current_consumer.node) or not ( current_consumer.scope_type == "comprehension" and self._has_homonym_in_upper_function_scope(node, consumer_level) # But don't catch homonyms against the filter of a comprehension, # (like "if x" in "[x for x in expr() if x]") # https://github.com/PyCQA/pylint/issues/5586 and not ( ( isinstance(node.parent.parent, nodes.Comprehension) and node.parent in node.parent.parent.ifs ) # Or homonyms against values to keyword arguments # (like "var" in "[func(arg=var) for var in expr()]") or ( isinstance(node.scope(), nodes.ComprehensionScope) and isinstance(node.parent, (nodes.Call, nodes.Keyword)) ) ) ): self._check_late_binding_closure(node) self._loopvar_name(node) return (VariableVisitConsumerAction.RETURN, None) found_nodes = current_consumer.get_next_to_consume(node) if found_nodes is None: return (VariableVisitConsumerAction.CONTINUE, None) if not found_nodes: if node.name in current_consumer.consumed_uncertain: confidence = CONTROL_FLOW else: confidence = HIGH self.add_message( "used-before-assignment", args=node.name, node=node, confidence=confidence, ) # Mark for consumption any nodes added to consumed_uncertain by # get_next_to_consume() because they might not have executed. return ( VariableVisitConsumerAction.RETURN, current_consumer.consumed_uncertain[node.name], ) self._check_late_binding_closure(node) if not ( self._is_undefined_variable_enabled or self._is_used_before_assignment_enabled ): return (VariableVisitConsumerAction.RETURN, found_nodes) defnode = utils.assign_parent(found_nodes[0]) defstmt = defnode.statement(future=_True) defframe = defstmt.frame(future=_True) # The class reuses itself in the class scope. is_recursive_klass = ( frame is defframe and defframe.parent_of(node) and isinstance(defframe, nodes.ClassDef) and node.name == defframe.name ) if ( is_recursive_klass and utils.get_node_first_ancestor_of_type(node, nodes.Lambda) and ( not utils.is_default_argument(node) or node.scope().parent.scope() is not defframe ) ): # Self-referential class references are fine in lambda's -- # As long as they are not part of the default argument directly # under the scope of the parent self-referring class. # Example of valid default argument: # class MyName3: # myattr = 1 # mylambda3 = lambda: lambda a=MyName3: a # Example of invalid default argument: # class MyName4: # myattr = 1 # mylambda4 = lambda a=MyName4: lambda: a # If the above conditional is _True, # there is no possibility of undefined-variable # Also do not consume class name # (since consuming blocks subsequent checks) # -- quit return (VariableVisitConsumerAction.RETURN, None) ( maybe_before_assign, annotation_return, use_outer_definition, ) = self._is_variable_violation( node, defnode, stmt, defstmt, frame, defframe, base_scope_type, is_recursive_klass, ) if use_outer_definition: return (VariableVisitConsumerAction.CONTINUE, None) if ( maybe_before_assign and not utils.is_defined_before(node) and not astroid.are_exclusive(stmt, defstmt, ("NameError",)) ): # Used and defined in the same place, e.g `x += 1` and `del x` defined_by_stmt = defstmt is stmt and isinstance( node, (nodes.DelName, nodes.AssignName) ) if ( is_recursive_klass or defined_by_stmt or annotation_return or isinstance(defstmt, nodes.Delete) ): if not utils.node_ignores_exception(node, NameError): # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance( stmt, ( nodes.AnnAssign, nodes.FunctionDef, nodes.Arguments, ), ) and node.name in node.root().locals ): if defined_by_stmt: return (VariableVisitConsumerAction.CONTINUE, [node]) return (VariableVisitConsumerAction.CONTINUE, None) elif base_scope_type != "lambda": # E0601 may *not* occurs in lambda scope. # Handle postponed evaluation of annotations if not ( self._postponed_evaluation_enabled and isinstance(stmt, (nodes.AnnAssign, nodes.FunctionDef)) ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif base_scope_type == "lambda": # E0601 can occur in class-level scope in lambdas, as in # the following example: # class A: # x = lambda attr: f + attr # f = 42 # We check lineno because doing the following is fine: # class A: # x = 42 # y = lambda attr: x + attr if ( isinstance(frame, nodes.ClassDef) and node.name in frame.locals and stmt.fromlineno <= defstmt.fromlineno ): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH, ) elif self._is_only_type_assignment(node, defstmt): if node.scope().locals.get(node.name): self.add_message( "used-before-assignment", args=node.name, node=node, confidence=HIGH ) else: self.add_message( "undefined-variable", args=node.name, node=node, confidence=HIGH ) return (VariableVisitConsumerAction.RETURN, found_nodes) elif isinstance(defstmt, nodes.ClassDef): return self._is_first_level_self_reference(node, defstmt, found_nodes) elif isinstance(defnode, nodes.NamedExpr): if isinstance(defnode.parent, nodes.IfExp): if self._is_never_evaluated(defnode, defnode.parent): self.add_message( "undefined-variable", args=node.name, node=node, confidence=INFERENCE, ) return (VariableVisitConsumerAction.RETURN, found_nodes) return (VariableVisitConsumerAction.RETURN, found_nodes) @utils.check_messages("no-name-in-module") def visit_import(self, node: nodes.Import) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return for name, _ in node.names: parts = name.split(".") try: module = next(_infer_name_module(node, parts[0])) except astroid.ResolveError: continue if not isinstance(module, nodes.Module): continue self._check_module_attrs(node, module, parts[1:]) @utils.check_messages("no-name-in-module") def visit_importfrom(self, node: nodes.ImportFrom) -> None: """Check modules attribute accesses.""" if not self._analyse_fallback_blocks and utils.is_from_fallback_block(node): # No need to verify this, since ImportError is already # handled by the client code. return if utils.is_node_in_guarded_import_block(node) is _True: # Don't verify import if part of guarded import block # I.e. `sys.version_info` or `typing.TYPE_CHECKING` return name_parts = node.modname.split(".") try: module = node.do_import_module(name_parts[0]) except astroid.AstroidBuildingException: return module = self._check_module_attrs(node, module, name_parts[1:]) if not module: return for name, _ in node.names: if name == "*": continue self._check_module_attrs(node, module, name.split(".")) @utils.check_messages( "unbalanced-tuple-unpacking", "unpacking-non-sequence", "self-cls-assignment" ) def visit_assign(self, node: nodes.Assign) -> None: """Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned. """ self._check_self_cls_assign(node) if not isinstance(node.targets[0], (nodes.Tuple, nodes.List)): return targets = node.targets[0].itered() try: inferred = utils.safe_infer(node.value) if inferred is not None: self._check_unpacking(inferred, node, targets) except astroid.InferenceError: return # listcomp have now also their scope def visit_listcomp(self, node: nodes.ListComp) -> None: """Visit dictcomp: update consumption analysis variable.""" self._to_consume.append(NamesConsumer(node, "comprehension")) def leave_listcomp(self, _: nodes.ListComp) -> None: """Leave dictcomp: update consumption analysis variable.""" # do not check for not used locals here self._to_consume.pop() def leave_assign(self, node: nodes.Assign) -> None: self._store_type_annotation_names(node) def leave_with(self, node: nodes.With) -> None: self._store_type_annotation_names(node) def visit_arguments(self, node: nodes.Arguments) -> None: for annotation in node.type_comment_args: self._store_type_annotation_node(annotation) # Relying on other checker's options, which might not have been initialized yet. @astroid.decorators.cachedproperty def _analyse_fallback_blocks(self): return get_global_option(self, "analyse-fallback-blocks", default=_False) @astroid.decorators.cachedproperty def _ignored_modules(self): return get_global_option(self, "ignored-modules", default=[]) @astroid.decorators.cachedproperty def _allow_global_unused_variables(self): return get_global_option(self, "allow-global-unused-variables", default=_True) @staticmethod def _defined_in_function_definition(node, frame): in_annotation_or_default_or_decorator = _False if ( isinstance(frame, nodes.FunctionDef) and node.statement(future=_True) is frame ): in_annotation_or_default_or_decorator = ( ( node in frame.args.annotations or node in frame.args.posonlyargs_annotations or node in frame.args.kwonlyargs_annotations or node is frame.args.varargannotation or node is frame.args.kwargannotation ) or frame.args.parent_of(node) or (frame.decorators and frame.decorators.parent_of(node)) or ( frame.returns and (node is frame.returns or frame.returns.parent_of(node)) ) ) return in_annotation_or_default_or_decorator @staticmethod def _in_lambda_or_comprehension_body( node: nodes.NodeNG, frame: nodes.NodeNG ) -> bool: """Return _True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.""" child = node parent = node.parent while parent is not None: if parent is frame: return _False if isinstance(parent, nodes.Lambda) and child is not parent.args: # Body of lambda should not have access to class attributes. return _True if isinstance(parent, nodes.Comprehension) and child is not parent.iter: # Only iter of list/set/dict/generator comprehension should have access. return _True if isinstance(parent, nodes.ComprehensionScope) and not ( parent.generators and child is parent.generators[0] ): # Body of list/set/dict/generator comprehension should not have access to class attributes. # Furthermore, only the first generator (if multiple) in comprehension should have access. return _True child = parent parent = parent.parent return _False @staticmethod def _is_variable_violation( node: nodes.Name, defnode, stmt: nodes.Statement, defstmt: nodes.Statement, frame, # scope of statement of node defframe, base_scope_type, is_recursive_klass, ) -> Tuple[bool, bool, bool]: # pylint: disable=too-many-nested-blocks maybe_before_assign = _True annotation_return = _False use_outer_definition = _False if frame is not defframe: maybe_before_assign = _detect_global_scope(node, frame, defframe) elif defframe.parent is None: # we are at the module level, check the name is not # defined in builtins if ( node.name in defframe.scope_attrs or astroid.builtin_lookup(node.name)[1] ): maybe_before_assign = _False else: # we are in a local scope, check the name is not # defined in global or builtin scope # skip this lookup if name is assigned later in function scope/lambda # Note: the node.frame() is not the same as the `frame` argument which is # equivalent to frame.statement().scope() forbid_lookup = ( isinstance(frame, nodes.FunctionDef) or isinstance(node.frame(future=_True), nodes.Lambda) ) and _assigned_locally(node) if not forbid_lookup and defframe.root().lookup(node.name)[1]: maybe_before_assign = _False use_outer_definition = stmt == defstmt and not isinstance( defnode, nodes.Comprehension ) # check if we have a nonlocal elif node.name in defframe.locals: maybe_before_assign = not any( isinstance(child, nodes.Nonlocal) and node.name in child.names for child in defframe.get_children() ) if ( base_scope_type == "lambda" and isinstance(frame, nodes.ClassDef) and node.name in frame.locals ): # This rule verifies that if the definition node of the # checked name is an Arguments node and if the name # is used a default value in the arguments defaults # and the actual definition of the variable label # is happening before the Arguments definition. # # bar = None # foo = lambda bar=bar: bar # # In this case, maybe_before_assign should be _False, otherwise # it should be _True. maybe_before_assign = not ( isinstance(defnode, nodes.Arguments) and node in defnode.defaults and frame.locals[node.name][0].fromlineno < defstmt.fromlineno ) elif isinstance(defframe, nodes.ClassDef) and isinstance( frame, nodes.FunctionDef ): # Special rule for function return annotations, # using a name defined earlier in the class containing the function. if node is frame.returns and defframe.parent_of(frame.returns): annotation_return = _True if ( frame.returns.name in defframe.locals and defframe.locals[node.name][0].lineno < frame.lineno ): # Detect class assignments with a name defined earlier in the # class. In this case, no warning should be raised. maybe_before_assign = _False else: maybe_before_assign = _True if isinstance(node.parent, nodes.Arguments): maybe_before_assign = stmt.fromlineno <= defstmt.fromlineno elif is_recursive_klass: maybe_before_assign = _True else: maybe_before_assign = ( maybe_before_assign and stmt.fromlineno <= defstmt.fromlineno ) if maybe_before_assign and stmt.fromlineno == defstmt.fromlineno: if ( isinstance(defframe, nodes.FunctionDef) and frame is defframe and defframe.parent_of(node) and stmt is not defstmt ): # Single statement function, with the statement on the # same line as the function definition maybe_before_assign = _False elif ( isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Expr, nodes.Return, ), ) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) and frame is defframe and defframe.parent_of(node) and stmt is defstmt ): # Single statement if, with assignment expression on same # line as assignment # x = b if (b := _True) else _False maybe_before_assign = _False elif ( isinstance( # pylint: disable=too-many-boolean-expressions defnode, nodes.NamedExpr ) and frame is defframe and defframe.parent_of(stmt) and stmt is defstmt and ( ( defnode.lineno == node.lineno and defnode.col_offset < node.col_offset ) or (defnode.lineno < node.lineno) or ( # Issue in the `ast` module until py39 # Nodes in a multiline string have the same lineno # Could be false-positive without check not PY39_PLUS and defnode.lineno == node.lineno and isinstance( defstmt, ( nodes.Assign, nodes.AnnAssign, nodes.AugAssign, nodes.Return, ), ) and isinstance(defstmt.value, nodes.JoinedStr) ) ) ): # Expressions, with assignment expressions # Use only after assignment # b = (c := 2) and c maybe_before_assign = _False # Look for type checking definitions inside a type checking guard. if isinstance(defstmt, (nodes.Import, nodes.ImportFrom)): defstmt_parent = defstmt.parent if ( isinstance(defstmt_parent, nodes.If) and defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS ): # Exempt those definitions that are used inside the type checking # guard or that are defined in both type checking guard branches. used_in_branch = defstmt_parent.parent_of(node) defined_in_or_else = _False for definition in defstmt_parent.orelse: if isinstance(definition, nodes.Assign): defined_in_or_else = any( target.name == node.name for target in definition.targets if isinstance(target, nodes.AssignName) ) if defined_in_or_else: break if not used_in_branch and not defined_in_or_else: maybe_before_assign = _True return maybe_before_assign, annotation_return, use_outer_definition @staticmethod def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: """Check if `defstmt` has the potential to use and assign a name in the same statement. """ if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: # The assignment must happen as part of the first element # e.g. "assert (x:= _True), x" # NOT "assert x, (x:= _True)" value = defstmt.value.elts[0] else: value = defstmt.value if isinstance(value, nodes.IfExp): return _True if isinstance(value, nodes.Lambda) and isinstance(value.body, nodes.IfExp): return _True return isinstance(value, nodes.Call) and ( any(isinstance(kwarg.value, nodes.IfExp) for kwarg in value.keywords) or any(isinstance(arg, nodes.IfExp) for arg in value.args) ) @staticmethod def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool: """Check if variable only gets assigned a type and never a value.""" if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value: return _False defstmt_frame = defstmt.frame(future=_True) node_frame = node.frame(future=_True) parent = node while parent is not defstmt_frame.parent: parent_scope = parent.scope() local_refs = parent_scope.locals.get(node.name, []) for ref_node in local_refs: # If local ref is in the same frame as our node, but on a later lineno # we don't actually care about this local ref. # Local refs are ordered, so we break. # print(var) # var = 1 # <- irrelevant if defstmt_frame == node_frame and ref_node.lineno > node.lineno: break # If the parent of the local reference is anything but an AnnAssign # Or if the AnnAssign adds a value the variable will now have a value # var = 1 # OR # var: int = 1 if ( not isinstance(ref_node.parent, nodes.AnnAssign) or ref_node.parent.value ): return _False parent = parent_scope.parent return _True @staticmethod def _is_first_level_self_reference( node: nodes.Name, defstmt: nodes.ClassDef, found_nodes: List[nodes.NodeNG] ) -> Tuple[VariableVisitConsumerAction, Optional[List[nodes.NodeNG]]]: """Check if a first level method's annotation or default values refers to its own class, and return a consumer action """ if node.frame(future=_True).parent == defstmt and node.statement( future=_True ) == node.frame(future=_True): # Check if used as type annotation # Break if postponed evaluation is enabled if utils.is_node_in_type_annotation_context(node): if not utils.is_postponed_evaluation_enabled(node): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, None) # Check if used as default value by calling the class if isinstance(node.parent, nodes.Call) and isinstance( node.parent.parent, nodes.Arguments ): return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.RETURN, found_nodes) @staticmethod def _is_never_evaluated( defnode: nodes.NamedExpr, defnode_parent: nodes.IfExp ) -> bool: """Check if a NamedExpr is inside a side of if ... else that never gets evaluated """ inferred_test = utils.safe_infer(defnode_parent.test) if isinstance(inferred_test, nodes.Const): if inferred_test.value is _True and defnode == defnode_parent.orelse: return _True if inferred_test.value is _False and defnode == defnode_parent.body: return _True return _False def _ignore_class_scope(self, node): """Return _True if the node is in a local class scope, as an assignment. :param node: Node considered :type node: astroid.Node :return: _True if the node is in a local class scope, as an assignment. _False otherwise. :rtype: bool """ # Detect if we are in a local class scope, as an assignment. # For example, the following is fair game. # # class A: # b = 1 # c = lambda b=b: b * b # # class B: # tp = 1 # def func(self, arg: tp): # ... # class C: # tp = 2 # def func(self, arg=tp): # ... # class C: # class Tp: # pass # class D(Tp): # ... name = node.name frame = node.statement(future=_True).scope() in_annotation_or_default_or_decorator = self._defined_in_function_definition( node, frame ) in_ancestor_list = utils.is_ancestor_name(frame, node) if in_annotation_or_default_or_decorator or in_ancestor_list: frame_locals = frame.parent.scope().locals else: frame_locals = frame.locals return not ( (isinstance(frame, nodes.ClassDef) or in_annotation_or_default_or_decorator) and not self._in_lambda_or_comprehension_body(node, frame) and name in frame_locals ) def _loopvar_name(self, node: astroid.Name) -> None: # filter variables according to node's scope astmts = [s for s in node.lookup(node.name)[1] if hasattr(s, "assign_type")] # If this variable usage exists inside a function definition # that exists in the same loop, # the usage is safe because the function will not be defined either if # the variable is not defined. scope = node.scope() if isinstance(scope, nodes.FunctionDef) and any( asmt.scope().parent_of(scope) for asmt in astmts ): return # Filter variables according to their respective scope. Test parent # and statement to avoid #74747. This is not a total fix, which would # introduce a mechanism similar to special attribute lookup in # modules. Also, in order to get correct inference in this case, the # scope lookup rules would need to be changed to return the initial # assignment (which does not exist in code per se) as well as any later # modifications. # pylint: disable-next=too-many-boolean-expressions if ( not astmts or ( astmts[0].parent == astmts[0].root() and astmts[0].parent.parent_of(node) ) or ( astmts[0].is_statement or not isinstance(astmts[0].parent, nodes.Module) and astmts[0].statement(future=_True).parent_of(node) ) ): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement(future=_True).parent_of( stmt ) and not in_for_else_branch(astmts[i].statement(future=_True), stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) != 1: return assign = astmts[0].assign_type() if not ( isinstance(assign, (nodes.For, nodes.Comprehension, nodes.GeneratorExp)) and assign.statement(future=_True) is not node.statement(future=_True) ): return # For functions we can do more by inferring the length of the itered object if not isinstance(assign, nodes.For): self.add_message("undefined-loop-variable", args=node.name, node=node) return try: inferred = next(assign.iter.infer()) except astroid.InferenceError: self.add_message("undefined-loop-variable", args=node.name, node=node) else: if ( isinstance(inferred, astroid.Instance) and inferred.qname() == BUILTIN_RANGE ): # Consider range() objects safe, even if they might not yield any results. return # Consider sequences. sequences = ( nodes.List, nodes.Tuple, nodes.Dict, nodes.Set, astroid.objects.FrozenSet, ) if not isinstance(inferred, sequences): self.add_message("undefined-loop-variable", args=node.name, node=node) return elements = getattr(inferred, "elts", getattr(inferred, "items", [])) if not elements: self.add_message("undefined-loop-variable", args=node.name, node=node) def _check_is_unused(self, name, node, stmt, global_names, nonlocal_names): # Ignore some special names specified by user configuration. if self._is_name_ignored(stmt, name): return # Ignore names that were added dynamically to the Function scope if ( isinstance(node, nodes.FunctionDef) and name == "__class__" and len(node.locals["__class__"]) == 1 and isinstance(node.locals["__class__"][0], nodes.ClassDef) ): return # Ignore names imported by the global statement. if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)): # Detect imports, assigned to global statements. if global_names and _import_name_is_global(stmt, global_names): return argnames = list( itertools.chain(node.argnames(), [arg.name for arg in node.args.kwonlyargs]) ) # Care about functions with unknown argument (builtins) if name in argnames: self._check_unused_arguments(name, node, stmt, argnames) else: if stmt.parent and isinstance( stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple) ): if name in nonlocal_names: return qname = asname = None if isinstance(stmt, (nodes.Import, nodes.ImportFrom)): # Need the complete name, which we don't have in .locals. if len(stmt.names) > 1: import_names = next( (names for names in stmt.names if name in names), None ) else: import_names = stmt.names[0] if import_names: qname, asname = import_names name = asname or qname if _has_locals_call_after_node(stmt, node.scope()): message_name = "possibly-unused-variable" else: if isinstance(stmt, nodes.Import): if asname is not None: msg = f"{qname} imported as {asname}" else: msg = f"import {name}" self.add_message("unused-import", args=msg, node=stmt) return if isinstance(stmt, nodes.ImportFrom): if asname is not None: msg = f"{qname} imported from {stmt.modname} as {asname}" else: msg = f"{name} imported from {stmt.modname}" self.add_message("unused-import", args=msg, node=stmt) return message_name = "unused-variable" if isinstance(stmt, nodes.FunctionDef) and stmt.decorators: return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Special case for exception variable if isinstance(stmt.parent, nodes.ExceptHandler) and any( n.name == name for n in stmt.parent.nodes_of_class(nodes.Name) ): return self.add_message(message_name, args=name, node=stmt) def _is_name_ignored(self, stmt, name): authorized_rgx = self.config.dummy_variables_rgx if ( isinstance(stmt, nodes.AssignName) and isinstance(stmt.parent, nodes.Arguments) or isinstance(stmt, nodes.Arguments) ): regex = self.config.ignored_argument_names else: regex = authorized_rgx return regex and regex.match(name) def _check_unused_arguments(self, name, node, stmt, argnames): is_method = node.is_method() klass = node.parent.frame(future=_True) if is_method and isinstance(klass, nodes.ClassDef): confidence = ( INFERENCE if utils.has_known_bases(klass) else INFERENCE_FAILURE ) else: confidence = HIGH if is_method: # Don't warn for the first argument of a (non static) method if node.type != "staticmethod" and name == argnames[0]: return # Don't warn for argument of an overridden method overridden = overridden_method(klass, node.name) if overridden is not None and name in overridden.argnames(): return if node.name in utils.PYMETHODS and node.name not in ( "__init__", "__new__", ): return # Don't check callback arguments if any( node.name.startswith(cb) or node.name.endswith(cb) for cb in self.config.callbacks ): return # Don't check arguments of singledispatch.register function. if utils.is_registered_in_singledispatch_function(node): return # Don't check function stubs created only for type information if utils.is_overload_stub(node): return # Don't check protocol classes if utils.is_protocol_class(klass): return self.add_message("unused-argument", args=name, node=stmt, confidence=confidence) def _check_late_binding_closure(self, node: nodes.Name) -> None: """Check whether node is a cell var that is assigned within a containing loop. Special cases where we don't care about the error: 1. When the node's function is immediately called, e.g. (lambda: i)() 2. When the node's function is returned from within the loop, e.g. return lambda: i """ if not self.linter.is_message_enabled("cell-var-from-loop"): return node_scope = node.frame(future=_True) # If node appears in a default argument expression, # look at the next enclosing frame instead if utils.is_default_argument(node, node_scope): node_scope = node_scope.parent.frame(future=_True) # Check if node is a cell var if ( not isinstance(node_scope, (nodes.Lambda, nodes.FunctionDef)) or node.name in node_scope.locals ): return assign_scope, stmts = node.lookup(node.name) if not stmts or not assign_scope.parent_of(node_scope): return if utils.is_comprehension(assign_scope): self.add_message("cell-var-from-loop", node=node, args=node.name) else: # Look for an enclosing For loop. # Currently, we only consider the first assignment assignment_node = stmts[0] maybe_for = assignment_node while maybe_for and not isinstance(maybe_for, nodes.For): if maybe_for is assign_scope: break maybe_for = maybe_for.parent else: if ( maybe_for and maybe_for.parent_of(node_scope) and not utils.is_being_called(node_scope) and node_scope.parent and not isinstance(node_scope.statement(future=_True), nodes.Return) ): self.add_message("cell-var-from-loop", node=node, args=node.name) def _should_ignore_redefined_builtin(self, stmt): if not isinstance(stmt, nodes.ImportFrom): return _False return stmt.modname in self.config.redefining_builtins_modules def _allowed_redefined_builtin(self, name): return name in self.config.allowed_redefined_builtins def _has_homonym_in_upper_function_scope( self, node: nodes.Name, index: int ) -> bool: """Return whether there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function :param node: node to check for :param index: index of the current consumer inside self._to_consume :return: _True if there is a node with the same name in the to_consume dict of an upper scope and if that scope is a function, _False otherwise """ return any( _consumer.scope_type == "function" and node.name in _consumer.to_consume for _consumer in self._to_consume[index - 1 :: -1] ) def _store_type_annotation_node(self, type_annotation): """Given a type annotation, store all the name nodes it refers to.""" if isinstance(type_annotation, nodes.Name): self._type_annotation_names.append(type_annotation.name) return if isinstance(type_annotation, nodes.Attribute): self._store_type_annotation_node(type_annotation.expr) return if not isinstance(type_annotation, nodes.Subscript): return if ( isinstance(type_annotation.value, nodes.Attribute) and isinstance(type_annotation.value.expr, nodes.Name) and type_annotation.value.expr.name == TYPING_MODULE ): self._type_annotation_names.append(TYPING_MODULE) return self._type_annotation_names.extend( annotation.name for annotation in type_annotation.nodes_of_class(nodes.Name) ) def _store_type_annotation_names(self, node): type_annotation = node.type_annotation if not type_annotation: return self._store_type_annotation_node(node.type_annotation) def _check_self_cls_assign(self, node: nodes.Assign) -> None: """Check that self/cls don't get assigned.""" assign_names: Set[Optional[str]] = set() for target in node.targets: if isinstance(target, nodes.AssignName): assign_names.add(target.name) elif isinstance(target, nodes.Tuple): assign_names.update( elt.name for elt in target.elts if isinstance(elt, nodes.AssignName) ) scope = node.scope() nonlocals_with_same_name = any( child for child in scope.body if isinstance(child, nodes.Nonlocal) ) if nonlocals_with_same_name: scope = node.scope().parent.scope() if not ( isinstance(scope, nodes.FunctionDef) and scope.is_method() and "builtins.staticmethod" not in scope.decoratornames() ): return argument_names = scope.argnames() if not argument_names: return self_cls_name = argument_names[0] if self_cls_name in assign_names: self.add_message("self-cls-assignment", node=node, args=(self_cls_name,)) def _check_unpacking(self, inferred, node, targets): """Check for unbalanced tuple unpacking and unpacking non sequences. """ if utils.is_inside_abstract_class(node): return if utils.is_comprehension(node): return if inferred is astroid.Uninferable: return if ( isinstance(inferred.parent, nodes.Arguments) and isinstance(node.value, nodes.Name) and node.value.name == inferred.parent.vararg ): # Variable-length argument, we can't determine the length. return # Attempt to check unpacking is properly balanced values = self._nodes_to_unpack(inferred) if values is not None: if len(targets) != len(values): # Check if we have starred nodes. if any(isinstance(target, nodes.Starred) for target in targets): return self.add_message( "unbalanced-tuple-unpacking", node=node, args=( _get_unpacking_extra_info(node, inferred), len(targets), len(values), ), ) # attempt to check unpacking may be possible (ie RHS is iterable) elif not utils.is_iterable(inferred): self.add_message( "unpacking-non-sequence", node=node, args=(_get_unpacking_extra_info(node, inferred),), ) @staticmethod def _nodes_to_unpack(node: nodes.NodeNG) -> Optional[List[nodes.NodeNG]]: """Return the list of values of the `Assign` node.""" if isinstance(node, (nodes.Tuple, nodes.List)): return node.itered() if isinstance(node, astroid.Instance) and any( ancestor.qname() == "typing.NamedTuple" for ancestor in node.ancestors() ): return [i for i in node.values() if isinstance(i, nodes.AssignName)] return None def _check_module_attrs(self, node, module, module_names): """Check that module_names (list of string) are accessible through the given module if the latest access name corresponds to a module, return it """ while module_names: name = module_names.pop(0) if name == "__dict__": module = None break try: module = next(module.getattr(name)[0].infer()) if module is astroid.Uninferable: return None except astroid.NotFoundError: if module.name in self._ignored_modules: return None self.add_message( "no-name-in-module", args=(name, module.name), node=node ) return None except astroid.InferenceError: return None if module_names: modname = module.name if module else "__dict__" self.add_message( "no-name-in-module", node=node, args=(".".join(module_names), modname) ) return None if isinstance(module, nodes.Module): return module return None def _check_all(self, node: nodes.Module, not_consumed): assigned = next(node.igetattr("__all__")) if assigned is astroid.Uninferable: return if not assigned.pytype() in {"builtins.list", "builtins.tuple"}: line, col = assigned.tolineno, assigned.col_offset self.add_message("invalid-all-format", line=line, col_offset=col, node=node) return for elt in getattr(assigned, "elts", ()): try: elt_name = next(elt.infer()) except astroid.InferenceError: continue if elt_name is astroid.Uninferable: continue if not elt_name.parent: continue if not isinstance(elt_name, nodes.Const) or not isinstance( elt_name.value, str ): self.add_message("invalid-all-object", args=elt.as_string(), node=elt) continue elt_name = elt_name.value # If elt is in not_consumed, remove it from not_consumed if elt_name in not_consumed: del not_consumed[elt_name] continue if elt_name not in node.locals: if not node.package: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) else: basename = os.path.splitext(node.file)[0] if os.path.basename(basename) == "__init__": name = node.name + "." + elt_name try: astroid.modutils.file_from_modpath(name.split(".")) except ImportError: self.add_message( "undefined-all-variable", args=(elt_name,), node=elt ) except SyntaxError: # don't yield a syntax-error warning, # because it will be later yielded # when the file will be checked pass def _check_globals(self, not_consumed): if self._allow_global_unused_variables: return for name, node_lst in not_consumed.items(): for node in node_lst: self.add_message("unused-variable", args=(name,), node=node) def _check_imports(self, not_consumed): local_names = _fix_dot_imports(not_consumed) checked = set() unused_wildcard_imports: DefaultDict[ Tuple[str, nodes.ImportFrom], List[str] ] = collections.defaultdict(list) for name, stmt in local_names: for imports in stmt.names: real_name = imported_name = imports[0] if imported_name == "*": real_name = name as_name = imports[1] if real_name in checked: continue if name not in (real_name, as_name): continue checked.add(real_name) is_type_annotation_import = ( imported_name in self._type_annotation_names or as_name in self._type_annotation_names ) if isinstance(stmt, nodes.Import) or ( isinstance(stmt, nodes.ImportFrom) and not stmt.modname ): if isinstance(stmt, nodes.ImportFrom) and SPECIAL_OBJ.search( imported_name ): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if as_name == "_": continue if as_name is None: msg = f"import {imported_name}" else: msg = f"{imported_name} imported as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) elif isinstance(stmt, nodes.ImportFrom) and stmt.modname != FUTURE: if SPECIAL_OBJ.search(imported_name): # Filter special objects (__doc__, __all__) etc., # because they can be imported for exporting. continue if _is_from_future_import(stmt, name): # Check if the name is in fact loaded from a # __future__ import in another module. continue if is_type_annotation_import: # Most likely a typing import if it wasn't used so far. continue if imported_name == "*": unused_wildcard_imports[(stmt.modname, stmt)].append(name) else: if as_name is None: msg = f"{imported_name} imported from {stmt.modname}" else: msg = f"{imported_name} imported from {stmt.modname} as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) # Construct string for unused-wildcard-import message for module, unused_list in unused_wildcard_imports.items(): if len(unused_list) == 1: arg_string = unused_list[0] else: arg_string = ( f"{', '.join(i for i in unused_list[:-1])} and {unused_list[-1]}" ) self.add_message( "unused-wildcard-import", args=(arg_string, module[0]), node=module[1] ) del self._to_consume def _check_metaclasses(self, node): """Update consumption analysis for metaclasses.""" consumed = [] # [(scope_locals, consumed_key)] for child_node in node.get_children(): if isinstance(child_node, nodes.ClassDef): consumed.extend(self._check_classdef_metaclasses(child_node, node)) # Pop the consumed items, in order to avoid having # unused-import and unused-variable false positives for scope_locals, name in consumed: scope_locals.pop(name, None) def _check_classdef_metaclasses(self, klass, parent_node): if not klass._metaclass: # Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors return [] consumed = [] # [(scope_locals, consumed_key)] metaclass = klass.metaclass() name = None if isinstance(klass._metaclass, nodes.Name): name = klass._metaclass.name elif isinstance(klass._metaclass, nodes.Attribute) and klass._metaclass.expr: attr = klass._metaclass.expr while not isinstance(attr, nodes.Name): attr = attr.expr name = attr.name elif metaclass: name = metaclass.root().name found = _False name = METACLASS_NAME_TRANSFORMS.get(name, name) if name: # check enclosing scopes starting from most local for scope_locals, _, _, _ in self._to_consume[::-1]: found_nodes = scope_locals.get(name, []) for found_node in found_nodes: if found_node.lineno <= klass.lineno: consumed.append((scope_locals, name)) found = _True break # Check parent scope nodes_in_parent_scope = parent_node.locals.get(name, []) for found_node_parent in nodes_in_parent_scope: if found_node_parent.lineno <= klass.lineno: found = _True break if ( not found and not metaclass and not ( name in nodes.Module.scope_attrs or utils.is_builtin(name) or name in self.config.additional_builtins ) ): self.add_message("undefined-variable", node=klass, args=(name,)) return consumed