Skip to content
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

Log a warning for uncommitted changes on transaction exit #266

Merged
merged 2 commits into from
Feb 21, 2024

Conversation

maxmynter
Copy link
Collaborator

Closes #264

Copy link

codecov bot commented Feb 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (f05c5b6) 94.24% compared to head (c599118) 94.32%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #266      +/-   ##
==========================================
+ Coverage   94.24%   94.32%   +0.08%     
==========================================
  Files           5        5              
  Lines         382      388       +6     
  Branches       70       72       +2     
==========================================
+ Hits          360      366       +6     
  Misses         13       13              
  Partials        9        9              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 135 to 138
if any(True for _ in self._ephemeral_branch.uncommitted()):
logger.warning(
f"Transaction closed with uncommitted changes. {'Objects added but not committed are lost.' if self.delete != 'never' else ''}"
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if any(True for _ in self._ephemeral_branch.uncommitted()):
logger.warning(
f"Transaction closed with uncommitted changes. {'Objects added but not committed are lost.' if self.delete != 'never' else ''}"
)
if any(self._ephemeral_branch.uncommitted()):
msg = f"Finished transaction {self._ephemeral_branch.id!r} contains uncommitted changes."
if self.delete != "never":
msg += " Objects added but not committed are lost."
warnings.warn(msg)

Explanation: We always prefer a warning over a log, because that works even when the user does not set up logging. If the user needs logging, he can do import logging; logging.capturewarnings(True), if he wants to catch the warnings, he can do so with a warnings filter context manager or by redirecting stderr.

Bonus: We keep a clean separation between stdout and stderr.

Copy link
Collaborator

@nicholasjng nicholasjng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@maxmynter maxmynter merged commit b139cdb into main Feb 21, 2024
7 checks passed
@maxmynter maxmynter deleted the warning-msg branch February 21, 2024 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warning message when ending a transaction while having uncommited objects
2 participants