fix: guard against bare unparameterized dict/list in construct_type and transform#1694
Open
lavkeshdwivedi wants to merge 1 commit into
Open
fix: guard against bare unparameterized dict/list in construct_type and transform#1694lavkeshdwivedi wants to merge 1 commit into
lavkeshdwivedi wants to merge 1 commit into
Conversation
…nd transform get_args(dict) and get_args(list) return empty tuples when the type has no type parameters. Three code paths assumed at least one/two elements and crashed on bare annotations: - _models.py: `_, items_type = get_args(type_)` → ValueError for bare dict - _models.py: `inner_type = args[0]` → IndexError for bare list - _utils/_transform.py (sync + async): `get_args(stripped_type)[1]` → IndexError for bare dict All three now guard with len checks and fall back to `object` as the item/value type when no type arguments are present, matching the behaviour callers would expect from unparameterized collections. Fixes anthropics#1619, anthropics#1626, anthropics#1628
Author
|
Friendly bump - this has been open since yesterday with no activity. Happy to address any review feedback. |
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.
Summary
Three code paths crash when a model field or transform annotation uses a bare, unparameterized
dictorlisttype (i.e.dictinstead ofDict[str, T], orlistinstead ofList[T]). This can happen when integrating with third-party schemas or when users annotate fields generically without type parameters.get_args(dict)andget_args(list)return empty tuples()for bare types. The affected code assumed at least one or two elements were always present:src/anthropic/_models.py_, items_type = get_args(type_)ValueError: not enough values to unpacksrc/anthropic/_models.pyinner_type = args[0]IndexError: tuple index out of rangesrc/anthropic/_utils/_transform.pyget_args(stripped_type)[1]IndexError: tuple index out of rangeFix
Guard each site with a length check and fall back to
objectas the element/value type when no type arguments are present. This matches the behaviour a caller would expect from an unparameterized collection.Tests
test_construct_type_bare_dict_does_not_crash— previously raisedValueErrortest_construct_type_bare_list_does_not_crash— previously raisedIndexErrortest_bare_dict_annotation_does_not_crash(sync + async) — previously raisedIndexErrorCloses
Fixes #1619, #1626, #1628