def _set_state_on_block_lines( self, msgs_store: MessageDefinitionStore, node: nodes.NodeNG, msg: MessageDefinition, msg_state: dict[int, bool], ) -> None: """Recursively walk (depth first) AST to collect block level options line numbers and set the state correctly. """ for child in node.get_children(): self._set_state_on_block_lines(msgs_store, child, msg, msg_state) # first child line number used to distinguish between disable # which are the first child of scoped node with those defined later. # For instance in the code below: # # 1. def meth8(self): # 2. """test late disabling""" # 3. pylint: disable=not-callable, useless-suppression # 4. print(self.blip) # 5. pylint: disable=no-member, useless-suppression # 6. print(self.bla) # # E1102 should be disabled from line 1 to 6 while E1101 from line 5 to 6 # # this is necessary to disable locally messages applying to class / # function using their fromlineno if (isinstance(node, (nodes.Module, nodes.ClassDef, nodes.FunctionDef)) and node.body): firstchildlineno = node.body[0].fromlineno else: firstchildlineno = node.tolineno self._set_message_state_in_block(msg, msg_state, node, firstchildlineno)
def _modified_iterating_check_on_node_and_children( self, body_node: nodes.NodeNG, iter_obj: nodes.NodeNG ) -> None: """See if node or any of its children raises modified iterating messages.""" self._modified_iterating_check(body_node, iter_obj) for child in body_node.get_children(): self._modified_iterating_check_on_node_and_children(child, iter_obj)
def walk(self, astroid: nodes.NodeNG) -> None: """Call visit events of astroid checkers for the given node, recurse on its children, then leave events. """ cid = astroid.__class__.__name__.lower() # Detect if the node is a new name for a deprecated alias. # In this case, favour the methods for the deprecated # alias if any, in order to maintain backwards # compatibility. visit_events: Sequence[AstCallback] = self.visit_events.get(cid, ()) leave_events: Sequence[AstCallback] = self.leave_events.get(cid, ()) try: if astroid.is_statement: self.nbstatements += 1 # generate events for this node on each checker for callback in visit_events: callback(astroid) # recurse on children for child in astroid.get_children(): self.walk(child) for callback in leave_events: callback(astroid) except Exception: if self.exception_msg is False: file = getattr(astroid.root(), "file", None) print( f"Exception on node {repr(astroid)} in file '{file}'", file=sys.stderr, ) traceback.print_exc() self.exception_msg = True raise
def _collect_block_lines( self, msgs_store: "MessageDefinitionStore", node: nodes.NodeNG, msg_state: MessageStateDict, ) -> None: """Recursively walk (depth first) AST to collect block level options line numbers. """ for child in node.get_children(): self._collect_block_lines(msgs_store, child, msg_state) first = node.fromlineno last = node.tolineno # first child line number used to distinguish between disable # which are the first child of scoped node with those defined later. # For instance in the code below: # # 1. def meth8(self): # 2. """test late disabling""" # 3. pylint: disable=not-callable, useless-suppression # 4. print(self.blip) # 5. pylint: disable=no-member, useless-suppression # 6. print(self.bla) # # E1102 should be disabled from line 1 to 6 while E1101 from line 5 to 6 # # this is necessary to disable locally messages applying to class / # function using their fromlineno if ( isinstance(node, (nodes.Module, nodes.ClassDef, nodes.FunctionDef)) and node.body ): firstchildlineno = node.body[0].fromlineno else: firstchildlineno = last for msgid, lines in msg_state.items(): for lineno, state in list(lines.items()): original_lineno = lineno if first > lineno or last < lineno: continue # Set state for all lines for this block, if the # warning is applied to nodes. message_definitions = msgs_store.get_message_definitions(msgid) for message_definition in message_definitions: if message_definition.scope == WarningScope.NODE: if lineno > firstchildlineno: state = True first_, last_ = node.block_range(lineno) else: first_ = lineno last_ = last for line in range(first_, last_ + 1): # do not override existing entries if line in self._module_msgs_state.get(msgid, ()): continue if line in lines: # state change in the same block state = lines[line] original_lineno = line if not state: self._suppression_mapping[(msgid, line)] = original_lineno try: self._module_msgs_state[msgid][line] = state except KeyError: self._module_msgs_state[msgid] = {line: state} del lines[lineno]
def default(self, node: nodes.NodeNG, *args: Any) -> None: for child in node.get_children(): self.dispatch(child, *args)