fix(_transform): guard against IndexError on bare dict annotation#275
Open
devteamaegis wants to merge 1 commit into
Open
fix(_transform): guard against IndexError on bare dict annotation#275devteamaegis wants to merge 1 commit into
devteamaegis wants to merge 1 commit into
Conversation
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.
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
Calling
transform()orasync_transform()with a mapping value typed as a baredict(no type parameters, e.g.Annotated[dict, PropertyInfo(alias="key")]) raisesIndexError. The same crash occurs in the async counterpart_async_transform_recursive.Repro:
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, returndataunchanged. When type args are present the existingdict[K, V]path continues to work as before.Test
Added
test_bare_dict_annotation_no_indexerrorwhich asserts that a mapping passed against a baredictannotation is returned unchanged (runs for both sync and async paths).