fix(_models): guard against empty get_args() on bare dict annotation#3342
Open
devteamaegis wants to merge 1 commit into
Conversation
construct_type() unpacked get_args(type_) with two targets directly after detecting origin == dict. When type_ is the bare unparameterised dict class, get_args(dict) returns () and the unpack raised ValueError. Add a guard that returns the value as-is when no type arguments are present. Fixes openai#3341
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
construct_type()insrc/openai/_models.pycrashes withValueError: not enough values to unpack (expected 2, got 0)when the field type is a bare, unparameteriseddictwith no type arguments. The code does_, items_type = get_args(type_)unconditionally after detectingorigin == dict, butget_args(dict)returns an empty tuple()for the bare class.Why it happens
The guard only checks
origin == dict; it never verifies thatget_argsactually returned two arguments before unpacking.Fix
Added a two-line guard: if
get_args(type_)returns an empty tuple (baredict), return the mapping value as-is instead of attempting the unpack. The parameterisedDict[K, V]path is unchanged.Test
Added
test_construct_type_bare_dict_annotationtotests/test_models.py. It callsconstruct_type(value={"key": "value"}, type_=dict)and asserts the result equals the input dict without raising.Fixes #3341