Skip to content

Commit

Permalink
refactor: Remove pytz dependency and use datetime.timezone.utc in…
Browse files Browse the repository at this point in the history
…stead of `pytz.UTC` where possible
  • Loading branch information
edgarrmondragon committed Jan 19, 2024
1 parent 02f6288 commit 4e6fd40
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 124 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Local Poetry configuration file

poetry.toml

# CI

_changelog_fragment.md
Expand Down
3 changes: 2 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@
"coverage[toml]",
"duckdb",
"duckdb-engine",
"pyarrow",
"pytest",
"pytest-benchmark",
"pytest-durations",
"pytest-snapshot",
"pyarrow",
"pytz",
"requests-mock",
"rfc3339-validator",
"time-machine",
Expand Down
223 changes: 113 additions & 110 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ pendulum = [
PyJWT = "~=2.4"
python-dateutil = ">=2.8.2"
python-dotenv = ">=0.20"
pytz = ">=2022.2.1"
PyYAML = ">=6.0"
requests = ">=2.25.1"
simpleeval = ">=0.9.13"
Expand Down Expand Up @@ -136,6 +135,7 @@ mypy = [
]
pytest-benchmark = ">=4.0.0"
pytest-snapshot = ">=0.9.0"
pytz = ">=2022.2.1"
requests-mock = ">=1.10.0"
rfc3339-validator = ">=0.1.4"
time-machine = [
Expand Down Expand Up @@ -214,7 +214,6 @@ fail_under = 82
[tool.mypy]
exclude = "tests"
files = "singer_sdk"
python_version = "3.8"
warn_redundant_casts = true
warn_return_any = true
warn_unused_configs = true
Expand Down
8 changes: 3 additions & 5 deletions singer_sdk/_singerlib/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from __future__ import annotations

import sys
from datetime import datetime, timedelta

import pytz
from datetime import datetime, timedelta, timezone

if sys.version_info < (3, 11):
from backports.datetime_fromisoformat import MonkeyPatch
Expand Down Expand Up @@ -33,9 +31,9 @@ def strptime_to_utc(dtimestr: str) -> datetime:
"""
d_object: datetime = datetime.fromisoformat(dtimestr)
if d_object.tzinfo is None:
return d_object.replace(tzinfo=pytz.UTC)
return d_object.replace(tzinfo=timezone.utc)

return d_object.astimezone(tz=pytz.UTC)
return d_object.astimezone(tz=timezone.utc)


def strftime(dtime: datetime, format_str: str = DATETIME_FMT) -> str:
Expand Down
9 changes: 6 additions & 3 deletions singer_sdk/helpers/_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
if t.TYPE_CHECKING:
from types import ModuleType

if sys.version_info < (3, 9):
from importlib_resources.abc import Traversable
else:
from importlib.abc import Traversable

if sys.version_info < (3, 9):
import importlib_resources
from importlib_resources.abc import Traversable
else:
import importlib.resources as importlib_resources
from importlib.abc import Traversable


def get_package_files(package: str | ModuleType) -> Traversable:
Expand All @@ -24,4 +27,4 @@ def get_package_files(package: str | ModuleType) -> Traversable:
Returns:
The file as a Traversable object.
"""
return t.cast(Traversable, importlib_resources.files(package))
return importlib_resources.files(package)
2 changes: 1 addition & 1 deletion singer_sdk/testing/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,4 @@ def singer_filepath(self) -> Traversable:
Returns:
The expected Path to this tests singer file.
"""
return importlib_resources.files(target_test_streams) / f"{self.name}.singer" # type: ignore[no-any-return]
return importlib_resources.files(target_test_streams) / f"{self.name}.singer"
4 changes: 2 additions & 2 deletions tests/_singerlib/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from datetime import datetime
from datetime import datetime, timezone

import pytest
import pytz
Expand Down Expand Up @@ -34,7 +34,7 @@ def test_round_trip():
ids=["Z", "offset+0", "offset+6", "offset-4"],
)
def test_strptime_to_utc(dtimestr):
assert strptime_to_utc(dtimestr).tzinfo == pytz.UTC
assert strptime_to_utc(dtimestr).tzinfo == timezone.utc


def test_stftime_non_utc():
Expand Down

0 comments on commit 4e6fd40

Please sign in to comment.