Skip to content

Hook for preprocessing test module AST prior to rewriting #3465

Closed
@Sup3rGeo

Description

@Sup3rGeo

Hi,

We are building a nice reporting framework around pytest and we would like to extend the approach taken when rewriting assertions to whatever other things we find interesting - basically providing more information without needing to change the source code.

For this I would propose a new hook called pytest_ast_preprocess(tree, fn, config). It basically has the same signature as assertion.rewrite.rewrite_asserts function, and is meant to delegate this AST processing to a plugin.

It is called preprocess because it is supposed to act before the rewrite_asserts function itself:

try:
    tree = ast.parse(source)
except SyntaxError:
    # Let this pop up again in the real import.
    state.trace("failed to parse: %r" % (fn,))
    return None, None
hook.pytest_ast_preprocess(tree,fn,config)   # not sure proper way to call hook
rewrite_asserts(tree, fn, config)
try:
    co = compile(tree, fn.strpath, "exec", dont_inherit=True)
except SyntaxError:
    # It's possible that this error is from some bug in the
    # assertion rewriting, but I don't know of a fast way to tell.
    state.trace("failed to compile: %r" % (fn,))
    return None, None

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions