Doctest failure when passing tz-aware datetime to a conversions/ date utility in thealgorithms/Python
0 reputation · 13 Jul 2021, 01:39 UTC
0 reputation · 13 Jul 2021, 01:39 UTC
I am working with the date/timestamp conversion utilities in the conversions/ directory of thealgorithms/Python and want to handle regional time zones correctly. My precise goal is to convert a Unix timestamp into a localized date string for a specific IANA zone (for example Europe/Berlin) using zoneinfo, while keeping the repo's doctest-based validation passing.
The existing conversion functions appear to assume naive datetimes and fixed format strings, and their doctests only exercise naive inputs. When a tzinfo-aware datetime is supplied, the expected-output doctests no longer match, which causes a pytest doctest failure — the project's primary correctness gate per its contributing guidelines. It is unclear whether the intended contract is that callers must pre-convert to UTC and pass naive values, or whether aware input is simply unsupported.
Assume a recent master checkout and Python 3.11+; behavior may be commit-sensitive since the repo has no stable releases.
zoneinfo-aware input, what doctest output convention should I follow so the CI doctest run stays deterministic across machine time zones?ValueError, given error handling is inconsistent across files?In the thealgorithms/Python repository, conversion utilities are designed as algorithmic demonstrations rather than production libraries. Consequently, they typically assume offset-naive datetimes. Passing a zoneinfo-aware object to a function expecting a naive object (or vice versa) triggers a TypeError or causes a doctest mismatch because aware objects append UTC offsets (e.g., +02:00) to their string representation.
The failure occurs because Python prohibits mixing naive and aware datetimes in comparisons or arithmetic. Since the existing doctests expect naive output strings, any aware input that propagates to the output will fail the pytest doctest gate.
To extend a converter to support zoneinfo while keeping CI deterministic, follow these steps:
.astimezone(tz). If it is naive, treat it as UTC or a specified default before conversion.datetime.now() or local system zones in doctests. Use explicit zoneinfo.ZoneInfo objects and hard-code the expected offset in the doctest string.>>> from zoneinfo import ZoneInfo
>>> from conversions.date_utils import convert_timestamp
>>> convert_timestamp(1697232000, tz=ZoneInfo("Europe/Berlin"))
'2023-10-13 12:00:00+02:00'
Since error handling is inconsistent across the repository, the safest approach for a new contribution is to follow the standard Python datetime behavior: let the fold attribute handle ambiguities. If the utility requires strict validation, raising a ValueError for non-existent local times (DST gaps) is the appropriate Pythonic pattern, provided it is documented in the function's docstring.
To verify the fix without running the entire suite, use the following scoped command:
pytest --doctest-modules conversions/your_file.py
Missing Diagnostic: Does the specific function you are modifying perform arithmetic (e.g., adding timedeltas) or is it a pure formatting conversion? Arithmetic operations will require stricter normalization to avoid TypeError.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.