Skip to content

Commit ff153f4

Browse files
committed
Move standalone utility functions to top of file
1 parent 671aee0 commit ff153f4

File tree

2 files changed

+103
-125
lines changed

2 files changed

+103
-125
lines changed

.github/actions/spelling/allow.txt

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
1-
ACMRTUXB
21
ACard
32
AClient
3+
ACMRTUXB
4+
aconnect
5+
adk
46
AError
57
AFast
8+
agentic
69
AGrpc
10+
aio
11+
aiomysql
12+
amannn
13+
aproject
714
ARequest
815
ARun
916
AServer
1017
AServers
1118
AService
1219
AStarlette
1320
AUser
14-
DSNs
15-
EUR
16-
GBP
17-
GVsb
18-
INR
19-
JPY
20-
JSONRPCt
21-
JWS
22-
Llm
23-
POSTGRES
24-
RUF
25-
SLF
26-
Tful
27-
aconnect
28-
adk
29-
agentic
30-
aio
31-
aiomysql
32-
amannn
33-
aproject
3421
autouse
3522
backticks
3623
cla
@@ -40,23 +27,32 @@ codegen
4027
coro
4128
datamodel
4229
drivername
30+
DSNs
4331
dunders
4432
euo
33+
EUR
4534
excinfo
4635
fernet
4736
fetchrow
4837
fetchval
38+
GBP
4939
genai
5040
getkwargs
5141
gle
42+
GVsb
5243
ietf
5344
initdb
5445
inmemory
46+
INR
5547
isready
48+
JPY
49+
JSONRPCt
50+
JWS
5651
kwarg
5752
langgraph
5853
lifecycles
5954
linting
55+
Llm
6056
lstrips
6157
mikeas
6258
mockurl
@@ -66,6 +62,7 @@ oidc
6662
opensource
6763
otherurl
6864
postgres
65+
POSTGRES
6966
postgresql
7067
protoc
7168
pyi
@@ -75,10 +72,13 @@ pyversions
7572
redef
7673
respx
7774
resub
75+
RUF
76+
SLF
7877
socio
7978
sse
8079
tagwords
8180
taskupdate
8281
testuuid
82+
Tful
8383
typeerror
8484
vulnz

src/a2a/utils/proto_utils.py

Lines changed: 82 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,88 @@ def dict_to_struct(dictionary: dict[str, Any]) -> struct_pb2.Struct:
4646
return struct
4747

4848

49+
def make_dict_serializable(value: Any) -> Any:
50+
"""Dict pre-processing utility: converts non-serializable values to serializable form.
51+
52+
Use this when you want to normalize a dictionary before dict->Struct conversion.
53+
54+
Args:
55+
value: The value to convert.
56+
57+
Returns:
58+
A serializable value.
59+
"""
60+
if isinstance(value, dict):
61+
return {k: make_dict_serializable(v) for k, v in value.items()}
62+
if isinstance(value, list | tuple):
63+
return [make_dict_serializable(item) for item in value]
64+
if isinstance(value, str | int | float | bool) or value is None:
65+
return value
66+
return str(value)
67+
68+
69+
def normalize_large_integers_to_strings(
70+
value: Any, max_safe_digits: int = 15
71+
) -> Any:
72+
"""Integer preprocessing utility: converts large integers to strings.
73+
74+
Use this when you want to convert large integers to strings considering
75+
JavaScript's MAX_SAFE_INTEGER (2^53 - 1) limitation.
76+
77+
Args:
78+
value: The value to convert.
79+
max_safe_digits: Maximum safe integer digits (default: 15).
80+
81+
Returns:
82+
A normalized value.
83+
"""
84+
if isinstance(value, dict):
85+
return {
86+
k: normalize_large_integers_to_strings(v, max_safe_digits)
87+
for k, v in value.items()
88+
}
89+
if isinstance(value, list | tuple):
90+
return [
91+
normalize_large_integers_to_strings(item, max_safe_digits)
92+
for item in value
93+
]
94+
if isinstance(value, int) and abs(value) > (10**max_safe_digits - 1):
95+
return str(value)
96+
return value
97+
98+
99+
def parse_string_integers_in_dict(value: Any, max_safe_digits: int = 15) -> Any:
100+
"""String post-processing utility: converts large integer strings back to integers.
101+
102+
Use this when you want to restore large integer strings to integers
103+
after Struct->dict conversion.
104+
105+
Args:
106+
value: The value to convert.
107+
max_safe_digits: Maximum safe integer digits (default: 15).
108+
109+
Returns:
110+
A parsed value.
111+
"""
112+
if isinstance(value, dict):
113+
return {
114+
k: parse_string_integers_in_dict(v, max_safe_digits)
115+
for k, v in value.items()
116+
}
117+
if isinstance(value, list | tuple):
118+
return [
119+
parse_string_integers_in_dict(item, max_safe_digits)
120+
for item in value
121+
]
122+
if (
123+
isinstance(value, str)
124+
and value.lstrip('-').isdigit()
125+
and len(value.lstrip('-')) > max_safe_digits
126+
):
127+
return int(value)
128+
return value
129+
130+
49131
class ToProto:
50132
"""Converts Python types to proto types."""
51133

@@ -980,107 +1062,3 @@ def role(cls, role: a2a_pb2.Role) -> types.Role:
9801062
return types.Role.agent
9811063
case _:
9821064
return types.Role.agent
983-
984-
985-
def dict_to_struct(dictionary: dict[str, Any]) -> struct_pb2.Struct:
986-
"""Converts a Python dict to a Struct proto.
987-
988-
Unfortunately, using `json_format.ParseDict` does not work because this
989-
wants the dictionary to be an exact match of the Struct proto with fields
990-
and keys and values, not the traditional Python dict structure.
991-
992-
Args:
993-
dictionary: The Python dict to convert.
994-
995-
Returns:
996-
The Struct proto.
997-
"""
998-
struct = struct_pb2.Struct()
999-
for key, val in dictionary.items():
1000-
if isinstance(val, dict):
1001-
struct[key] = dict_to_struct(val)
1002-
else:
1003-
struct[key] = val
1004-
return struct
1005-
1006-
1007-
def make_dict_serializable(value: Any) -> Any:
1008-
"""Dict preprocessing utility: converts non-serializable values to serializable form.
1009-
1010-
Use this when you want to normalize a dictionary before dict->Struct conversion.
1011-
1012-
Args:
1013-
value: The value to convert.
1014-
1015-
Returns:
1016-
A serializable value.
1017-
"""
1018-
if isinstance(value, dict):
1019-
return {k: make_dict_serializable(v) for k, v in value.items()}
1020-
if isinstance(value, list | tuple):
1021-
return [make_dict_serializable(item) for item in value]
1022-
if isinstance(value, str | int | float | bool) or value is None:
1023-
return value
1024-
return str(value)
1025-
1026-
1027-
def normalize_large_integers_to_strings(
1028-
value: Any, max_safe_digits: int = 15
1029-
) -> Any:
1030-
"""Integer preprocessing utility: converts large integers to strings.
1031-
1032-
Use this when you want to convert large integers to strings considering
1033-
JavaScript's MAX_SAFE_INTEGER (2^53 - 1) limitation.
1034-
1035-
Args:
1036-
value: The value to convert.
1037-
max_safe_digits: Maximum safe integer digits (default: 15).
1038-
1039-
Returns:
1040-
A normalized value.
1041-
"""
1042-
if isinstance(value, dict):
1043-
return {
1044-
k: normalize_large_integers_to_strings(v, max_safe_digits)
1045-
for k, v in value.items()
1046-
}
1047-
if isinstance(value, list | tuple):
1048-
return [
1049-
normalize_large_integers_to_strings(item, max_safe_digits)
1050-
for item in value
1051-
]
1052-
if isinstance(value, int) and abs(value) > (10**max_safe_digits - 1):
1053-
return str(value)
1054-
return value
1055-
1056-
1057-
def parse_string_integers_in_dict(value: Any, max_safe_digits: int = 15) -> Any:
1058-
"""String post-processing utility: converts large integer strings back to integers.
1059-
1060-
Use this when you want to restore large integer strings to integers
1061-
after Struct->dict conversion.
1062-
1063-
Args:
1064-
value: The value to convert.
1065-
max_safe_digits: Maximum safe integer digits (default: 15).
1066-
1067-
Returns:
1068-
A parsed value.
1069-
"""
1070-
if isinstance(value, dict):
1071-
return {
1072-
k: parse_string_integers_in_dict(v, max_safe_digits)
1073-
for k, v in value.items()
1074-
}
1075-
if isinstance(value, list | tuple):
1076-
return [
1077-
parse_string_integers_in_dict(item, max_safe_digits)
1078-
for item in value
1079-
]
1080-
if (
1081-
isinstance(value, str)
1082-
and value.lstrip('-').isdigit()
1083-
and len(value.lstrip('-')) > max_safe_digits
1084-
):
1085-
return int(value)
1086-
return value

0 commit comments

Comments
 (0)