-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Hook for preprocessing test module AST prior to rewriting #3465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
GitMate.io thinks possibly related issues are #2203 (Exclude some test modules from assertion rewriting), #2419 (Rewrite hook attempts to rewrite namespace package), #1978 (pytest not finding any tests under module), #427 (Option to have pytest rewrite assertions in additional non-test modules), and #46 (adding hooks for controlling instances of tests). |
I'm 👍: it is a trivial change to the core and opens possibilities for plugins to do fancy stuff with it. |
this is strictly a massive problem, since this creates a setup where ast caching no longer reliably matches the desired ast im -1 on it unless we present a way to make it consistent per project + prevent inter-project ast rewriting issues (as they can happen for shared editable installs) |
What do you mean @RonnyPfannschmidt ? We don't cache the AST, we only cache the generated code (well after AST has been processed). |
whops - i intended to talk about the cached code objects |
I see, thanks. So the proposal is fine then, right? After all is just an extra hook which can alter the AST just like pytest itself does. |
@nicoddemus the proposal as is is unacceptable as it creates the issues i mention without a solution, just having that hook simply creates a massive liability and there is no chance for that api to ever be usable correctly |
Sorry I fail to see the problem 😕 Currently pytest does roughly this:
This proposal changes the flow to this IIUC:
Can you elaborate what are the problems with the above? |
I guess what @RonnyPfannschmidt could be talking about is that, once the modules are compiled for the first time, the AST rewriting part is actually never called again. I can see this becoming a problem (actually hook users will see this necessarily) if the hook implementation changes in a plugin and user forget to clean Now if I understood well enough, are you talking about running the same test in a session with one AST rewriting plugin, and then in another session another AST rewriting plugin tries to run the same tests and ends up actually not rewriting because of the cached files? |
@Sup3rGeo correct- if the ast rewriters that acted as part of the system are not considered on cached module loading, this will under guarantee create issues that are next to impossible to debug |
Can we detect from pluginmanager that there exists a plugin/conftest that implemented a specific hook? If possible, one idea would be to detect that this hook if implemented by at least one plugin and, if so, just don't read nor save any
|
might be related to #1680 |
If you're interested in a black-magic workaround, here's a pytest test which rewrites itself before executing: https://github.com/asottile/future-fstrings/blob/f06c03e5817aeb9fdccc1b4b6728dec600ce137b/tests/future_fstrings_test.py#L1 EDIT: though even that falls victim to the pyc problem mentioned above: https://github.com/asottile/future-fstrings/blob/f06c03e5817aeb9fdccc1b4b6728dec600ce137b/tox.ini#L9-L10 |
Closing this issue because, while it would be a nice feature, it would also be very difficult to use safely or debug when it goes wrong 😕 |
just a extra note - instead of a hook, its thinkable that we can consider a explicitly listed set of ast rewrite addons, which in turn take part in deciding the filename |
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 asassertion.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:Thanks!
The text was updated successfully, but these errors were encountered: