FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(afc): guard type argument unpacking for unsubscripted typing.List and typing.Dict in convert_if_exist_pydantic_model by chauvuusvn · Pull Request #3045 · googleapis/python-genai · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
26 changes: 16 additions & 10 deletions google/genai/_extra_utils.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,23 @@ def convert_if_exist_pydantic_model(
f' {annotation}, due to error {e}'
)
if isinstance(value, list) and get_origin(annotation) == list:
item_type = get_args(annotation)[0]
return [
convert_if_exist_pydantic_model(item, item_type, param_name, func_name)
for item in value
]
args = get_args(annotation)
if args:
item_type = args[0]
return [
convert_if_exist_pydantic_model(item, item_type, param_name, func_name)
for item in value
]
return value
if isinstance(value, dict) and get_origin(annotation) == dict:
_, value_type = get_args(annotation)
return {
k: convert_if_exist_pydantic_model(v, value_type, param_name, func_name)
for k, v in value.items()
}
args = get_args(annotation)
if len(args) == 2:
_, value_type = args
return {
k: convert_if_exist_pydantic_model(v, value_type, param_name, func_name)
for k, v in value.items()
}
return value
# example 1: typing.Union[int, float]
# example 2: int | float equivalent to UnionType[int, float]
if get_origin(annotation) in (Union, UnionType):
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""Test convert_if_exist_pydantic_model."""

import inspect
from typing import Optional, Union
from typing import Any, Dict, List, Optional, Union
import pydantic
import pytest
import sys
Expand All @@ -42,9 +42,15 @@ def test_builtin_types():
assert convert_if_exist_pydantic_model(
[1], list, 'param_name', 'func_name'
) == [1]
assert convert_if_exist_pydantic_model(
[1, 2], List, 'param_name', 'func_name'
) == [1, 2]
assert convert_if_exist_pydantic_model(
{'key1': 1}, dict, 'param_name', 'func_name'
) == {'key1': 1}
assert convert_if_exist_pydantic_model(
{'key1': 1}, Dict, 'param_name', 'func_name'
) == {'key1': 1}
assert convert_if_exist_pydantic_model(
{'key1': 1, 'key2': 2}, dict[str, int], 'param_name', 'func_name'
) == {'key1': 1, 'key2': 2}
Expand Down

Back | FazBrowse Home | New Git URL