TP2000-1581 Fixing timezone errors #1336
Merged
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.
TP2000-1581 Fixing timezone errors
Why
When running tests there is a lot of noise and warnings which distract from the useful output. Lots of these warnings are timezone related. For example:
geo_areas/tests/test_views.py::test_geo_area_detail_version_control_view
tamato/venv3.12/lib/python3.12/site-packages/django/db/models/fields/init.py:1595: RuntimeWarning: DateTimeField TrackedModelCheck.created_at received a naive datetime (1970-01-01 00:00:00) while time zone support is active.
warnings.warn(
Django has time zone support enabled by default meaning it stores datetime information in UTC in the database and uses time-zone-aware datetime objects. These warnings arise when we assign naive datetimes or basic dates to datetime fields.
What
This PR looks at where we are using naive datetime objects or simple dates and replaces them with timezone aware dates to reduce the warnings.
make_aware()
is a Django utility function used to make datetimes timezone aware. It sets the timezone to that defined in the project settings - in this case 'Europe/London'.This allows more flexibility rather than hardcoding a timezone.
Pytest log output is down from 2000+ lines to ~1580.
DateTimeField errors are down from 161 to 3. The last 3 I can't pinpoint exactly but they seem to relate to migrations in the pytest setup and do not appear when pytest --no-migrations is run. Given that we shouldn't edit old migrations anyway I've left these for now.