Skip to content

fix(abstractions): raise ValueError for out-of-range timedelta strings - #760

Open
Max Azatian (HardMax71) wants to merge 1 commit into
microsoft:mainfrom
HardMax71:fix/timedelta-overflow-value-error
Open

Max Azatian (HardMax71) wants to merge 1 commit into
microsoft:mainfrom
HardMax71:fix/timedelta-overflow-value-error

Conversation

@HardMax71

Copy link
Copy Markdown
Contributor

Overview

parse_timedelta_string and parse_timedelta_from_iso_format let OverflowError through when a duration is past timedelta.max, like "50001140846:00021" from the issue or "P1000000000D". The callers only catch ValueError. So after #664 the OverflowError still escaped from three places: a typed timedelta property in JSON (it crashed deserialization of the whole response), the form serializer's additional data, and the JSON writer's string check. Both functions now raise ValueError from the OverflowError. These values are handled like any other invalid duration: get_timedelta_value returns None, additional data keeps the raw string, and the writer raises its own "Invalid timedelta string value found" error.

Related Issue

Fixes #480

Notes

The json and form packages get this through abstractions, so it reaches them with the next abstractions release. I didn't touch their microsoft-kiota-abstractions>=1.11.1 lower bound. The text serializer's get_timedelta_value doesn't catch anything, so there it now raises ValueError, as it already does for any other invalid string.

Testing Instructions

  • cd packages/abstractions && pytest tests/test_date_utils.py -k out_of_range: out-of-range ISO durations (one of them parses to float infinity) and an out-of-range hh:mm:ss string raise ValueError.
  • cd packages/serialization/json && pytest -k out_of_range: get_timedelta_value returns None and write_timedelta_value raises "Invalid timedelta string value found for property diff".
  • cd packages/serialization/form && pytest -k out_of_range: get_object_value keeps the value as a string in additional data.
  • All 11 new tests fail on main. Full suites pass on Python 3.10, 3.13 and 3.14: abstractions 150, json 200, form 55. yapf, mypy and pylint (10/10) are clean on all three. isort only flags the import order at the top of date_utils.py, which is the same on main.
  • The repro from my comment on JSON deserialization does not handle OverflowError #480 against this branch: both out-of-range values now give the same results as "not-a-duration".

parse_timedelta_string and parse_timedelta_from_iso_format let OverflowError through when a duration is past timedelta.max, such as "50001140846:00021" or "P1000000000D". The callers only catch ValueError, so a typed timedelta property in JSON, form additional data and the JSON writer's string check raised OverflowError instead of treating the value as invalid. Both functions now raise ValueError from the OverflowError. Fixes microsoft#480.
@sonarqubecloud

sonarqubecloud Bot commented Oct 3, 2026

Copy link
Copy Markdown

@HardMax71

Max Azatian (HardMax71) commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor Author

try-catch-rethrow is tbh quite an antipattern. I'd say in general refactoring of this stuff is needed, but as a separate PR, unrelated to #480

ready for review btw

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

JSON deserialization does not handle OverflowError

1 participant