def should_rewrite(self): """:const:`True` if the source imports :func:`assert_hook`.""" return ('assert_hook' in self.source and any(s.module == 'attest' and any(n.name == 'assert_hook' for n in s.names) for s in ast.parse(self.source).body if isinstance(s, ast.ImportFrom)))
def __init__(self, expr, globals, locals): self.expr = expr # Putting locals in globals for closures self.globals = dict(globals) self.locals = locals self.globals.update(self.locals) self.result = [] self.node = ast.parse(self.expr).body[0].value
def node(self): """The transformed AST node.""" node = ast.parse(self.source, self.filename) node = self.visit(node) ast.fix_missing_locations(node) return node
from attest import ast, statistics from attest.codegen import to_source, SourceGenerator __all__ = ['COMPILES_AST', 'ExpressionEvaluator', 'TestFailure', 'assert_hook', 'AssertTransformer', 'AssertImportHook', ] try: compile(ast.parse('pass'), '<string>', 'exec') except TypeError: COMPILES_AST = False else: COMPILES_AST = True class ExpressionEvaluator(SourceGenerator): """Evaluates ``expr`` in the context of ``globals`` and ``locals``, expanding the values of variables and the results of binary operations, but keeping comparison and boolean operators. .. testsetup:: from attest import ExpressionEvaluator
import sys from attest import ast, statistics from attest.codegen import to_source, SourceGenerator __all__ = [ 'COMPILES_AST', 'ExpressionEvaluator', 'TestFailure', 'assert_hook', 'AssertTransformer', 'AssertImportHook', ] try: compile(ast.parse('pass'), '<string>', 'exec') except TypeError: COMPILES_AST = False else: COMPILES_AST = True class ExpressionEvaluator(SourceGenerator): """Evaluates ``expr`` in the context of ``globals`` and ``locals``, expanding the values of variables and the results of binary operations, but keeping comparison and boolean operators. .. testsetup:: from attest import ExpressionEvaluator