Skip to content

Commit

Permalink
fix(sonarcloud): use zoneinfo.ZoneInfo instead of pytz.timezone
Browse files Browse the repository at this point in the history
fix SonarCloud code smell python:S6890:
> Don't use `pytz` module with Python 3.9 and later.
> "zoneinfo" should be preferred to "pytz" when using Python 3.9 and
> later python:S6890

See rule: https://rules.sonarsource.com/python/RSPEC-6890/

Also:
 - Remove `pytz` dependency as unused
 - Update snapshots, see comment below

**NOTE**: Times in snapshots changed because of difference between
pytz.timezone and zoneinfo.ZoneInfo usage. See:
- https://stackoverflow.com/questions/35462876/python-pytz-timezone-function-returns-a-timezone-that-is-off-by-9-minutes
- https://stackoverflow.com/questions/75559676/pytz-and-zoneinfo-return-different-times-for-same-functions-whats-wrong-with-m
- https://blog.ganssle.io/articles/2018/03/pytz-fastest-footgun.html

It seems pytz was using local mean solar time which is not necessarily
an integer hours offset from UTC, and using zoneinfo.ZoneInfo with
"Europe/Helsinki" returned an integer hours offset from UTC.

refs KK-1416
  • Loading branch information
karisal-anders committed Feb 20, 2025
1 parent 82b09d2 commit d2937b8
Show file tree
Hide file tree
Showing 12 changed files with 1,941 additions and 1,616 deletions.
1,003 changes: 548 additions & 455 deletions children/tests/snapshots/snap_test_models.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions children/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from copy import deepcopy
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo

import pytest
import pytz
from auditlog.context import disable_auditlog
from dateutil.relativedelta import relativedelta
from django.conf import settings
Expand Down Expand Up @@ -1297,7 +1297,7 @@ def test_get_available_events(
# Past occurrences
OccurrenceFactory.create_batch(
3,
time=datetime(1970, 1, 1, 0, 0, 0, tzinfo=pytz.timezone(settings.TIME_ZONE)),
time=datetime(1970, 1, 1, 0, 0, 0, tzinfo=ZoneInfo(settings.TIME_ZONE)),
event__project=project,
venue=venue,
)
Expand Down
4 changes: 2 additions & 2 deletions events/factories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import random
from zoneinfo import ZoneInfo

import factory
import pytz
from django.utils import timezone

from children.factories import ChildFactory
Expand Down Expand Up @@ -70,7 +70,7 @@ class TixlyEventFactory(RandomExternalTicketSystemEventFactory):
class OccurrenceFactory(
SaveAfterPostGenerationMixin, factory.django.DjangoModelFactory
):
time = factory.Faker("date_time", tzinfo=pytz.timezone("Europe/Helsinki"))
time = factory.Faker("date_time", tzinfo=ZoneInfo("Europe/Helsinki"))
event = factory.SubFactory(EventFactory)
venue = factory.SubFactory(VenueFactory)

Expand Down
18 changes: 9 additions & 9 deletions events/tests/snapshots/snap_test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@
'ticketSystem': {
'type': 'INTERNAL'
},
'time': '1970-12-29T14:27:50.629900+00:00',
'time': '1970-12-29T14:07:50.629900+00:00',
'venue': {
'translations': [
{
Expand Down Expand Up @@ -794,7 +794,7 @@
'ticketSystem': {
'type': 'INTERNAL'
},
'time': '1970-12-29T14:27:50.629900+00:00',
'time': '1970-12-29T14:07:50.629900+00:00',
'venue': {
'translations': [
{
Expand Down Expand Up @@ -858,7 +858,7 @@
'ticketSystem': {
'type': 'INTERNAL'
},
'time': '2004-11-06T11:51:21.341823+00:00',
'time': '2004-11-06T11:31:21.341823+00:00',
'venue': {
'translations': [
{
Expand Down Expand Up @@ -913,7 +913,7 @@
'ticketSystem': {
'type': 'INTERNAL'
},
'time': '1975-04-24T16:02:40.246613+00:00',
'time': '1975-04-24T15:42:40.246613+00:00',
'venue': {
'translations': [
{
Expand Down Expand Up @@ -1258,12 +1258,12 @@
'edges': [
{
'node': {
'time': '1988-06-02T19:08:36.943149+00:00'
'time': '1988-06-02T17:48:36.943149+00:00'
}
},
{
'node': {
'time': '1998-01-14T19:16:11.484399+00:00'
'time': '1998-01-14T18:56:11.484399+00:00'
}
}
]
Expand Down Expand Up @@ -1395,17 +1395,17 @@
'edges': [
{
'node': {
'time': '1971-09-03T06:53:22.223652+00:00'
'time': '1971-09-03T06:33:22.223652+00:00'
}
},
{
'node': {
'time': '1981-10-20T21:01:52.047649+00:00'
'time': '1981-10-20T20:41:52.047649+00:00'
}
},
{
'node': {
'time': '2017-01-24T18:52:46.127687+00:00'
'time': '2017-01-24T18:32:46.127687+00:00'
}
}
]
Expand Down
987 changes: 537 additions & 450 deletions gdpr/tests/snapshots/snap_test_gdpr_api.py

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions messaging/tests/snapshots/snap_test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@
'edges': [
{
'node': {
'time': '1970-12-29T14:27:50.629900+00:00'
'time': '1970-12-29T14:07:50.629900+00:00'
}
},
{
'node': {
'time': '1977-02-25T23:14:59.889967+00:00'
'time': '1977-02-25T22:54:59.889967+00:00'
}
},
{
'node': {
'time': '1997-09-11T01:32:17.610651+00:00'
'time': '1997-09-11T00:12:17.610651+00:00'
}
}
]
Expand Down Expand Up @@ -191,7 +191,7 @@
'edges': [
{
'node': {
'time': '1980-08-23T04:46:09.363315+00:00'
'time': '1980-08-23T04:26:09.363315+00:00'
}
}
]
Expand All @@ -214,7 +214,7 @@
'edges': [
{
'node': {
'time': '1980-08-23T04:46:09.363315+00:00'
'time': '1980-08-23T04:26:09.363315+00:00'
}
}
]
Expand All @@ -237,7 +237,7 @@
'edges': [
{
'node': {
'time': '2003-01-02T18:10:46.571751+00:00'
'time': '2003-01-02T17:50:46.571751+00:00'
}
}
]
Expand All @@ -260,7 +260,7 @@
'edges': [
{
'node': {
'time': '2003-01-02T18:10:46.571751+00:00'
'time': '2003-01-02T17:50:46.571751+00:00'
}
}
]
Expand Down
1 change: 0 additions & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ markdown
Pillow
psycopg2
pycountry
pytz
qrcode
requests
sentry-sdk
Expand Down
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ python-jose==3.4.0
# via django-helusers
python3-openid==3.2.0
# via social-auth-core
pytz==2025.1
# via -r requirements.in
pyyaml==6.0.2
# via drf-spectacular
qrcode==8.0
Expand Down
143 changes: 84 additions & 59 deletions subscriptions/tests/snapshots/snap_test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@

from snapshottest import Snapshot


snapshots = Snapshot()

snapshots["test_child_free_spot_notifications_query 1"] = {
"data": {
"child": {
"freeSpotNotificationSubscriptions": {
"edges": [
snapshots['test_child_free_spot_notifications_query 1'] = {
'data': {
'child': {
'freeSpotNotificationSubscriptions': {
'edges': [
{
"node": {
"child": {"name": "Subscriber"},
"createdAt": "2020-12-12T00:00:00+00:00",
"id": "RnJlZVNwb3ROb3RpZmljYXRpb25TdWJzY3JpcHRpb25Ob2RlOjE=",
"occurrence": {"time": "1993-10-11T08:58:57.954874+00:00"},
'node': {
'child': {
'name': 'Subscriber'
},
'createdAt': '2020-12-12T00:00:00+00:00',
'id': 'RnJlZVNwb3ROb3RpZmljYXRpb25TdWJzY3JpcHRpb25Ob2RlOjE=',
'occurrence': {
'time': '1993-10-11T08:38:57.954874+00:00'
}
}
}
]
Expand All @@ -25,27 +30,27 @@
}
}

snapshots["test_occurrences_has_child_free_spot_notification_query 1"] = {
"data": {
"child": {
"availableEvents": {
"edges": [
snapshots['test_occurrences_has_child_free_spot_notification_query 1'] = {
'data': {
'child': {
'availableEvents': {
'edges': [
{
"node": {
"occurrences": {
"edges": [
'node': {
'occurrences': {
'edges': [
{
"node": {
"childHasFreeSpotNotificationSubscription": True,
"time": "2020-12-26T00:00:00+00:00",
'node': {
'childHasFreeSpotNotificationSubscription': True,
'time': '2020-12-26T00:00:00+00:00'
}
},
{
"node": {
"childHasFreeSpotNotificationSubscription": False,
"time": "2020-12-27T00:00:00+00:00",
'node': {
'childHasFreeSpotNotificationSubscription': False,
'time': '2020-12-27T00:00:00+00:00'
}
},
}
]
}
}
Expand All @@ -56,60 +61,80 @@
}
}

snapshots["test_subscribe_to_free_spot_notification 1"] = {
"data": {
"subscribeToFreeSpotNotification": {
"child": {"name": "Subscriber"},
"occurrence": {"time": "2020-12-26T00:00:00+00:00"},
snapshots['test_subscribe_to_free_spot_notification 1'] = {
'data': {
'subscribeToFreeSpotNotification': {
'child': {
'name': 'Subscriber'
},
'occurrence': {
'time': '2020-12-26T00:00:00+00:00'
}
}
}
}

snapshots["test_unsubscribe_from_all_notifications 1"] = {
"data": {
"unsubscribeFromAllNotifications": {
"guardian": {"user": {"username": "blake64"}},
"unsubscribed": True,
snapshots['test_unsubscribe_from_all_notifications 1'] = {
'data': {
'unsubscribeFromAllNotifications': {
'guardian': {
'user': {
'username': 'blake64'
}
},
'unsubscribed': True
}
}
}

snapshots["test_unsubscribe_from_all_notifications_using_auth_verification_token 1"] = {
"data": {
"unsubscribeFromAllNotifications": {
"guardian": {"user": {"username": "blake64"}},
"unsubscribed": True,
snapshots['test_unsubscribe_from_all_notifications_using_auth_verification_token 1'] = {
'data': {
'unsubscribeFromAllNotifications': {
'guardian': {
'user': {
'username': 'blake64'
}
},
'unsubscribed': True
}
}
}

snapshots[
"test_unsubscribe_from_all_notifications_using_auth_verification_token_as_logged_in 1"
] = {
"data": {
"unsubscribeFromAllNotifications": {
"guardian": {"user": {"username": "blake64"}},
"unsubscribed": True,
snapshots['test_unsubscribe_from_all_notifications_using_auth_verification_token_as_logged_in 1'] = {
'data': {
'unsubscribeFromAllNotifications': {
'guardian': {
'user': {
'username': 'blake64'
}
},
'unsubscribed': True
}
}
}

snapshots[
"test_unsubscribe_from_all_notifications_when_logged_in_user_not_auth_token_user 1"
] = {
"data": {
"unsubscribeFromAllNotifications": {
"guardian": {"user": {"username": "jeffersonkimberly"}},
"unsubscribed": True,
snapshots['test_unsubscribe_from_all_notifications_when_logged_in_user_not_auth_token_user 1'] = {
'data': {
'unsubscribeFromAllNotifications': {
'guardian': {
'user': {
'username': 'jeffersonkimberly'
}
},
'unsubscribed': True
}
}
}

snapshots["test_unsubscribe_from_free_spot_notification 1"] = {
"data": {
"unsubscribeFromFreeSpotNotification": {
"child": {"name": "Subscriber"},
"occurrence": {"time": "1993-10-11T08:58:57.954874+00:00"},
snapshots['test_unsubscribe_from_free_spot_notification 1'] = {
'data': {
'unsubscribeFromFreeSpotNotification': {
'child': {
'name': 'Subscriber'
},
'occurrence': {
'time': '1993-10-11T08:38:57.954874+00:00'
}
}
}
}
Loading

0 comments on commit d2937b8

Please sign in to comment.