Skip to content

Commit f23216f

Browse files
committed
Add a clearer type for the flatten functions.
1 parent c4a7d12 commit f23216f

File tree

1 file changed

+22
-9
lines changed
  • instrumentation-genai/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai

1 file changed

+22
-9
lines changed

instrumentation-genai/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/dict_util.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import logging
1616
import json
17-
from typing import Any, Callable, Dict, Optional, Sequence, Set, Tuple, Union
17+
from typing import Any, Callable, Dict, Optional, Protocol, Sequence, Set, Tuple, Union
1818

1919
Primitive = Union[bool, str, int, float]
2020
BoolList = list[bool]
@@ -26,6 +26,19 @@
2626
FlattenedDict = Dict[str, FlattenedValue]
2727

2828

29+
class FlattenFunc(Protocol):
30+
31+
def __call__(
32+
self,
33+
key: str,
34+
value: Any,
35+
exclude_keys: Set[str],
36+
rename_keys: Dict[str, str],
37+
flatten_functions: Dict[str, "FlattenFunc"],
38+
**kwargs: Any) -> Any:
39+
return None
40+
41+
2942
_logger = logging.getLogger(__name__)
3043

3144

@@ -57,8 +70,8 @@ def _is_homogenous_primitive_list(v):
5770

5871

5972
def _get_flatten_func(
60-
flatten_functions: Dict[str, Callable], key_names: set[str]
61-
):
73+
flatten_functions: Dict[str, FlattenFunc], key_names: set[str]
74+
) -> Optional[FlattenFunc]:
6275
for key in key_names:
6376
flatten_func = flatten_functions.get(key)
6477
if flatten_func is not None:
@@ -71,7 +84,7 @@ def _flatten_with_flatten_func(
7184
value: Any,
7285
exclude_keys: Set[str],
7386
rename_keys: Dict[str, str],
74-
flatten_functions: Dict[str, Callable],
87+
flatten_functions: Dict[str, FlattenFunc],
7588
key_names: Set[str],
7689
) -> Tuple[bool, Any]:
7790
flatten_func = _get_flatten_func(flatten_functions, key_names)
@@ -98,7 +111,7 @@ def _flatten_compound_value(
98111
value: Any,
99112
exclude_keys: Set[str],
100113
rename_keys: Dict[str, str],
101-
flatten_functions: Dict[str, Callable],
114+
flatten_functions: Dict[str, FlattenFunc],
102115
key_names: Set[str],
103116
_from_json=False,
104117
) -> FlattenedDict:
@@ -165,7 +178,7 @@ def _flatten_value(
165178
value: Any,
166179
exclude_keys: Set[str],
167180
rename_keys: Dict[str, str],
168-
flatten_functions: Dict[str, Callable],
181+
flatten_functions: Dict[str, FlattenFunc],
169182
_from_json=False,
170183
) -> FlattenedDict:
171184
if value is None:
@@ -195,7 +208,7 @@ def _flatten_dict(
195208
key_prefix: str,
196209
exclude_keys: Set[str],
197210
rename_keys: Dict[str, str],
198-
flatten_functions: Dict[str, Callable],
211+
flatten_functions: Dict[str, FlattenFunc],
199212
) -> FlattenedDict:
200213
result = {}
201214
for key, value in d.items():
@@ -218,7 +231,7 @@ def _flatten_list(
218231
key_prefix: str,
219232
exclude_keys: Set[str],
220233
rename_keys: Dict[str, str],
221-
flatten_functions: Dict[str, Callable],
234+
flatten_functions: Dict[str, FlattenFunc],
222235
) -> FlattenedDict:
223236
result = {}
224237
result[_concat_key(key_prefix, "length")] = len(lst)
@@ -240,7 +253,7 @@ def flatten_dict(
240253
key_prefix: Optional[str] = None,
241254
exclude_keys: Optional[Sequence[str]] = None,
242255
rename_keys: Optional[Dict[str, str]] = None,
243-
flatten_functions: Optional[Dict[str, Callable]] = None,
256+
flatten_functions: Optional[Dict[str, FlattenFunc]] = None,
244257
):
245258
key_prefix = key_prefix or ""
246259
if exclude_keys is None:

0 commit comments

Comments
 (0)