Support datetime.timedelta for seconds/milliseconds duration encoding (python)#10947
Support datetime.timedelta for seconds/milliseconds duration encoding (python)#10947Copilot wants to merge 9 commits into
Conversation
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
…uration Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
|
need new feature to support the test. |
…duration encoding Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Added in a7edc4d: SDK users can now pass |
commit: |
|
All changed packages have been documented.
Show changes
|
Collapse the seconds/milliseconds (int/float) duration serializers and deserializers behind shared parameterized helpers in model_base and serialization runtime templates. Behavior is unchanged; all encode/duration mock_api tests pass (azure + unbranded, sync + async). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds end-to-end support in @typespec/http-client-python for duration values encoded as numeric seconds/milliseconds, allowing SDK users to pass and receive datetime.timedelta (in addition to the existing ISO8601 duration behavior).
Changes:
- Extend the Python emitter + generator model typing so
durationwithseconds/millisecondsencodings carriesencode+ numericwireType, and maps to new combined format tokens (e.g.duration-seconds-int). - Add runtime serialization/deserialization helpers for the new numeric duration format tokens in both the DPG model runtime (
model_base.py.jinja2) and msrest runtime (serialization.py.jinja2). - Update mock API tests (azure + unbranded, sync + async) to exercise query/property/header duration paths using
datetime.timedeltafor seconds/milliseconds scenarios.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/http-client-python/tests/mock_api/unbranded/test_encode_duration.py | Expands unbranded sync duration tests to use timedelta for seconds/milliseconds query/property/header scenarios. |
| packages/http-client-python/tests/mock_api/unbranded/asynctests/test_encode_duration_async.py | Expands unbranded async duration tests to use timedelta for seconds/milliseconds query/property/header scenarios. |
| packages/http-client-python/tests/mock_api/azure/test_encode_duration.py | Expands azure sync duration tests to use timedelta for seconds/milliseconds query/property/header scenarios. |
| packages/http-client-python/tests/mock_api/azure/asynctests/test_encode_duration_async.py | Expands azure async duration tests to use timedelta for seconds/milliseconds query/property/header scenarios. |
| packages/http-client-python/generator/pygen/codegen/templates/serialization.py.jinja2 | Adds msrest serializer/deserializer support for `duration-*-{int |
| packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 | Adds DPG runtime serializer/deserializer support for numeric duration formats and routes timedelta serialization through format-aware logic. |
| packages/http-client-python/generator/pygen/codegen/models/primitive_types.py | Updates DurationType to interpret encode + wireType and emit combined format tokens for numeric duration encodings. |
| packages/http-client-python/emitter/src/types.ts | Emits encode + wireType metadata for duration when encoded as seconds/milliseconds. |
| .chronus/changes/add-python-duration-seconds-milliseconds-encode-2026-6-10-9-3-37.md | Adds a chronus feature changelog entry for the new timedelta support. |
|
You can try these changes here
|
|
@iscai-msft / @l0lawrence / @tadelesh pls help review and code diff is in Azure/azure-sdk-for-python#47471. You may focus on the api signature change in https://github.com/Azure/azure-sdk-for-python/pull/47471/changes#diff-60dc1ef1c0ec0a97c1bd1e6ecfba30a049b2644327aa9c276be3831b24148882
|

Overview:
Other language emitters have fully support this feature and this feature belongs to
corefeatures in spec dashboard.PR description
Adds support in
@typespec/http-client-pythonfor SDK users to passdatetime.timedeltafor durations encoded asseconds/milliseconds(in addition to the existingISO8601support), and extends theencode/durationmock_api test coverage to exercise this.Changes
Feature: timedelta support for seconds/milliseconds durations
emitter/src/types.ts):emitBuiltInTypenow emitsseconds/millisecondsDurationKnownEncodingvalues as adurationtype carrying theencodeand numericwireType, alongside the existingISO8601branch.generator/pygen/codegen/models/primitive_types.py):DurationTypereadsencode+wireTypeand produces a combined format tokenduration-{seconds|milliseconds}-{int|float};ISO8601retains the legacydurationbehavior.model_base.py.jinja2(DPG body model properties): added_serialize_durationplus seconds/milliseconds deserializers and the corresponding_DESERIALIZE_MAPPING_WITHFORMATentries.serialization.py.jinja2(msrest query/header params): added serialize/deserialize methods for the seconds/milliseconds (int/float) formats, registered in the type maps.This lets users pass
datetime.timedeltaand receivedatetime.timedeltaback for these encodings, with conversion to/from the numeric wire value handled by the runtime.Test coverage
query,property,header), now usingdatetime.timedeltainput:millisecondsencodings (int32,float,float64)larger-unitencodings (int32/floatseconds and milliseconds)millisecondsarraystests/mock_api/azure/test_encode_duration.py,tests/mock_api/azure/asynctests/test_encode_duration_async.pytests/mock_api/unbranded/test_encode_duration.py,tests/mock_api/unbranded/asynctests/test_encode_duration_async.pyFloat64SecondsDurationProperty,Int32MillisecondsLargerUnitDurationProperty) and assertdatetime.timedeltaon responses..chronus/changes(changeKind: feature).Example of newly-covered cases using
datetime.timedelta:Testing
encode/durationfor both azure and unbranded flavors; verified generated models usedatetime.timedeltawith the new format tokens