From 0fbf241493be8d338ab028dda2ef52bd188290be Mon Sep 17 00:00:00 2001 From: GeorgeS311578 Date: Wed, 23 Sep 2026 23:50:38 -0500 Subject: [PATCH] Show the original input in to_datetime parsing errors --- HinetPy/utils.py | 7 ++++++- tests/test_utils.py | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/HinetPy/utils.py b/HinetPy/utils.py index 44558b18..2e7a50cb 100644 --- a/HinetPy/utils.py +++ b/HinetPy/utils.py @@ -197,6 +197,7 @@ def to_datetime(value: str | datetime | date) -> datetime: return datetime.combine(value, datetime.min.time()) # is a string + original = value for char in ["T", "-", ":", ",", "_"]: value = value.replace(char, " ") @@ -218,7 +219,11 @@ def to_datetime(value: str | datetime | date) -> datetime: elif len(parts) == 6: strfmt = "%Y %m %d %H %M %S.%f" if "." in value else "%Y %m %d %H %M %S" - return datetime.strptime(value, strfmt) + try: + return datetime.strptime(value, strfmt) + except ValueError as err: + msg = f"Unable to parse {original!r} as a datetime." + raise ValueError(msg) from err def check_cmd_exists(cmd: str) -> bool: diff --git a/tests/test_utils.py b/tests/test_utils.py index bd94d1d7..9134f2f1 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -136,3 +136,11 @@ def json(self): monkeypatch.setattr(utils.requests, "get", lambda url, timeout: response) with pytest.raises(requests.HTTPError): check_package_release() + + +def test_to_datetime_invalid_string(): + """ + Test that to_datetime reports the original input string when parsing fails. + """ + with pytest.raises(ValueError, match=r"2010-01-01T03:45:12,5"): + to_datetime("2010-01-01T03:45:12,5")