Skip to content

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

Merged
Vincent Biret (baywet) merged 2 commits into
microsoft:mainfrom
HardMax71:fix/timedelta-overflow-value-error
Oct 5, 2026
Merged

Vincent Biret (baywet) merged 2 commits 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.
@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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 timedelta overflow failures into ValueError.
  • Adds coverage for ISO and hh:mm:ss overflows.
  • 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.

@baywet Vincent Biret (baywet) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

@baywet
Vincent Biret (baywet) enabled auto-merge (squash) October 5, 2026 12:35
@sonarqubecloud

sonarqubecloud Bot commented Oct 5, 2026

Copy link
Copy Markdown

@baywet
Vincent Biret (baywet) merged commit 2658939 into microsoft:main Oct 5, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done ✔️

Development

Successfully merging this pull request may close these issues.

JSON deserialization does not handle OverflowError

3 participants