fix(_models): guard against bare dict in construct_type()#1620
Open
devteamaegis wants to merge 1 commit into
Open
fix(_models): guard against bare dict in construct_type()#1620devteamaegis wants to merge 1 commit into
devteamaegis wants to merge 1 commit into
Conversation
get_args(dict) returns () for a plain unparameterised dict, causing the unconditional two-value unpack on line 650 to raise ValueError. Guard the unpack with `if args:` and fall back to `object` as the items_type when no type parameters are present. Fixes anthropics#1619
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()withtype_=dict(a bare, unparameterised dict) raisesValueError: not enough values to unpack (expected 2, got 0). This hits whenever a model field is annotated as plaindictrather thanDict[str, SomeType].Why it happens
The
origin == dictbranch unconditionally unpacksget_args(type_)as exactly two values. For a baredictwith no type parameters,get_argsreturns an empty tuple(), making the two-value unpack fail.Fix
Capture
get_args(type_)into a localargsvariable and fall back toobjectasitems_typewhenargsis empty. Whenitems_typeisobject, every value passes throughconstruct_typeunchanged, which is the correct behaviour for an untyped dict.Test
Added
test_construct_type_bare_dictintests/test_models.py— passestype_=dictand asserts the original dict is returned without raising.Fixes #1619