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

fix future deprecate warning of django.utils.timezone.utc #339

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

### Changed
- Remove warning for future Django deprecation [PR #339](https://github.com/model-bakers/model_bakery/pull/339)

### Removed

Expand Down
5 changes: 2 additions & 3 deletions model_bakery/timezone.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"""Utility functions to manage timezone code."""

from datetime import datetime
from datetime import datetime, timezone

from django.conf import settings
from django.utils.timezone import utc


def tz_aware(value: datetime) -> datetime:
"""Return an UTC-aware datetime in case of USE_TZ=True."""
if settings.USE_TZ:
value = value.replace(tzinfo=utc)
value = value.replace(tzinfo=timezone.utc)

return value
3 changes: 1 addition & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from inspect import getmodule

import pytest
from django.utils.timezone import utc

from model_bakery.utils import get_calling_module, import_from_str, seq
from tests.generic.models import User
Expand Down Expand Up @@ -145,7 +144,7 @@ def test_time(self):
@pytest.mark.parametrize("use_tz", [False, True])
def test_datetime(self, settings, use_tz):
settings.USE_TZ = use_tz
tzinfo = utc if use_tz else None
tzinfo = datetime.timezone.utc if use_tz else None

sequence = seq(
datetime.datetime(2021, 2, 11, 15, 39, 58, 457698),
Expand Down