fix(abstractions): raise ValueError for out-of-range timedelta strings - #760
Conversation
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.
|
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 |
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The exception normalization is correctly scoped and covered across affected serializer paths.
Review effort: Balanced
Findings: None
What changed in this PR
Normalizes out-of-range duration parsing to ValueError, allowing serializers to handle invalid values consistently.
Changes:
- Converts
timedeltaoverflow failures intoValueError. - Adds coverage for ISO and
hh:mm:ssoverflows. - Verifies JSON and form serializer behavior.
| File | Description |
|---|---|
packages/abstractions/kiota_abstractions/date_utils.py |
Normalizes overflow exceptions. |
packages/abstractions/tests/test_date_utils.py |
Tests out-of-range durations. |
packages/serialization/json/tests/unit/test_json_parse_node.py |
Tests JSON parsing fallback. |
packages/serialization/json/tests/unit/test_json_serialization_writer.py |
Tests writer validation errors. |
packages/serialization/form/tests/unit/test_form_parse_node.py |
Tests raw-string preservation. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Vincent Biret (baywet)
left a comment
There was a problem hiding this comment.
Thanks for the contribution!
|



Overview
parse_timedelta_stringandparse_timedelta_from_iso_formatlet OverflowError through when a duration is pasttimedelta.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_valuereturns 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.1lower bound. The text serializer'sget_timedelta_valuedoesn'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_valuereturns None andwrite_timedelta_valueraises "Invalid timedelta string value found for property diff".cd packages/serialization/form && pytest -k out_of_range:get_object_valuekeeps the value as a string in additional data.