Skip to content

fix(_transform): guard against IndexError on bare dict annotation#275

Open
devteamaegis wants to merge 1 commit into
groq:mainfrom
devteamaegis:fix/indexerror-bare-dict
Open

fix(_transform): guard against IndexError on bare dict annotation#275
devteamaegis wants to merge 1 commit into
groq:mainfrom
devteamaegis:fix/indexerror-bare-dict

Conversation

@devteamaegis
Copy link
Copy Markdown

What's broken

Calling transform() or async_transform() with a mapping value typed as a bare dict (no type parameters, e.g. Annotated[dict, PropertyInfo(alias="key")]) raises IndexError. The same crash occurs in the async counterpart _async_transform_recursive.

Repro:

from typing_extensions import Annotated
from groq._utils._transform import transform, PropertyInfo
transform({"key": {"a": 1}}, Annotated[dict, PropertyInfo(alias="key")])
# IndexError: tuple index out of range

Why it happens

get_args(dict) returns an empty tuple. Both _transform_recursive (line 183) and _async_transform_recursive (line 349) unconditionally index [1] without checking whether type args exist.

Fix

Added a bounds check in both functions: if get_args(stripped_type) is empty, return data unchanged. When type args are present the existing dict[K, V] path continues to work as before.

Test

Added test_bare_dict_annotation_no_indexerror which asserts that a mapping passed against a bare dict annotation is returned unchanged (runs for both sync and async paths).

get_args(dict) returns an empty tuple, so the unconditional [1] index
in _transform_recursive (and its async counterpart) raised IndexError
whenever a mapping value was passed against an un-parameterised dict
type annotation. Add a bounds check and fall through to the data as-is
when no type args are present.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant