fix(client): handle bare dict/list annotations in construct_type and transform#1672
Open
Zawwarsami16 wants to merge 2 commits into
Open
fix(client): handle bare dict/list annotations in construct_type and transform#1672Zawwarsami16 wants to merge 2 commits into
Zawwarsami16 wants to merge 2 commits into
Conversation
get_args() returns an empty tuple for an unparameterized `dict` or `list` annotation, so the dict branch's two-value unpack raised ValueError and the list branch's args[0] raised IndexError. Guard both and fall back to `object` as the element type so values pass through unchanged instead of crashing. Fixes anthropics#1619, anthropics#1626
A bare `dict` annotation (no type parameters) made both the sync and async transform helpers index get_args(...)[1] on an empty tuple, raising IndexError. Guard the lookup and fall back to `object` so the mapping is passed through unchanged. Fixes anthropics#1628
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A field annotated with a bare, unparameterized
dictorlist(rather thanDict[K, V]/List[T]) crashes the runtime helpers, becauseget_args()returns an empty tuple in that case and the code indexes into it unconditionally.Three reported crashes, one root cause:
construct_type(value={...}, type_=dict)→ValueError: not enough values to unpack (expected 2, got 0)(_models.py) — BUG: construct_type() crashes with ValueError when type_ is a bare dict #1619construct_type(value=[...], type_=list)→IndexError: tuple index out of range(_models.py) — BUG: construct_type() raises IndexError when called with bare unparameterized list #1626transform({...}, dict)→IndexError: tuple index out of range, in both the sync and async paths (_transform.py) — IndexError on bare (unparameterized) dict annotation in _transform_recursive #1628Fix
Guard each
get_args()lookup and fall back toobjectas the element type when no type parameters are present. Withobjectas the element type, values pass straight through unchanged — which is the right behavior for an unparameterized container, since there's no inner type to coerce to.Repro (before)
All three (plus the async transform path) now return the input unchanged.
Tests
tests/test_models.py:test_bare_dict_annotation,test_bare_list_annotationtests/test_transform.py:test_bare_dict_annotation(parametrized sync + async)Full
test_models.pyandtest_transform.pysuites pass locally (122 tests); ruff check + format are clean.