-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathutils.py
More file actions
268 lines (211 loc) · 9.17 KB
/
utils.py
File metadata and controls
268 lines (211 loc) · 9.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from __future__ import annotations
import functools
import warnings
from collections.abc import Callable
from typing import Any, TypeVar
from pydantic import BaseModel
from .connection import Connection
__all__ = [
"ensure_dict",
"normalize_result",
"notify_model",
"request_model",
"request_model_from_dict",
"request_optional_model",
"serialize_params",
"validate_model",
"validate_model_from_dict",
"validate_optional_model",
]
ModelT = TypeVar("ModelT", bound=BaseModel)
MethodT = TypeVar("MethodT", bound=Callable)
ClassT = TypeVar("ClassT", bound=type)
T = TypeVar("T")
MultiParamModelSpec = tuple[type[BaseModel], ...]
def _param_models_name(models: MultiParamModelSpec) -> str:
return " | ".join(model_type.__name__ for model_type in models)
def _param_models_field_names(models: MultiParamModelSpec) -> tuple[str, ...]:
shared_fields = set(models[0].model_fields)
for model_type in models[1:]:
shared_fields &= set(model_type.model_fields)
return tuple(field_name for field_name in models[0].model_fields if field_name in shared_fields)
def model_to_kwargs(model_obj: BaseModel, models: MultiParamModelSpec) -> dict[str, Any]:
kwargs = {
field_name: getattr(model_obj, field_name)
for field_name in _param_models_field_names(models)
if field_name != "field_meta"
}
if meta := getattr(model_obj, "field_meta", None):
kwargs.update(meta)
return kwargs
def serialize_params(params: BaseModel) -> dict[str, Any]:
"""Return a JSON-serializable representation used for RPC calls."""
return params.model_dump(by_alias=True, exclude_none=True, exclude_defaults=True)
def normalize_result(payload: Any) -> dict[str, Any]:
"""Convert optional BaseModel/None responses into JSON-friendly payloads."""
if payload is None:
return {}
if isinstance(payload, BaseModel):
return serialize_params(payload)
return payload
def ensure_dict(payload: Any) -> dict[str, Any]:
"""Return payload when it is a dict, otherwise an empty dict."""
return payload if isinstance(payload, dict) else {}
def validate_model(payload: Any, model_type: type[ModelT]) -> ModelT:
"""Validate payload using the provided Pydantic model."""
return model_type.model_validate(payload)
def validate_model_from_dict(payload: Any, model_type: type[ModelT]) -> ModelT:
"""Validate payload, coercing non-dict values to an empty dict first."""
return model_type.model_validate(ensure_dict(payload))
def validate_optional_model(payload: Any, model_type: type[ModelT]) -> ModelT | None:
"""Validate payload when it is a dict, otherwise return None."""
if isinstance(payload, dict):
return model_type.model_validate(payload)
return None
async def request_model(
conn: Connection,
method: str,
params: BaseModel,
response_model: type[ModelT],
) -> ModelT:
"""Send a request with serialized params and validate the response."""
response = await conn.send_request(method, serialize_params(params))
return validate_model(response, response_model)
async def request_model_from_dict(
conn: Connection,
method: str,
params: BaseModel,
response_model: type[ModelT],
) -> ModelT:
"""Send a request and validate the response, coercing non-dict payloads."""
response = await conn.send_request(method, serialize_params(params))
return validate_model_from_dict(response, response_model)
async def request_optional_model(
conn: Connection,
method: str,
params: BaseModel,
response_model: type[ModelT],
) -> ModelT | None:
"""Send a request and validate optional dict responses."""
response = await conn.send_request(method, serialize_params(params))
return validate_optional_model(response, response_model)
async def notify_model(conn: Connection, method: str, params: BaseModel) -> None:
"""Send a notification with serialized params."""
await conn.send_notification(method, serialize_params(params))
def param_model(param_cls: type[BaseModel]) -> Callable[[MethodT], MethodT]:
"""Decorator to map the method parameters to a Pydantic model.
It is just a marker and does nothing at runtime.
"""
def decorator(func: MethodT) -> MethodT:
func.__param_model__ = param_cls # type: ignore[attr-defined]
return func
return decorator
def param_models(*param_cls: type[BaseModel]) -> Callable[[MethodT], MethodT]:
"""Decorator to mark a method as accepting multiple legacy parameter models."""
if not param_cls:
raise ValueError("param_models() requires at least one model class")
def decorator(func: MethodT) -> MethodT:
func.__param_models__ = param_cls # type: ignore[attr-defined]
return func
return decorator
def to_camel_case(snake_str: str) -> str:
"""Convert snake_case strings to camelCase."""
components = snake_str.split("_")
return components[0] + "".join(x.title() for x in components[1:])
def _make_legacy_func(func: Callable[..., T], model: type[BaseModel]) -> Callable[[Any, BaseModel], T]:
@functools.wraps(func)
def wrapped(self, params: BaseModel) -> T:
warnings.warn(
f"Calling {func.__name__} with {model.__name__} parameter is " # type: ignore[attr-defined]
"deprecated, please update to the new API style.",
DeprecationWarning,
stacklevel=3,
)
kwargs = {
field_name: getattr(params, field_name) for field_name in model.model_fields if field_name != "field_meta"
}
if meta := getattr(params, "field_meta", None):
kwargs.update(meta)
return func(self, **kwargs) # type: ignore[arg-type]
return wrapped
def _make_compatible_func(func: Callable[..., T], model: type[BaseModel]) -> Callable[..., T]:
@functools.wraps(func)
def wrapped(self, *args: Any, **kwargs: Any) -> T:
param = None
if not kwargs and len(args) == 1:
param = args[0]
elif not args and len(kwargs) == 1:
param = kwargs.get("params")
if isinstance(param, model):
warnings.warn(
f"Calling {func.__name__} with {model.__name__} parameter " # type: ignore[attr-defined]
"is deprecated, please update to the new API style.",
DeprecationWarning,
stacklevel=3,
)
kwargs = {
field_name: getattr(param, field_name)
for field_name in model.model_fields
if field_name != "field_meta"
}
if meta := getattr(param, "field_meta", None):
kwargs.update(meta)
return func(self, **kwargs) # type: ignore[arg-type]
return func(self, *args, **kwargs)
return wrapped
def _make_multi_legacy_func(func: Callable[..., T], models: MultiParamModelSpec) -> Callable[[Any, BaseModel], T]:
model_name = _param_models_name(models)
@functools.wraps(func)
def wrapped(self, params: BaseModel) -> T:
warnings.warn(
f"Calling {func.__name__} with {model_name} parameter is " # type: ignore[attr-defined]
"deprecated, please update to the new API style.",
DeprecationWarning,
stacklevel=3,
)
return func(self, **model_to_kwargs(params, models)) # type: ignore[arg-type]
return wrapped
def _make_multi_compatible_func(func: Callable[..., T], models: MultiParamModelSpec) -> Callable[..., T]:
model_name = _param_models_name(models)
@functools.wraps(func)
def wrapped(self, *args: Any, **kwargs: Any) -> T:
param = None
if not kwargs and len(args) == 1:
param = args[0]
elif not args and len(kwargs) == 1:
param = kwargs.get("params")
if isinstance(param, models):
warnings.warn(
f"Calling {func.__name__} with {model_name} parameter " # type: ignore[attr-defined]
"is deprecated, please update to the new API style.",
DeprecationWarning,
stacklevel=3,
)
return func(self, **model_to_kwargs(param, models)) # type: ignore[arg-type]
return func(self, *args, **kwargs)
return wrapped
def compatible_class(cls: ClassT) -> ClassT:
"""Mark a class as backward compatible with old API style."""
for attr in dir(cls):
func = getattr(cls, attr)
if not callable(func):
continue
model = getattr(func, "__param_model__", None)
models = getattr(func, "__param_models__", None)
if model is None and models is None:
continue
if "_" in attr:
if models is not None:
setattr(cls, to_camel_case(attr), _make_multi_legacy_func(func, models))
else:
if model is None:
continue
setattr(cls, to_camel_case(attr), _make_legacy_func(func, model))
else:
if models is not None:
setattr(cls, attr, _make_multi_compatible_func(func, models))
else:
if model is None:
continue
setattr(cls, attr, _make_compatible_func(func, model))
return cls