Skip to content

Support datetime.timedelta for seconds/milliseconds duration encoding (python)#10947

Open
Copilot wants to merge 9 commits into
mainfrom
copilot/add-python-test-case-duration
Open

Support datetime.timedelta for seconds/milliseconds duration encoding (python)#10947
Copilot wants to merge 9 commits into
mainfrom
copilot/add-python-test-case-duration

Conversation

Copilot AI commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Overview:

Other language emitters have fully support this feature and this feature belongs to core features in spec dashboard.

PR description

Adds support in @typespec/http-client-python for SDK users to pass datetime.timedelta for durations encoded as seconds/milliseconds (in addition to the existing ISO8601 support), and extends the encode/duration mock_api test coverage to exercise this.

Changes

Feature: timedelta support for seconds/milliseconds durations

  • Emitter (emitter/src/types.ts): emitBuiltInType now emits seconds/milliseconds DurationKnownEncoding values as a duration type carrying the encode and numeric wireType, alongside the existing ISO8601 branch.
  • Generator (generator/pygen/codegen/models/primitive_types.py): DurationType reads encode + wireType and produces a combined format token duration-{seconds|milliseconds}-{int|float}; ISO8601 retains the legacy duration behavior.
  • Runtime serialization/deserialization (both paths):
    • model_base.py.jinja2 (DPG body model properties): added _serialize_duration plus seconds/milliseconds deserializers and the corresponding _DESERIALIZE_MAPPING_WITHFORMAT entries.
    • 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.timedelta and receive datetime.timedelta back for these encodings, with conversion to/from the numeric wire value handled by the runtime.

Test coverage

  • Added scenario coverage across all three operation groups (query, property, header), now using datetime.timedelta input:
    • milliseconds encodings (int32, float, float64)
    • larger-unit encodings (int32/float seconds and milliseconds)
    • milliseconds arrays
  • Files (sync + async, both flavors):
    • tests/mock_api/azure/test_encode_duration.py, tests/mock_api/azure/asynctests/test_encode_duration_async.py
    • tests/mock_api/unbranded/test_encode_duration.py, tests/mock_api/unbranded/asynctests/test_encode_duration_async.py
  • Property assertions use the correct per-scenario models (e.g. Float64SecondsDurationProperty, Int32MillisecondsLargerUnitDurationProperty) and assert datetime.timedelta on responses.
  • Changelog entry under .chronus/changes (changeKind: feature).

Example of newly-covered cases using datetime.timedelta:

client.query.int32_milliseconds_array(
    input=[datetime.timedelta(milliseconds=36000), datetime.timedelta(milliseconds=47000)]
)
client.header.float_milliseconds_larger_unit(duration=datetime.timedelta(milliseconds=210000))
result = client.property.int32_milliseconds_larger_unit(
    Int32MillisecondsLargerUnitDurationProperty(value=datetime.timedelta(milliseconds=180000))
)
assert result.value == datetime.timedelta(milliseconds=180000)

Testing

  • ✅ Regenerated encode/duration for both azure and unbranded flavors; verified generated models use datetime.timedelta with the new format tokens
  • ✅ mock_api suites pass against the live mock server (azure + unbranded, sync + async — 12 passing)

Copilot AI and others added 2 commits June 10, 2026 09:14
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
…uration

Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:python Issue for the Python client emitter: @typespec/http-client-python label Jun 10, 2026
Copilot finished work on behalf of msyyc June 10, 2026 09:17
Copilot AI requested a review from msyyc June 10, 2026 09:17
@msyyc

msyyc commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

need new feature to support the test.

@msyyc msyyc marked this pull request as ready for review June 10, 2026 09:22
@msyyc msyyc closed this Jun 10, 2026
@msyyc msyyc deleted the copilot/add-python-test-case-duration branch June 10, 2026 09:22
@msyyc msyyc restored the copilot/add-python-test-case-duration branch June 11, 2026 09:22
@msyyc msyyc reopened this Jun 11, 2026
@msyyc msyyc marked this pull request as draft June 11, 2026 09:22
…duration encoding

Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>

Copilot AI commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

need new feature to support the test.

Added in a7edc4d: SDK users can now pass datetime.timedelta for seconds/milliseconds duration encodings, supported end-to-end in the emitter, generator model, and serialization/deserialization logic.

Copilot AI changed the title Add python encode/duration mock_api test coverage Support datetime.timedelta for seconds/milliseconds duration encoding (python) Jun 11, 2026
Copilot finished work on behalf of msyyc June 11, 2026 09:57
Copilot AI requested a review from msyyc June 11, 2026 09:57
@pkg-pr-new

pkg-pr-new Bot commented Jun 12, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-python@10947

commit: 5ee0911

@github-actions

github-actions Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • @typespec/http-client-python
Show changes

@typespec/http-client-python - feature ✏️

Support datetime.timedelta for duration types encoded as seconds or milliseconds. SDK users can now pass a datetime.timedelta (instead of a raw int/float) and responses are deserialized back into datetime.timedelta.

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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 duration with seconds/milliseconds encodings carries encode + numeric wireType, 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.timedelta for 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.

@azure-sdk-automation

Copy link
Copy Markdown

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@msyyc msyyc enabled auto-merge June 12, 2026 07:38
@msyyc

msyyc commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

JialinHuang803 pushed a commit to JialinHuang803/typespec-azure that referenced this pull request Jun 15, 2026
fix CI for microsoft/typespec#10947

## Description

Temporarily comment out the encode duration mock API test cases
(`test_encode_duration.py` and `test_encode_duration_async.py` for both
`azure` and `unbranded`) until duration is fully supported. These test
cases will be reopened once duration support is complete.

## Changes

- Commented out test cases in:
-
`packages/typespec-python/tests/mock_api/azure/test_encode_duration.py`
-
`packages/typespec-python/tests/mock_api/azure/asynctests/test_encode_duration_async.py`
-
`packages/typespec-python/tests/mock_api/unbranded/test_encode_duration.py`
-
`packages/typespec-python/tests/mock_api/unbranded/asynctests/test_encode_duration_async.py`
- Added changelog entry under `.chronus/changes`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:python Issue for the Python client emitter: @typespec/http-client-python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants