Skip to content

Serialize timedelta objects in parseable form#837

Merged
maddenp-cu merged 3 commits intoufs-community:mainfrom
maddenp-cu:timedelta-tagged-tests
Jan 8, 2026
Merged

Serialize timedelta objects in parseable form#837
maddenp-cu merged 3 commits intoufs-community:mainfrom
maddenp-cu:timedelta-tagged-tests

Conversation

@maddenp-cu
Copy link
Collaborator

Synopsis

This issue came up in practice. Given

a.yaml

start: !datetime 2025-01-01T00
stop: !datetime 2025-01-03T03
raw: !timedelta '{{ ((stop - start) * 0.8).total_seconds() / 3600 }}'

Current behavior:

$ uw config realize -i a.yaml | tee b.yaml
start: 2025-01-01T00:00:00
stop: 2025-01-03T03:00:00
raw: !timedelta '1 day, 16:48:00'
$ python
>>> from uwtools.api.config import get_yaml_config
>>> c = get_yaml_config("b.yaml")
>>> c["raw"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/maddenp/git/uwtools/src/uwtools/config/support.py", line 204, in __repr__
    return "%s %s" % (self.tag, self.converted)
  File "/home/maddenp/git/uwtools/src/uwtools/config/support.py", line 226, in converted
    return dict(zip(UWYAMLConvert.TAGS, converters, strict=True))[self.tag](self.value)
  File "/home/maddenp/git/uwtools/src/uwtools/utils/time.py", line 28, in to_timedelta
    args = dict(zip(keys, map(float, value.split(":")), strict=False))
ValueError: could not convert string to float: '1 day, 16'

The problem is that the value '1 day, 16:48:00' that appears in b.yaml, which is the standard representation of the timedelta object given by calling str() on it, is not parseable by uwtools as a timedelta object.

New behavior:

$ uw config realize -i a.yaml | tee b.yaml
start: 2025-01-01T00:00:00
stop: 2025-01-03T03:00:00
raw: !timedelta '40:48:00'
$ python
>>> from uwtools.api.config import get_yaml_config
>>> c = get_yaml_config("b.yaml")
>>> c["raw"]
!timedelta 1 day, 16:48:00

It's fine for the timedelta object to be internally represented as 1 day, 16:48:00, but when it is serialized (dumped) to YAML, it needs to be in the format that uwtools supports so that it can be read in again later.

After this merges, I'd like to do a v2.12.2 bugfix release of this, too.

Type

  • Bug fix (corrects a known issue)

Impact

  • This is a non-breaking change (existing functionality continues to work as expected)

Checklist

  • I have added myself and any co-authors to the PR's Assignees list.
  • I have reviewed the documentation and have made any updates necessitated by this change.

@maddenp-cu maddenp-cu self-assigned this Jan 8, 2026
@maddenp-cu maddenp-cu marked this pull request as ready for review January 8, 2026 06:07
@maddenp-cu maddenp-cu merged commit b5b031a into ufs-community:main Jan 8, 2026
2 checks passed
@maddenp-cu maddenp-cu deleted the timedelta-tagged-tests branch January 8, 2026 14:14
maddenp-cu added a commit to maddenp-cu/uwtools that referenced this pull request Jan 8, 2026
@maddenp-cu maddenp-cu mentioned this pull request Jan 8, 2026
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants