-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Allow fixing snapshots without disabling pytest rewriting #57
Comments
This would mean that your snapshots are always fixed and your tests would always pass. I don't think that this is what you mean. I think that I understand you wrong here and that the problem is that you found some incorrect snapshots when you run If this is the case then #48 might be helpful, because it would allow you to run
The problem is that pytest changes the location of all sub-expressions in the assertion to the source range of the whole assertion. We would need to change this in pytest if executing should work for expressions in assert statements. There might still be some problems for ast nodes which are generated by pytest to instrument the source, but the general idea should work.
There are probably a lot of corner cases, because I can not promise 100% that everything will work, but there is a path.
Definitely. This things are connected. |
That's 100% what I mean. I'm sure that changes to snapshots would not go unnoticed in the git diffs. And this would of course be disabled in CI. Personally the way I use inline-snapshot at the moment is that I have a 'run configuration' for every test file that contains snapshots, and the configuration sets
I wonder why I seem to remember PositionNodeFinder working inside pytest assertions. Looking now I can see that it doesn't. Did something change, or am I just completely misremembering?
It wouldn't always work, but it usually would. My thinking is that pytest rewriting would still be disabled by default but that this would be configurable. So there'd be a way to
Of course I would love this for |
The problem here is that I'm not so sure in the general case (for some random user at some time of the day (I know that I make a lot more mistakes when I'm tired 😄 .)). I have nothing against your approach of making
I don't know, it is also too long ago for me.
The problem for inline-snapshot is that I would have to explain and document the cases where it does not work. It is not the best user experience when the user has to change his code almost randomly to make it work. The way which I would prefer, is to change the code in pytest which currently changes the node locations. This should allow executing to find the nodes for 3.11+. This is also easy to explain for the user (feature x is only available for 3.11+). |
The docs would be something like:
Anyway, I agree that if you can make it just work in 3.11+ with pytest, that'd be good too. |
Yesterday a teammate asked if
--inline-snapshot=fix
could be enabled by default. I also think I'd quite like this. But it seems that the main problem is that doing so disables pytest rewriting, making assertions worse even in tests that aren't using snapshots.While I understand that pytest rewriting interferes with
executing
, it seems there are still two ways that fixing snapshots can work in common cases without disabling rewriting:executing
still works even inside pytest assertions, right?executing
can't find the node, try (approximately, I'm simplifying and not checking this code)statement = only(ex.stmts); node = only(child for child in ast.walk(statement) if isinstance(...) and child.func.id == 'snapshot')
or maybe something likeassert isinstance(statement, ast.Assert); node = statement.value.operands[1])
.Either of these would suffice for us 100% of the time. All our uses of
snapshot
are of the formassert ... == snapshot(...)
, and although we support Python 3.8 we develop in Python 3.12.It also looks like working without rewriting might be helpful for #41.
The text was updated successfully, but these errors were encountered: