Commit c8b8194
Support datetime.timedelta for seconds/milliseconds duration encoding (python) (#10947)
# 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`:
```python
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)
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Co-authored-by: Yuchao Yan <yuchaoyan@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 8494a66 commit c8b8194
9 files changed
Lines changed: 504 additions & 74 deletions
File tree
- .chronus/changes
- packages/http-client-python
- emitter/src
- generator/pygen/codegen
- models
- templates
- tests/mock_api
- azure
- asynctests
- unbranded
- asynctests
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
479 | 479 | | |
480 | 480 | | |
481 | 481 | | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
482 | 489 | | |
483 | 490 | | |
484 | 491 | | |
| |||
Lines changed: 15 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
522 | 522 | | |
523 | 523 | | |
524 | 524 | | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
525 | 539 | | |
526 | | - | |
| 540 | + | |
527 | 541 | | |
528 | 542 | | |
529 | 543 | | |
| |||
Lines changed: 34 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
115 | 138 | | |
116 | 139 | | |
117 | 140 | | |
| |||
307 | 330 | | |
308 | 331 | | |
309 | 332 | | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
310 | 339 | | |
311 | 340 | | |
312 | 341 | | |
| |||
336 | 365 | | |
337 | 366 | | |
338 | 367 | | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
339 | 372 | | |
340 | 373 | | |
341 | 374 | | |
| |||
576 | 609 | | |
577 | 610 | | |
578 | 611 | | |
579 | | - | |
| 612 | + | |
580 | 613 | | |
581 | 614 | | |
582 | 615 | | |
| |||
Lines changed: 109 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
516 | 516 | | |
517 | 517 | | |
518 | 518 | | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
519 | 523 | | |
520 | 524 | | |
521 | 525 | | |
| |||
1105 | 1109 | | |
1106 | 1110 | | |
1107 | 1111 | | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
1108 | 1167 | | |
1109 | 1168 | | |
1110 | 1169 | | |
| |||
1377 | 1436 | | |
1378 | 1437 | | |
1379 | 1438 | | |
| 1439 | + | |
| 1440 | + | |
| 1441 | + | |
| 1442 | + | |
1380 | 1443 | | |
1381 | 1444 | | |
1382 | 1445 | | |
| |||
1389 | 1452 | | |
1390 | 1453 | | |
1391 | 1454 | | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
1392 | 1459 | | |
1393 | 1460 | | |
1394 | 1461 | | |
| |||
1950 | 2017 | | |
1951 | 2018 | | |
1952 | 2019 | | |
| 2020 | + | |
| 2021 | + | |
| 2022 | + | |
| 2023 | + | |
| 2024 | + | |
| 2025 | + | |
| 2026 | + | |
| 2027 | + | |
| 2028 | + | |
| 2029 | + | |
| 2030 | + | |
| 2031 | + | |
| 2032 | + | |
| 2033 | + | |
| 2034 | + | |
| 2035 | + | |
| 2036 | + | |
| 2037 | + | |
| 2038 | + | |
| 2039 | + | |
| 2040 | + | |
| 2041 | + | |
| 2042 | + | |
| 2043 | + | |
| 2044 | + | |
| 2045 | + | |
| 2046 | + | |
| 2047 | + | |
| 2048 | + | |
| 2049 | + | |
| 2050 | + | |
| 2051 | + | |
| 2052 | + | |
| 2053 | + | |
| 2054 | + | |
| 2055 | + | |
| 2056 | + | |
| 2057 | + | |
| 2058 | + | |
| 2059 | + | |
| 2060 | + | |
| 2061 | + | |
1953 | 2062 | | |
1954 | 2063 | | |
1955 | 2064 | | |
| |||
Lines changed: 84 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
16 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
17 | 26 | | |
18 | 27 | | |
19 | 28 | | |
| |||
27 | 36 | | |
28 | 37 | | |
29 | 38 | | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
34 | 53 | | |
35 | 54 | | |
36 | 55 | | |
| |||
43 | 62 | | |
44 | 63 | | |
45 | 64 | | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
55 | 111 | | |
56 | 112 | | |
57 | 113 | | |
58 | 114 | | |
59 | 115 | | |
60 | 116 | | |
61 | 117 | | |
62 | | - | |
63 | | - | |
64 | | - | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
0 commit comments