fix(_models): guard against IndexError in construct_type for bare list#1627
Open
devteamaegis wants to merge 1 commit into
Open
fix(_models): guard against IndexError in construct_type for bare list#1627devteamaegis wants to merge 1 commit into
devteamaegis wants to merge 1 commit into
Conversation
get_args(list) returns () when no type parameter is present, causing args[0] to raise IndexError. Guard with `args[0] if args else object` so bare list falls through to identity behavior. Fixes anthropics#1626
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's broken
Calling
construct_type(value=[...], type_=list)with a bare, unparameterizedlistraisesIndexError: tuple index out of range. Theorigin == listbranch at line 671 ofsrc/anthropic/_models.pyunconditionally executesinner_type = args[0], butget_args(list)returns an empty tuple when no type parameter is present.Why it happens
get_origin(list)returnslist(matching the branch), butget_args(list)returns()rather than a one-element tuple, soargs[0]is always out of range for unparameterized barelist.Fix
Changed
inner_type = args[0]toinner_type = args[0] if args else object. When no type parameter is present, elements are returned unchanged (identity viaconstruct_type(value=entry, type_=object)), which is the correct safe fallback.Test
Added
test_construct_type_bare_listintests/test_models.pyconfirming thatconstruct_type(value=["a", "b", "c"], type_=list)returns["a", "b", "c"]without raising.Fixes #1626