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

refactor: Remove pytz dependency and use datetime.timezone.utc instead of pytz.UTC where possible #2157

Merged
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
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
6 changes: 3 additions & 3 deletions singer_sdk/helpers/_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
if t.TYPE_CHECKING:
from types import ModuleType

from singer_sdk.helpers._compat 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 +24,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)
4 changes: 3 additions & 1 deletion singer_sdk/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

import yaml

from singer_sdk.helpers._resources import Traversable, get_package_files
from singer_sdk.helpers._resources import get_package_files

if t.TYPE_CHECKING:
from types import TracebackType

from singer_sdk.helpers._compat import Traversable

DEFAULT_LOG_INTERVAL = 60.0
METRICS_LOGGER_NAME = __name__
METRICS_LOG_LEVEL_SETTING = "metrics_log_level"
Expand Down
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"
7 changes: 4 additions & 3 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 @@ -30,11 +30,12 @@ def test_round_trip():
"2021-01-01T00:00:00.000000+00:00",
"2021-01-01T00:00:00.000000+06:00",
"2021-01-01T00:00:00.000000-04:00",
"2021-01-01T00:00:00.000000",
],
ids=["Z", "offset+0", "offset+6", "offset-4"],
ids=["Z", "offset+0", "offset+6", "offset-4", "no offset"],
)
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