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 walk(self, node): """recursive walk on the given node""" walker = ASTWalker(self.linter) if self.enable_plugin: walker.add_checker(self.checker) for checker in self.impacted_checkers: walker.add_checker(checker) walker.walk(node)
def test_check_messages(self): linter = self.MockLinter( {"first-message": True, "second-message": False, "third-message": True} ) walker = ASTWalker(linter) checker = self.Checker() walker.add_checker(checker) walker.walk(astroid.parse("x = func()")) assert {"module", "assignname"} == checker.called
def test_only_required_for_messages(self) -> None: linter = self.MockLinter( {"first-message": True, "second-message": False, "third-message": True} ) walker = ASTWalker(linter) # type: ignore[arg-type] checker = self.Checker() walker.add_checker(checker) walker.walk(astroid.parse("x = func()")) assert {"module", "assignname"} == checker.called
def test_deprecated_methods(self): class Checker(object): def __init__(self): self.called = False @check_messages("first-message") def visit_assname(self, node): self.called = True linter = self.MockLinter({"first-message": True}) walker = ASTWalker(linter) checker = Checker() walker.add_checker(checker) with warnings.catch_warnings(record=True): warnings.simplefilter("always") walker.walk(astroid.parse("x = 1")) assert not checker.called
def test_deprecated_methods(self) -> None: class Checker(BaseChecker): # pylint: disable-next=super-init-not-called def __init__(self) -> None: self.called = False @only_required_for_messages("first-message") def visit_assname(self, node): # pylint: disable=unused-argument self.called = True linter = self.MockLinter({"first-message": True}) walker = ASTWalker(linter) # type: ignore[arg-type] checker = Checker() walker.add_checker(checker) with warnings.catch_warnings(record=True): warnings.simplefilter("always") walker.walk(astroid.parse("x = 1")) assert not checker.called
def walk(self, node): """recursive walk on the given node""" walker = ASTWalker(linter) walker.add_checker(self.checker) walker.walk(node)