Skip to content

Commit

Permalink
chore: Enable DTZ (flake8-datetimezs) Ruff checks (#1567)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon authored Mar 30, 2023
1 parent 0411c2c commit a5a94d7
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ select = [
"A", # flake8-builtins
"COM", # flake8-commas
"C4", # flake8-comprehensions
"DTZ", # flake8-datetimezs
"T10", # flake8-debugger
"ISC", # flake8-implicit-str-concat
"ICN", # flake8-import-conventions
Expand Down
7 changes: 5 additions & 2 deletions singer_sdk/helpers/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
_MAX_TIME = "23:59:59.999999"
JSONSCHEMA_ANNOTATION_SECRET = "secret" # noqa: S105
JSONSCHEMA_ANNOTATION_WRITEONLY = "writeOnly"
UTC = datetime.timezone.utc


class DatetimeErrorTreatmentEnum(Enum):
Expand Down Expand Up @@ -452,9 +453,11 @@ def _conform_primitive_property(elem: Any, property_schema: dict) -> Any:
if isinstance(elem, datetime.date):
return elem.isoformat() + "T00:00:00+00:00"
if isinstance(elem, datetime.timedelta):
epoch = datetime.datetime.utcfromtimestamp(0)
epoch = datetime.datetime.fromtimestamp(0, UTC)
timedelta_from_epoch = epoch + elem
return timedelta_from_epoch.isoformat() + "+00:00"
if timedelta_from_epoch.tzinfo is None:
timedelta_from_epoch = timedelta_from_epoch.replace(tzinfo=UTC)
return timedelta_from_epoch.isoformat()
if isinstance(elem, datetime.time):
return str(elem)
if isinstance(elem, bytes):
Expand Down
2 changes: 1 addition & 1 deletion singer_sdk/sinks/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _get_context(self, record: dict) -> dict: # noqa: ARG002
if self._pending_batch is None:
new_context = {
"batch_id": str(uuid.uuid4()),
"batch_start_time": datetime.datetime.now(),
"batch_start_time": datetime.datetime.now(tz=datetime.timezone.utc),
}
self.start_batch(new_context)
self._pending_batch = new_context
Expand Down
7 changes: 5 additions & 2 deletions singer_sdk/sinks/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,12 @@ def _add_sdc_metadata_to_record(
context: Stream partition or context dictionary.
"""
record["_sdc_extracted_at"] = message.get("time_extracted")
record["_sdc_received_at"] = datetime.datetime.now().isoformat()
record["_sdc_received_at"] = datetime.datetime.now(
tz=datetime.timezone.utc,
).isoformat()
record["_sdc_batched_at"] = (
context.get("batch_start_time", None) or datetime.datetime.now()
context.get("batch_start_time", None)
or datetime.datetime.now(tz=datetime.timezone.utc)
).isoformat()
record["_sdc_deleted_at"] = record.get("_sdc_deleted_at")
record["_sdc_sequence"] = int(round(time.time() * 1000))
Expand Down
4 changes: 2 additions & 2 deletions tests/_singerlib/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ def test_record_message_naive_time_extracted():
singer.RecordMessage(
stream="test",
record={"id": 1, "name": "test"},
time_extracted=datetime(2021, 1, 1),
time_extracted=datetime(2021, 1, 1), # noqa: DTZ001
)


def test_record_message_time_extracted_to_utc():
"""Check that record message's time_extracted is converted to UTC."""
naive = datetime(2021, 1, 1, 12)
naive = datetime(2021, 1, 1, 12) # noqa: DTZ001
nairobi = timezone("Africa/Nairobi")

record = singer.RecordMessage(
Expand Down
4 changes: 2 additions & 2 deletions tests/_singerlib/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_small_years():


def test_round_trip():
now = datetime.utcnow().replace(tzinfo=pytz.UTC)
now = datetime.now(tz=pytz.UTC)
dtime = strftime(now)
parsed_datetime = strptime_to_utc(dtime)
formatted_datetime = strftime(parsed_datetime)
Expand All @@ -38,6 +38,6 @@ def test_strptime_to_utc(dtimestr):


def test_stftime_non_utc():
now = datetime.utcnow().replace(tzinfo=pytz.timezone("America/New_York"))
now = datetime.now(tz=pytz.timezone("America/New_York"))
with pytest.raises(NonUTCDatetimeError):
strftime(now)
5 changes: 4 additions & 1 deletion tests/core/test_record_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ def test_conform_record_data_types(
(pendulum.parse("2021-08-25T20:05:28+00:00"), "2021-08-25T20:05:28+00:00"),
(pendulum.parse("2021-08-25T20:05:28+07:00"), "2021-08-25T20:05:28+07:00"),
(
datetime.strptime("2021-08-25T20:05:28", "%Y-%m-%dT%H:%M:%S"),
datetime.strptime( # noqa: DTZ007
"2021-08-25T20:05:28",
"%Y-%m-%dT%H:%M:%S",
),
"2021-08-25T20:05:28+00:00",
),
(
Expand Down
5 changes: 4 additions & 1 deletion tests/core/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ def test_object_arrays_remove_types(caplog: pytest.LogCaptureFixture):

def test_conform_primitives():
assert (
_conform_primitive_property(datetime.datetime(2020, 5, 17), {"type": "string"})
_conform_primitive_property(
datetime.datetime(2020, 5, 17, tzinfo=datetime.timezone.utc),
{"type": "string"},
)
== "2020-05-17T00:00:00+00:00"
)
assert (
Expand Down

0 comments on commit a5a94d7

Please sign in to comment.