class documentation

class NamesConsumer: (source)

View In Hierarchy

A simple class to handle consumed, to consume and scope type info of node locals.

Method __init__ Undocumented
Method __iter__ Undocumented
Method __repr__ Undocumented
Method get_next_to_consume Return a list of the nodes that define `node` from this scope.
Method mark_as_consumed Mark the given nodes as consumed for the name.
Instance Variable node Undocumented
Property consumed Undocumented
Property consumed_uncertain Retrieves nodes filtered out by get_next_to_consume() that may not have executed.
Property scope_type Undocumented
Property to_consume Undocumented
Static Method _branch_handles_name Undocumented
Static Method _check_loop_finishes_via_except Check for a case described in https://github.com/PyCQA/pylint/issues/5683.
Static Method _defines_name_raises_or_returns Undocumented
Static Method _defines_name_raises_or_returns_recursive Return True if some child of `node` defines the name `name`, raises, or returns.
Static Method _exhaustively_define_name_raise_or_return Return True if there is a collectively exhaustive set of paths under this `if_node` that define `name`, raise, or return.
Static Method _recursive_search_for_continue_before_break Return True if any Continue node can be found in descendants of `stmt` before encountering `break_stmt`, ignoring any nested loops.
Static Method _uncertain_nodes_in_except_blocks Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in an except block.
Static Method _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks Return any nodes in ``found_nodes`` that should be treated as uncertain.
Static Method _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks Undocumented
Method _uncertain_nodes_in_false_tests Identify nodes of uncertain execution because they are defined under tests that evaluate false.
Instance Variable _atomic Undocumented
Instance Variable _if_nodes_deemed_uncertain Undocumented
def __init__(self, node: nodes.NodeNG, scope_type: str): (source)

Undocumented

def __iter__(self) -> Iterator[Any]: (source)

Undocumented

def __repr__(self) -> str: (source)

Undocumented

def get_next_to_consume(self, node: nodes.Name) -> list[nodes.NodeNG]|None: (source)

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.

def mark_as_consumed(self, name: str, consumed_nodes: list[nodes.NodeNG]): (source)

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

Undocumented

Retrieves nodes filtered out by get_next_to_consume() that may not have executed. These include nodes 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.

Undocumented

Undocumented

@staticmethod
def _branch_handles_name(name: str, body: Iterable[nodes.NodeNG]) -> bool: (source)

Undocumented

@staticmethod
def _check_loop_finishes_via_except(node: nodes.NodeNG, other_node_try_except: nodes.TryExcept) -> bool: (source)

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)

@staticmethod
def _defines_name_raises_or_returns(name: str, node: nodes.NodeNG) -> bool: (source)

Undocumented

@staticmethod
def _defines_name_raises_or_returns_recursive(name: str, node: nodes.NodeNG) -> bool: (source)

Return True if some child of `node` defines the name `name`, raises, or returns.

@staticmethod
def _exhaustively_define_name_raise_or_return(name: str, node: nodes.NodeNG) -> bool: (source)

Return True if there is a collectively exhaustive set of paths under this `if_node` that define `name`, raise, or return.

@staticmethod
def _recursive_search_for_continue_before_break(stmt: nodes.Statement, break_stmt: nodes.Break) -> bool: (source)

Return True if any Continue node can be found in descendants of `stmt` before encountering `break_stmt`, ignoring any nested loops.

@staticmethod
def _uncertain_nodes_in_except_blocks(found_nodes: list[nodes.NodeNG], node: nodes.NodeNG, node_statement: nodes.Statement) -> list[nodes.NodeNG]: (source)

Return any nodes in ``found_nodes`` that should be treated as uncertain because they are in an except block.

@staticmethod
def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks(found_nodes: list[nodes.NodeNG], node_statement: nodes.Statement) -> list[nodes.NodeNG]: (source)

Return any nodes in ``found_nodes`` that should be treated as uncertain. Nodes are uncertain when they are in a try block and the ``node_statement`` being evaluated is in one of its except handlers.

@staticmethod
def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks(found_nodes: list[nodes.NodeNG], node_statement: nodes.Statement) -> list[nodes.NodeNG]: (source)

Undocumented

def _uncertain_nodes_in_false_tests(self, found_nodes: list[nodes.NodeNG], node: nodes.NodeNG) -> list[nodes.NodeNG]: (source)

Identify nodes of uncertain execution because they are defined under tests that evaluate false. Don't identify a node if there is a collectively exhaustive set of paths that define the name, raise, or return (e.g. every if/else branch).

Undocumented

_if_nodes_deemed_uncertain: set[nodes.If] = (source)

Undocumented