fix: handle bare dict annotation in construct_type and _transform_recursive - #3625
Open
Xsidz wants to merge 1 commit into
Open
fix: handle bare dict annotation in construct_type and _transform_recursive#3625Xsidz wants to merge 1 commit into
Xsidz wants to merge 1 commit into
Conversation
…ursive Fixes openai#3341 and openai#3338: when a field is annotated with plain `dict` (no type parameters), `get_args(dict)` returns `()`. The old code unpacked it with `_, items_type = get_args(type_)` / `get_args(stripped_type)[1]`, raising ValueError / IndexError. Guard with a length check and fall back to `Any` for unparameterised dicts.
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
Fixes #3341 and #3338.
construct_type()in_models.pyand_transform_recursive()/_async_transform_recursive()in_utils/_transform.pyall crashed when a field was annotated with a bare, unparameteriseddict(no[K, V]type arguments):_models.py:660:_, items_type = get_args(type_)—get_args(dict)returns(), so the unpack raisesValueError: not enough values to unpack (expected 2, got 0)._transform.py:183and_transform.py:349:get_args(stripped_type)[1]— same empty tuple, raisesIndexError.The fix guards each site with a length check and falls back to
Anywhen the dict is unparameterised, which preserves the existing value pass-through behaviour.Test plan
test_construct_type_bare_dictintests/test_models.py— reproduces the exact crash from issue BUG: construct_type() crashes with ValueError on bare dict annotation (no type args) #3341.test_bare_dict_annotationintests/test_transform.py— reproduces the exact crash from issue BUG: IndexError in _transform_recursive when TypedDict field uses bare dict annotation #3338, covers both sync and async paths via the@parametrizefixture.tests/test_models.pyandtests/test_transform.pysuite: 117 passed, 0 failures.