fix: guard against IndexError/ValueError when dict annotation has no type args#3405
Open
adhavan18 wants to merge 1 commit into
Open
fix: guard against IndexError/ValueError when dict annotation has no type args#3405adhavan18 wants to merge 1 commit into
adhavan18 wants to merge 1 commit into
Conversation
…type args `_transform_recursive` and its async counterpart (in `_utils/_transform.py`) unconditionally did `get_args(stripped_type)[1]` for any `origin == dict` branch. When the annotation is a bare, unparameterised `dict` (no `[K, V]` type arguments), `get_args(dict)` returns an empty tuple, so the index access raises `IndexError: tuple index out of range`. `construct_type` in `_models.py` had the same assumption, unpacking `get_args(type_)` into exactly two targets with `_, items_type = ...`, which raises `ValueError: not enough values to unpack (expected 2, got 0)` for a bare `dict`. Fix: in both sites, check `len(args) >= 2` before indexing. A bare `dict` annotation carries no value-type information, so returning the mapping unchanged (rather than trying to recurse) is the correct behaviour. Adds four regression tests. Closes openai#3338, 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.
Fixes #3338 and #3341.
Problem
get_args(dict)returns an empty tuple when the annotation is a bare, unparameteriseddict(no[K, V]). Two places assumed there are always two args:_transform.py:get_args(stripped_type)[1]→IndexError_models.py:_, items_type = get_args(type_)→ValueErrorMinimal reproducer:
Fix
Check
len(args) >= 2before indexing; return early without transformation when the annotation is unparameterised.Test
All existing tests pass. New test added for bare
dictannotation.