Simplify error behaviour of transaction_if_not_already#156
Merged
Conversation
The targets of these comments were not previously clear. Co-authored-by: Lily <code@lilyf.org>
Before this change, `transaction_if_not_already` repeated the call to `_execute_on_commit_callbacks_in_tests` which could be found in `transaction`. This now delegates transaction-closing emulation to `transaction`. Co-authored-by: Lily <code@lilyf.org>
Before this change, errors raised through `transaction_if_not_already` would invalidate the state of any outer transaction (other than a test's transaction). This was a side-effect of the fact that we used `atomic(savepoint=False)` in that case. By using `contextlib.nullcontext` when a transaction is already open, we avoid that pitfall. Co-authored-by: Lily <code@lilyf.org>
LilyFirefly
reviewed
May 1, 2026
This is functionally the same, but avoids a function call. Co-authored-by: Lily <code@lilyf.org>
samueljsb
approved these changes
May 6, 2026
Collaborator
samueljsb
left a comment
There was a problem hiding this comment.
This looks like a sensible bugfix to me.
I don't think this justifies a major version bump: we're not changing the API, nor the expectations about what this function will do. All changes to the code will change behaviour -- that doesn't mean every release needs to bump the major version.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Before this change, errors raised through
transaction_if_not_alreadywould invalidate the state of any outer transaction (other than a test's transaction).That was a side-effect of the fact that we used
atomic(savepoint=False)in that case.This avoids that pitfall by using
contextlib.nullcontextwhen a transaction is already open. The logic oftransaction_if_not_alreadybecomes easier to understand as a result.Because this changes production behaviour, this change will require a 2.0 release.Edit: As per review comments, we will not make a 2.0 release as a result of this change.