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

chore: use zoneinfo instead of pytz #107

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 8 additions & 21 deletions dirty_equals/_datetime.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from __future__ import annotations as _annotations

import sys
from datetime import date, datetime, timedelta, timezone, tzinfo
from typing import TYPE_CHECKING, Any
from typing import Any

from ._numeric import IsNumeric
from ._utils import Omit

if TYPE_CHECKING:
if sys.version_info >= (3, 9):
from zoneinfo import ZoneInfo
else:
# This code block is due to a typing issue with backports.zoneinfo package:
# https://github.com/pganssle/zoneinfo/issues/125
from backports.zoneinfo._zoneinfo import ZoneInfo


class IsDatetime(IsNumeric[datetime]):
Expand Down Expand Up @@ -122,24 +127,6 @@ def approx_equals(self, other: datetime, delta: timedelta) -> bool:
return True


def _zoneinfo(tz: str) -> ZoneInfo:
"""
Instantiate a `ZoneInfo` object from a string, falling back to `pytz.timezone` when `ZoneInfo` is not available
(most likely on Python 3.8 and webassembly).
"""
try:
from zoneinfo import ZoneInfo
except ImportError:
try:
import pytz
except ImportError as e:
raise ImportError('`pytz` or `zoneinfo` required for tz handling') from e
else:
return pytz.timezone(tz) # type: ignore[return-value]
else:
return ZoneInfo(tz)


class IsNow(IsDatetime):
"""
Check if a datetime is close to now, this is similar to `IsDatetime(approx=datetime.now())`,
Expand Down Expand Up @@ -185,7 +172,7 @@ def __init__(
```
"""
if isinstance(tz, str):
tz = _zoneinfo(tz)
tz = ZoneInfo(tz)

self.tz = tz

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ classifiers = [
]
requires-python = '>=3.8'
dependencies = [
'pytz>=2021.3;python_version<"3.9"',
'backports.zoneinfo;python_version<"3.9"',
]
optional-dependencies = {pydantic = ['pydantic>=2.4.2'] }
dynamic = ['version']
Expand Down
2 changes: 0 additions & 2 deletions requirements/linting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ pydantic-core==2.20.1
# via pydantic
ruff==0.5.7
# via -r requirements/linting.in
types-pytz==2024.1.0.20240417
# via -r requirements/linting.in
typing-extensions==4.12.2
# via
# mypy
Expand Down
2 changes: 1 addition & 1 deletion requirements/tests.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pytest
pytest-mock
pytest-pretty
pytest-examples
pytz
backports.zoneinfo;python_version<"3.9"
2 changes: 1 addition & 1 deletion requirements/tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pytest-mock==3.14.0
# via -r requirements/tests.in
pytest-pretty==1.2.0
# via -r requirements/tests.in
pytz==2024.1
backports.zoneinfo;python_version<"3.9"
# via -r requirements/tests.in
rich==13.7.1
# via pytest-pretty
Expand Down
20 changes: 10 additions & 10 deletions tests/test_datetime.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import sys
from datetime import date, datetime, timedelta, timezone
from unittest.mock import Mock

import pytest
import pytz

from dirty_equals import IsDate, IsDatetime, IsNow, IsToday

try:
if sys.version_info >= (3, 9):
from zoneinfo import ZoneInfo
except ImportError:
ZoneInfo = None
else:
# This code block is due to a typing issue with backports.zoneinfo package:
# https://github.com/pganssle/zoneinfo/issues/125
from backports.zoneinfo._zoneinfo import ZoneInfo


@pytest.mark.parametrize(
Expand Down Expand Up @@ -61,16 +63,14 @@
id='tz-1-hour',
),
pytest.param(
pytz.timezone('Europe/London').localize(datetime(2022, 2, 15, 15, 15)),
IsDatetime(
approx=pytz.timezone('America/New_York').localize(datetime(2022, 2, 15, 10, 15)), enforce_tz=False
),
datetime(2022, 2, 15, 15, 15, tzinfo=ZoneInfo('Europe/London')),
IsDatetime(approx=datetime(2022, 2, 15, 10, 15, tzinfo=ZoneInfo('America/New_York')), enforce_tz=False),
True,
id='tz-both-tz',
),
pytest.param(
pytz.timezone('Europe/London').localize(datetime(2022, 2, 15, 15, 15)),
IsDatetime(approx=pytz.timezone('America/New_York').localize(datetime(2022, 2, 15, 10, 15))),
datetime(2022, 2, 15, 15, 15, tzinfo=ZoneInfo('Europe/London')),
IsDatetime(approx=datetime(2022, 2, 15, 10, 15, tzinfo=ZoneInfo('America/New_York'))),
False,
id='tz-both-tz-different',
),
Expand Down
Loading