fix(models): handle bare dict/list type annotations without type parameters#3360
Open
tianming-1996 wants to merge 1 commit into
Open
fix(models): handle bare dict/list type annotations without type parameters#3360tianming-1996 wants to merge 1 commit into
tianming-1996 wants to merge 1 commit into
Conversation
…meters When a TypedDict field is annotated with bare 'dict' or 'list' (without type parameters like dict[str, str] or list[int]), calling transform() or construct_type() would crash with IndexError or ValueError. Fixes openai#3338: IndexError in _transform_recursive when TypedDict field uses bare dict annotation Fixes openai#3341: construct_type() crashes with ValueError on bare dict annotation Also fixes: bare list annotation causing extract_type_arg() to fail Changes: - _transform.py: Add args length check in dict branch, handle bare list in list/iterable/sequence branches for both sync and async versions - _models.py: Add args length check in dict branch, handle bare list in list branch - Add regression tests for bare dict and bare list annotations Signed-off-by: tianming-1996 <tianming@example.com>
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
IndexErrorin_transform_recursivewhen TypedDict fields use baredictannotations (e.g.metadata: dictinstead ofmetadata: dict[str, str])ValueErrorinconstruct_type()when deserializing baredictannotationsRuntimeErrorfromextract_type_arg()for barelistannotations in transform_async_transform_recursive)Changes
src/openai/_utils/_transform.pyget_args()length check in dict branch; handle bare list in list/iterable/sequence branches (sync + async)src/openai/_models.pyget_args()length check in dict branch; handle bare list in list branchtests/test_transform.pytests/test_models.pyconstruct_type()with bare dict/listRoot Cause
All four bugs share the same root cause: when a type like
dictorlistis used without generic parameters,typing.get_args()returns an empty tuple(). Code that unconditionally indexed into this tuple (e.g.get_args(type_)[1]) or unpacked it (e.g._, items_type = get_args(type_)) would crash.Test Plan
Fixes #3338
Fixes #3341