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

Stop using datetime.utcnow() in tests #1308

Closed

Conversation

s-t-e-v-e-n-k
Copy link

@s-t-e-v-e-n-k s-t-e-v-e-n-k commented Jan 31, 2024

datetime.utcnow() is deprecated for Python 3.12+, and raises a warning. Since warnings are treated as errors, this results in test failures. Since utcnow calls are done by the SQLAlchemy mapping machinery, we need to use a callable.

Fixes #1303

Checklist:

  • Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.
  • Add or update relevant docs, in the docs folder and in code.
  • Add an entry in CHANGES.rst summarizing the change and linking to the issue.
  • Add .. versionchanged:: entries in any relevant code docs.
  • Run pre-commit hooks and fix any issues.
  • Run pytest and tox, no tests failed.

@s-t-e-v-e-n-k s-t-e-v-e-n-k force-pushed the stop-using-utcnow branch 2 times, most recently from 90d6e72 to 172e239 Compare January 31, 2024 05:55
Copy link
Contributor

@pamelafox pamelafox left a comment

Choose a reason for hiding this comment

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

Thanks! As this is a bug fix, could you make it off the latest 3.x branch, per https://github.com/pallets-eco/flask-sqlalchemy/blob/main/CONTRIBUTING.rst#start-coding ?

@@ -14,6 +15,11 @@
from flask_sqlalchemy.model import Model


class UTCNow(datetime):
Copy link
Member

Choose a reason for hiding this comment

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

Rather than making this a subclass with a tricky __new__, this should just be a function:

def now() -> datetime:
    return datetime.now(timezone.utc)

@s-t-e-v-e-n-k
Copy link
Author

I have addressed both comments, thanks!

CHANGES.rst Outdated Show resolved Hide resolved
datetime.utcnow() is deprecated for Python 3.12+, and raises a warning.
Since warnings are treated as errors, this results in test failures.
Since utcnow calls are done by the SQLAlchemy mapping machinery, we need
to use a function.

Fixes pallets-eco#1303
@s-t-e-v-e-n-k
Copy link
Author

@davidism Thanks, I've reverted that bit.

@davidism
Copy link
Member

Sorry, completely forgot about this PR and ended up fixing it myself when doing some updates. Thanks for working on it!

@davidism davidism closed this Apr 25, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Python 3.12 changes cause test failures due to deprecation warning
3 participants