Skip to content

Commit

Permalink
Removing pytz dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBurchLog committed Nov 4, 2024
1 parent 35698fd commit 957bf8f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
23 changes: 14 additions & 9 deletions brewtils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import copy
from datetime import datetime
from enum import Enum
from zoneinfo import ZoneInfo

import pytz # noqa # not in requirements file
import six # noqa # not in requirements file

from brewtils.errors import ModelError, _deprecate
Expand Down Expand Up @@ -1273,9 +1273,10 @@ def scheduler_attributes(self):

@property
def scheduler_kwargs(self):
tz = pytz.timezone(self.timezone)

return {"timezone": tz, "run_date": tz.localize(self.run_date)}
tz = ZoneInfo(self.timezone.upper())

return {"timezone": tz, "run_date": self.run_date.replace(tzinfo=tz)}


class IntervalTrigger(BaseModel):
Expand Down Expand Up @@ -1332,14 +1333,16 @@ def scheduler_attributes(self):

@property
def scheduler_kwargs(self):
tz = pytz.timezone(self.timezone)
tz = ZoneInfo(self.timezone.upper())

kwargs = {key: getattr(self, key) for key in self.scheduler_attributes}
kwargs.update(
{
"timezone": tz,
"start_date": tz.localize(self.start_date) if self.start_date else None,
"end_date": tz.localize(self.end_date) if self.end_date else None,
"start_date": (
self.start_date.replace(tzinfo=tz) if self.start_date else None
),
"end_date": self.end_date.replace(tzinfo=tz) if self.end_date else None,
}
)

Expand Down Expand Up @@ -1408,14 +1411,16 @@ def scheduler_attributes(self):

@property
def scheduler_kwargs(self):
tz = pytz.timezone(self.timezone)
tz = ZoneInfo(self.timezone.upper())

kwargs = {key: getattr(self, key) for key in self.scheduler_attributes}
kwargs.update(
{
"timezone": tz,
"start_date": tz.localize(self.start_date) if self.start_date else None,
"end_date": tz.localize(self.end_date) if self.end_date else None,
"start_date": (
self.start_date.replace(tzinfo=tz) if self.start_date else None
),
"end_date": self.end_date.replace(tzinfo=tz) if self.end_date else None,
}
)

Expand Down
10 changes: 5 additions & 5 deletions brewtils/test/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-

import copy
from datetime import datetime
from datetime import datetime, timezone
from zoneinfo import ZoneInfo

import pytest
import pytz

from brewtils.models import (
AliasUserMap,
Expand Down Expand Up @@ -62,7 +62,7 @@ def ts_epoch():
@pytest.fixture
def ts_dt_utc(ts_epoch):
"""Jan 1, 2016 UTC as timezone-aware datetime."""
return datetime.fromtimestamp(ts_epoch / 1000, tz=pytz.utc)
return datetime.fromtimestamp(ts_epoch / 1000, tz=timezone.utc)


@pytest.fixture
Expand All @@ -74,7 +74,7 @@ def ts_epoch_eastern():
@pytest.fixture
def ts_dt_eastern():
"""Jan 1, 2016 US/Eastern as timezone-aware datetime."""
return datetime(2016, 1, 1, tzinfo=pytz.timezone("US/Eastern"))
return datetime(2016, 1, 1, tzinfo=ZoneInfo("US/Eastern"))


@pytest.fixture
Expand All @@ -92,7 +92,7 @@ def ts_2_epoch():
@pytest.fixture
def ts_2_dt_utc(ts_2_epoch):
"""Feb 2, 2017 UTC as timezone-aware datetime."""
return datetime.fromtimestamp(ts_2_epoch / 1000, tz=pytz.utc)
return datetime.fromtimestamp(ts_2_epoch / 1000, tz=timezone.utc)


@pytest.fixture
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def find_version():
"marshmallow-polyfield<6",
"packaging",
"pika<=1.4,>=1.0.1",
"pytz",
"requests<3",
"simplejson<4",
"six<2",
Expand Down
28 changes: 19 additions & 9 deletions test/models_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# -*- coding: utf-8 -*-
import warnings
from datetime import timezone
from zoneinfo import ZoneInfo

import pytest
import pytz
from pytest_lazyfixture import lazy_fixture

from brewtils.errors import ModelError
from brewtils.models import (
Choices,
Expand All @@ -13,17 +16,16 @@
LoggingConfig,
Parameter,
PatchOperation,
User,
Queue,
Request,
RequestFile,
RequestTemplate,
Role,
Subscriber,
StatusInfo,
Subscriber,
Topic,
User,
)
from pytest_lazyfixture import lazy_fixture


@pytest.fixture
Expand Down Expand Up @@ -567,7 +569,7 @@ def test_repr(self, role):
class TestDateTrigger(object):
def test_scheduler_kwargs(self, bg_date_trigger, ts_dt_utc):
assert bg_date_trigger.scheduler_kwargs == {
"timezone": pytz.utc,
"timezone": ZoneInfo("UTC"),
"run_date": ts_dt_utc,
}

Expand Down Expand Up @@ -595,7 +597,7 @@ def test_scheduler_kwargs_default(self):
"seconds": None,
"start_date": None,
"end_date": None,
"timezone": pytz.utc,
"timezone": ZoneInfo("UTC"),
"jitter": None,
"reschedule_on_finish": None,
}
Expand All @@ -605,7 +607,11 @@ def test_scheduler_kwargs(
):
expected = interval_trigger_dict
expected.update(
{"timezone": pytz.utc, "start_date": ts_dt_utc, "end_date": ts_2_dt_utc}
{
"timezone": ZoneInfo("UTC"),
"start_date": ts_dt_utc,
"end_date": ts_2_dt_utc,
}
)
assert bg_interval_trigger.scheduler_kwargs == expected

Expand All @@ -623,7 +629,7 @@ def test_scheduler_kwargs_default(self):
"second": None,
"start_date": None,
"end_date": None,
"timezone": pytz.utc,
"timezone": ZoneInfo("UTC"),
"jitter": None,
}

Expand All @@ -632,7 +638,11 @@ def test_scheduler_kwargs(
):
expected = cron_trigger_dict
expected.update(
{"timezone": pytz.utc, "start_date": ts_dt_utc, "end_date": ts_2_dt_utc}
{
"timezone": ZoneInfo("UTC"),
"start_date": ts_dt_utc,
"end_date": ts_2_dt_utc,
}
)
assert bg_cron_trigger.scheduler_kwargs == expected

Expand Down

0 comments on commit 957bf8f

Please sign in to comment.