Commit 19be8db
feat(types): Sequence[T] on response-only list fields for covariant adoption (#635)
* feat(types): Sequence[T] on response-only list fields for covariant adoption
Adopters who subclass SDK response models and narrow the element type
(e.g. list[MyPackage] instead of list[Package]) get mypy[assignment]
errors because list[T] is invariant. Changing affected_packages,
media_buys, packages (on MediaBuy), and media_buy_deliveries to
Sequence[T] removes those errors — Sequence is covariant so
Sequence[MyPackage] is a valid subtype of Sequence[Package].
Response-only fields are safe because adopters receive these, they do
not construct or mutate them. Request-side list fields (packages/
creatives on request types) remain list[T] because adopters call
.append() on those. See issue #624 for the full 4-category analysis.
The rewrite is implemented in post_generate_fixes.py via a new
rewrite_response_list_to_sequence() function so it re-applies on
every codegen run and survives schema regeneration.
Closes #624 (Category A)
https://claude.ai/code/session_019aVaC1DXjVH6Kan4gBe1sb
* fix(types): Sequence consistency in ergonomic coercion + import ordering
Fix two issues surfaced in pre-PR expert review:
1. _ergonomic.py was patching media_buy_deliveries back to list[MediaBuyDelivery]
at import time, overwriting the Sequence[MediaBuyDelivery] annotation set by
post_generate_fixes.py. The generate_ergonomic_coercion.py generator now
detects Sequence[T] fields (via updated is_list_of) and emits Sequence[T]
in the coercion patch, preserving source/runtime consistency. _ergonomic.py
regenerated to reflect this.
2. Import ordering in generated files: from collections.abc import Sequence was
inserted after from enum import Enum. Fixed insertion anchor in
post_generate_fixes.py and corrected the two affected generated files.
https://claude.ai/code/session_019aVaC1DXjVH6Kan4gBe1sb
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent edd7d0a commit 19be8db
6 files changed
Lines changed: 96 additions & 14 deletions
File tree
- scripts
- src/adcp/types
- generated_poc/media_buy
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | | - | |
| 98 | + | |
99 | 99 | | |
100 | | - | |
| 100 | + | |
101 | 101 | | |
102 | | - | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
103 | 107 | | |
104 | | - | |
| 108 | + | |
105 | 109 | | |
106 | 110 | | |
107 | 111 | | |
108 | 112 | | |
109 | | - | |
| 113 | + | |
110 | 114 | | |
111 | 115 | | |
112 | 116 | | |
113 | | - | |
| 117 | + | |
114 | 118 | | |
115 | 119 | | |
116 | 120 | | |
| |||
369 | 373 | | |
370 | 374 | | |
371 | 375 | | |
372 | | - | |
373 | | - | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
374 | 380 | | |
375 | 381 | | |
376 | 382 | | |
377 | 383 | | |
| 384 | + | |
378 | 385 | | |
379 | 386 | | |
380 | 387 | | |
| |||
555 | 562 | | |
556 | 563 | | |
557 | 564 | | |
| 565 | + | |
| 566 | + | |
558 | 567 | | |
559 | | - | |
560 | 568 | | |
561 | 569 | | |
562 | | - | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
563 | 577 | | |
564 | 578 | | |
565 | 579 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
547 | 547 | | |
548 | 548 | | |
549 | 549 | | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
550 | 611 | | |
551 | 612 | | |
552 | 613 | | |
| |||
1151 | 1212 | | |
1152 | 1213 | | |
1153 | 1214 | | |
| 1215 | + | |
1154 | 1216 | | |
1155 | 1217 | | |
1156 | 1218 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
| 36 | + | |
34 | 37 | | |
35 | 38 | | |
36 | 39 | | |
| |||
500 | 503 | | |
501 | 504 | | |
502 | 505 | | |
503 | | - | |
| 506 | + | |
504 | 507 | | |
505 | 508 | | |
506 | 509 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
334 | 335 | | |
335 | 336 | | |
336 | 337 | | |
337 | | - | |
| 338 | + | |
338 | 339 | | |
339 | 340 | | |
340 | 341 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
0 commit comments