Skip to content

Commit

Permalink
chore: Move datetime.fromisoformat patch to singer_sdk.helpers._compat
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Dec 2, 2023
1 parent 0b0faef commit a8a0564
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
20 changes: 19 additions & 1 deletion singer_sdk/helpers/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import datetime
import sys

if sys.version_info < (3, 8):
Expand All @@ -21,4 +22,21 @@
else:
from importlib import resources

__all__ = ["metadata", "final", "resources", "entry_points"]
if sys.version_info < (3, 11):
from backports.datetime_fromisoformat import MonkeyPatch

MonkeyPatch.patch_fromisoformat()

datetime_fromisoformat = datetime.datetime.fromisoformat
date_fromisoformat = datetime.date.fromisoformat
time_fromisoformat = datetime.time.fromisoformat

__all__ = [
"metadata",
"final",
"resources",
"entry_points",
"datetime_fromisoformat",
"date_fromisoformat",
"time_fromisoformat",
]
19 changes: 9 additions & 10 deletions singer_sdk/sinks/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import datetime
import importlib.util
import json
import sys
import time
import typing as t
from gzip import GzipFile
Expand All @@ -23,7 +22,12 @@
BatchFileFormat,
StorageTarget,
)
from singer_sdk.helpers._compat import final
from singer_sdk.helpers._compat import (
date_fromisoformat,
datetime_fromisoformat,
final,
time_fromisoformat,
)
from singer_sdk.helpers._typing import (
DatetimeErrorTreatmentEnum,
get_datelike_property_type,
Expand All @@ -35,11 +39,6 @@

from singer_sdk.target_base import Target

if sys.version_info < (3, 11):
from backports.datetime_fromisoformat import MonkeyPatch

MonkeyPatch.patch_fromisoformat()

JSONSchemaValidator = Draft7Validator


Expand Down Expand Up @@ -384,11 +383,11 @@ def _parse_timestamps_in_record(
try:
if value is not None:
if datelike_type == "time":
date_val = datetime.time.fromisoformat(date_val)
date_val = time_fromisoformat(date_val)
elif datelike_type == "date":
date_val = datetime.date.fromisoformat(date_val)
date_val = date_fromisoformat(date_val)
else:
date_val = datetime.datetime.fromisoformat(date_val)
date_val = datetime_fromisoformat(date_val)
except ValueError as ex:
date_val = handle_invalid_timestamp_in_record(
record,
Expand Down

0 comments on commit a8a0564

Please sign in to comment.