def _astroid_module_checker(self): """Context manager for checking ASTs The value in the context is callable accepting AST as its only argument. """ walker = ASTWalker(self) _checkers = self.prepare_checkers() tokencheckers = [ c for c in _checkers if interfaces.implements(c, interfaces.ITokenChecker) and c is not self ] rawcheckers = [ c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker) ] # notify global begin for checker in _checkers: checker.open() if interfaces.implements(checker, interfaces.IAstroidChecker): walker.add_checker(checker) yield functools.partial( self.check_astroid_module, walker=walker, tokencheckers=tokencheckers, rawcheckers=rawcheckers, ) # notify global end self.stats["statement"] = walker.nbstatements for checker in reversed(_checkers): checker.close()
def check(self, filename, file_content): # initialize msgs_state now that all messages have been registered into # the store for msg in self.msgs_store.messages: if not msg.may_be_emitted(): self._msgs_state[msg.msgid] = False basename = (os.path.splitext(os.path.basename(filename))[0] if filename else "untitled") walker = utils.PyLintASTWalker(self) self.config.reports = True _checkers = self.prepare_checkers() tokencheckers = [ c for c in _checkers if interfaces.implements(c, interfaces.ITokenChecker) and c is not self ] rawcheckers = [ c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker) ] # notify global begin for checker in _checkers: checker.open() if interfaces.implements(checker, interfaces.IAstroidChecker): walker.add_checker(checker) self.set_current_module(basename, filename) ast_node = bd.string_build(file_content, filename, basename) self.file_state = utils.FileState(basename) self._ignore_file = False # fix the current file (if the source file was not available or # if it's actually a c extension) self.current_file = ast_node.file # pylint: disable=maybe-no-member self.check_astroid_module(ast_node, walker, rawcheckers, tokencheckers) # warn about spurious inline messages handling spurious_messages = self.file_state.iter_spurious_suppression_messages( self.msgs_store) for msgid, line, args in spurious_messages: self.add_message(msgid, line, None, args) # notify global end self.stats["statement"] = walker.nbstatements for checker in reversed(_checkers): checker.close()
def build_message_def(checker, msgid, msg_tuple): if implements(checker, (IRawChecker, ITokenChecker)): default_scope = WarningScope.LINE else: default_scope = WarningScope.NODE options = {} if len(msg_tuple) > 3: (msg, symbol, descr, options) = msg_tuple elif len(msg_tuple) > 2: (msg, symbol, descr) = msg_tuple else: # messages should have a symbol, but for backward compatibility # they may not. (msg, descr) = msg_tuple warnings.warn("[pylint 0.26] description of message %s doesn't include " "a symbolic name" % msgid, DeprecationWarning) symbol = None options.setdefault('scope', default_scope) return MessageDefinition(checker, msgid, msg, descr, symbol, **options)
def create_message_definition_from_tuple(self, msgid, msg_tuple): if implements(self, (IRawChecker, ITokenChecker)): default_scope = WarningScope.LINE else: default_scope = WarningScope.NODE options = {} if len(msg_tuple) > 3: (msg, symbol, descr, options) = msg_tuple elif len(msg_tuple) > 2: (msg, symbol, descr) = msg_tuple else: error_msg = """Messages should have a msgid and a symbol. Something like this : "W1234": ( "message", "message-symbol", "Message description with detail.", ... ), """ raise InvalidMessageError(error_msg) options.setdefault("scope", default_scope) return MessageDefinition(self, msgid, msg, descr, symbol, **options)
def create_message_definition_from_tuple( self, msgid: str, msg_tuple: MessageDefinitionTuple) -> MessageDefinition: with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=DeprecationWarning) if isinstance(self, (BaseTokenChecker, BaseRawFileChecker)): default_scope = WarningScope.LINE # TODO: 3.0: Remove deprecated if-statement elif implements(self, (IRawChecker, ITokenChecker)): warnings.warn( # pragma: no cover "Checkers should subclass BaseTokenChecker or BaseRawFileChecker " "instead of using the __implements__ mechanism. Use of __implements__ " "will no longer be supported in pylint 3.0", DeprecationWarning, ) default_scope = WarningScope.LINE # pragma: no cover else: default_scope = WarningScope.NODE options: ExtraMessageOptions = {} if len(msg_tuple) == 4: (msg, symbol, descr, options) = msg_tuple # type: ignore[misc] elif len(msg_tuple) == 3: (msg, symbol, descr) = msg_tuple # type: ignore[misc] else: error_msg = """Messages should have a msgid, a symbol and a description. Something like this : "W1234": ( "message", "message-symbol", "Message description with detail.", ... ), """ raise InvalidMessageError(error_msg) options.setdefault("scope", default_scope) return MessageDefinition(self, msgid, msg, descr, symbol, **options)