Skip to content

Commit e45afd9

Browse files
committed
@GitHK review: validate returns
1 parent 3aff143 commit e45afd9

File tree

4 files changed

+45
-69
lines changed

4 files changed

+45
-69
lines changed

packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/webserver/v1/api_keys.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
"""API Keys RPC API subclient."""
22

3-
from typing import cast
4-
53
from models_library.basic_types import IDStr
64
from models_library.products import ProductName
75
from models_library.rpc.webserver.auth.api_keys import ApiKeyCreate, ApiKeyGet
86
from models_library.users import UserID
7+
from pydantic import TypeAdapter
98

109
from ._base import BaseRpcApi
1110

@@ -21,8 +20,7 @@ async def create_api_key(
2120
api_key: ApiKeyCreate,
2221
) -> ApiKeyGet:
2322
"""Create an API key."""
24-
return cast(
25-
ApiKeyGet,
23+
return TypeAdapter(ApiKeyGet).validate_python(
2624
await self._request(
2725
"create_api_key",
2826
product_name=product_name,
@@ -40,8 +38,7 @@ async def get_api_key(
4038
api_key_id: IDStr,
4139
) -> ApiKeyGet:
4240
"""Get an API key by ID."""
43-
return cast(
44-
ApiKeyGet,
41+
return TypeAdapter(ApiKeyGet).validate_python(
4542
await self._request(
4643
"get_api_key",
4744
product_name=product_name,

packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/webserver/v1/functions.py

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from models_library.rest_ordering import OrderBy
3232
from models_library.rest_pagination import PageMetaInfoLimitOffset
3333
from models_library.users import UserID
34+
from pydantic import TypeAdapter
3435

3536
from ._base import BaseRpcApi
3637

@@ -48,8 +49,7 @@ async def register_function(
4849
function: Function,
4950
) -> RegisteredFunction:
5051
"""Register a function."""
51-
return cast(
52-
RegisteredFunction,
52+
return TypeAdapter(RegisteredFunction).validate_python(
5353
await self._request(
5454
"register_function",
5555
product_name=product_name,
@@ -66,8 +66,7 @@ async def get_function(
6666
function_id: FunctionID,
6767
) -> RegisteredFunction:
6868
"""Get a function by ID."""
69-
return cast(
70-
RegisteredFunction,
69+
return TypeAdapter(RegisteredFunction).validate_python(
7170
await self._request(
7271
"get_function",
7372
product_name=product_name,
@@ -84,8 +83,7 @@ async def get_function_input_schema(
8483
function_id: FunctionID,
8584
) -> FunctionInputSchema:
8685
"""Get function input schema."""
87-
return cast(
88-
FunctionInputSchema,
86+
return TypeAdapter(FunctionInputSchema).validate_python(
8987
await self._request(
9088
"get_function_input_schema",
9189
product_name=product_name,
@@ -102,8 +100,7 @@ async def get_function_output_schema(
102100
function_id: FunctionID,
103101
) -> FunctionOutputSchema:
104102
"""Get function output schema."""
105-
return cast(
106-
FunctionOutputSchema,
103+
return TypeAdapter(FunctionOutputSchema).validate_python(
107104
await self._request(
108105
"get_function_output_schema",
109106
product_name=product_name,
@@ -140,8 +137,9 @@ async def list_functions(
140137
search_by_multi_columns: str | None = None,
141138
) -> tuple[list[RegisteredFunction], PageMetaInfoLimitOffset]:
142139
"""List available functions."""
143-
return cast(
144-
tuple[list[RegisteredFunction], PageMetaInfoLimitOffset],
140+
return TypeAdapter(
141+
tuple[list[RegisteredFunction], PageMetaInfoLimitOffset]
142+
).validate_python(
145143
await self._request(
146144
"list_functions",
147145
product_name=product_name,
@@ -167,8 +165,9 @@ async def list_function_jobs(
167165
filter_by_function_job_collection_id: FunctionJobCollectionID | None = None,
168166
) -> tuple[list[RegisteredFunctionJob], PageMetaInfoLimitOffset]:
169167
"""List function jobs."""
170-
return cast(
171-
tuple[list[RegisteredFunctionJob], PageMetaInfoLimitOffset],
168+
return TypeAdapter(
169+
tuple[list[RegisteredFunctionJob], PageMetaInfoLimitOffset]
170+
).validate_python(
172171
await self._request(
173172
"list_function_jobs",
174173
product_name=product_name,
@@ -193,8 +192,9 @@ async def list_function_jobs_with_status(
193192
filter_by_function_job_collection_id: FunctionJobCollectionID | None = None,
194193
) -> tuple[list[RegisteredFunctionJobWithStatus], PageMetaInfoLimitOffset]:
195194
"""List function jobs with status."""
196-
return cast(
197-
tuple[list[RegisteredFunctionJobWithStatus], PageMetaInfoLimitOffset],
195+
return TypeAdapter(
196+
tuple[list[RegisteredFunctionJobWithStatus], PageMetaInfoLimitOffset]
197+
).validate_python(
198198
await self._request(
199199
"list_function_jobs_with_status",
200200
product_name=product_name,
@@ -217,8 +217,9 @@ async def list_function_job_collections(
217217
filters: FunctionJobCollectionsListFilters | None = None,
218218
) -> tuple[list[RegisteredFunctionJobCollection], PageMetaInfoLimitOffset]:
219219
"""List function job collections."""
220-
return cast(
221-
tuple[list[RegisteredFunctionJobCollection], PageMetaInfoLimitOffset],
220+
return TypeAdapter(
221+
tuple[list[RegisteredFunctionJobCollection], PageMetaInfoLimitOffset]
222+
).validate_python(
222223
await self._request(
223224
"list_function_job_collections",
224225
product_name=product_name,
@@ -238,8 +239,7 @@ async def update_function_title(
238239
title: str,
239240
) -> RegisteredFunction:
240241
"""Update function title."""
241-
return cast(
242-
RegisteredFunction,
242+
return TypeAdapter(RegisteredFunction).validate_python(
243243
await self._request(
244244
"update_function_title",
245245
product_name=product_name,
@@ -258,8 +258,7 @@ async def update_function_description(
258258
description: str,
259259
) -> RegisteredFunction:
260260
"""Update function description."""
261-
return cast(
262-
RegisteredFunction,
261+
return TypeAdapter(RegisteredFunction).validate_python(
263262
await self._request(
264263
"update_function_description",
265264
product_name=product_name,
@@ -278,8 +277,7 @@ async def run_function(
278277
inputs: FunctionInputs,
279278
) -> RegisteredFunctionJob:
280279
"""Run a function."""
281-
return cast(
282-
RegisteredFunctionJob,
280+
return TypeAdapter(RegisteredFunctionJob).validate_python(
283281
await self._request(
284282
"run_function",
285283
product_name=product_name,
@@ -297,8 +295,7 @@ async def register_function_job(
297295
function_job: FunctionJob,
298296
) -> RegisteredFunctionJob:
299297
"""Register a function job."""
300-
return cast(
301-
RegisteredFunctionJob,
298+
return TypeAdapter(RegisteredFunctionJob).validate_python(
302299
await self._request(
303300
"register_function_job",
304301
product_name=product_name,
@@ -316,8 +313,7 @@ async def patch_registered_function_job(
316313
registered_function_job_patch: RegisteredFunctionJobPatch,
317314
) -> RegisteredFunctionJob:
318315
"""Patch a registered function job."""
319-
return cast(
320-
RegisteredFunctionJob,
316+
return TypeAdapter(RegisteredFunctionJob).validate_python(
321317
await self._request(
322318
"patch_registered_function_job",
323319
product_name=product_name,
@@ -335,8 +331,7 @@ async def get_function_job(
335331
function_job_id: FunctionJobID,
336332
) -> RegisteredFunctionJob:
337333
"""Get a function job."""
338-
return cast(
339-
RegisteredFunctionJob,
334+
return TypeAdapter(RegisteredFunctionJob).validate_python(
340335
await self._request(
341336
"get_function_job",
342337
product_name=product_name,
@@ -353,8 +348,7 @@ async def get_function_job_status(
353348
function_job_id: FunctionJobID,
354349
) -> FunctionJobStatus:
355350
"""Get function job status."""
356-
return cast(
357-
FunctionJobStatus,
351+
return TypeAdapter(FunctionJobStatus).validate_python(
358352
await self._request(
359353
"get_function_job_status",
360354
product_name=product_name,
@@ -371,8 +365,7 @@ async def get_function_job_outputs(
371365
function_job_id: FunctionJobID,
372366
) -> FunctionOutputs:
373367
"""Get function job outputs."""
374-
return cast(
375-
FunctionOutputs,
368+
return TypeAdapter(FunctionOutputs).validate_python(
376369
await self._request(
377370
"get_function_job_outputs",
378371
product_name=product_name,
@@ -391,8 +384,7 @@ async def update_function_job_status(
391384
check_write_permissions: bool = True,
392385
) -> FunctionJobStatus:
393386
"""Update function job status."""
394-
return cast(
395-
FunctionJobStatus,
387+
return TypeAdapter(FunctionJobStatus).validate_python(
396388
await self._request(
397389
"update_function_job_status",
398390
product_name=product_name,
@@ -413,8 +405,7 @@ async def update_function_job_outputs(
413405
check_write_permissions: bool = True,
414406
) -> FunctionOutputs:
415407
"""Update function job outputs."""
416-
return cast(
417-
FunctionOutputs,
408+
return TypeAdapter(FunctionOutputs).validate_python(
418409
await self._request(
419410
"update_function_job_outputs",
420411
product_name=product_name,
@@ -449,8 +440,7 @@ async def find_cached_function_jobs(
449440
inputs: FunctionInputs,
450441
) -> list[RegisteredFunctionJob] | None:
451442
"""Find cached function jobs."""
452-
return cast(
453-
list[RegisteredFunctionJob] | None,
443+
return TypeAdapter(list[RegisteredFunctionJob] | None).validate_python(
454444
await self._request(
455445
"find_cached_function_jobs",
456446
product_name=product_name,
@@ -468,8 +458,7 @@ async def register_function_job_collection(
468458
function_job_collection: FunctionJobCollection,
469459
) -> RegisteredFunctionJobCollection:
470460
"""Register a function job collection."""
471-
return cast(
472-
RegisteredFunctionJobCollection,
461+
return TypeAdapter(RegisteredFunctionJobCollection).validate_python(
473462
await self._request(
474463
"register_function_job_collection",
475464
product_name=product_name,
@@ -486,8 +475,7 @@ async def get_function_job_collection(
486475
function_job_collection_id: FunctionJobCollectionID,
487476
) -> RegisteredFunctionJobCollection:
488477
"""Get a function job collection."""
489-
return cast(
490-
RegisteredFunctionJobCollection,
478+
return TypeAdapter(RegisteredFunctionJobCollection).validate_python(
491479
await self._request(
492480
"get_function_job_collection",
493481
product_name=product_name,
@@ -519,8 +507,7 @@ async def get_function_user_permissions(
519507
function_id: FunctionID,
520508
) -> FunctionUserAccessRights:
521509
"""Get function user permissions."""
522-
return cast(
523-
FunctionUserAccessRights,
510+
return TypeAdapter(FunctionUserAccessRights).validate_python(
524511
await self._request(
525512
"get_function_user_permissions",
526513
product_name=product_name,
@@ -536,8 +523,7 @@ async def get_functions_user_api_access_rights(
536523
user_id: UserID,
537524
) -> FunctionUserApiAccessRights:
538525
"""Get functions user API access rights."""
539-
return cast(
540-
FunctionUserApiAccessRights,
526+
return TypeAdapter(FunctionUserApiAccessRights).validate_python(
541527
await self._request(
542528
"get_functions_user_api_access_rights",
543529
product_name=product_name,

packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/webserver/v1/licenses.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Licenses RPC API subclient."""
22

3-
from typing import Any, cast
3+
from typing import Any
44

55
from models_library.api_schemas_webserver.licensed_items import LicensedItemRpcGetPage
66
from models_library.api_schemas_webserver.licensed_items_checkouts import (
@@ -14,6 +14,7 @@
1414
from models_library.services_types import ServiceRunID
1515
from models_library.users import UserID
1616
from models_library.wallets import WalletID
17+
from pydantic import TypeAdapter
1718

1819
from ._base import BaseRpcApi
1920

@@ -28,8 +29,7 @@ async def get_licensed_items(
2829
offset: int = 0,
2930
limit: int = 20,
3031
) -> LicensedItemRpcGetPage:
31-
return cast(
32-
LicensedItemRpcGetPage,
32+
return TypeAdapter(LicensedItemRpcGetPage).validate_python(
3333
await self._request_without_authentication(
3434
"get_licensed_items",
3535
product_name=product_name,
@@ -48,8 +48,7 @@ async def get_available_licensed_items_for_wallet(
4848
limit: int = 20,
4949
) -> LicensedItemRpcGetPage:
5050
"""Get licensed items for a wallet."""
51-
return cast(
52-
LicensedItemRpcGetPage,
51+
return TypeAdapter(LicensedItemRpcGetPage).validate_python(
5352
await self._request(
5453
"get_available_licensed_items_for_wallet",
5554
product_name=product_name,
@@ -68,8 +67,7 @@ async def get_licensed_items_checkouts_for_wallet(
6867
wallet_id: int | None = None,
6968
) -> list[dict[str, Any]]:
7069
"""Get licensed items checkouts for a wallet."""
71-
return cast(
72-
list[dict[str, Any]],
70+
return TypeAdapter(list[dict[str, Any]]).validate_python(
7371
await self._request(
7472
"get_licensed_items_checkouts_for_wallet",
7573
product_name=product_name,
@@ -89,8 +87,7 @@ async def checkout_licensed_item_for_wallet(
8987
service_run_id: ServiceRunID,
9088
) -> LicensedItemCheckoutRpcGet:
9189
"""Checkout a licensed item for a wallet."""
92-
return cast(
93-
LicensedItemCheckoutRpcGet,
90+
return TypeAdapter(LicensedItemCheckoutRpcGet).validate_python(
9491
await self._request(
9592
"checkout_licensed_item_for_wallet",
9693
product_name=product_name,
@@ -110,8 +107,7 @@ async def release_licensed_item_for_wallet(
110107
licensed_item_checkout_id: LicensedItemCheckoutID,
111108
) -> LicensedItemCheckoutRpcGet:
112109
"""Release a licensed item checkout for a wallet."""
113-
return cast(
114-
LicensedItemCheckoutRpcGet,
110+
return TypeAdapter(LicensedItemCheckoutRpcGet).validate_python(
115111
await self._request(
116112
"release_licensed_item_for_wallet",
117113
product_name=product_name,

packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/webserver/v1/projects.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Projects RPC API subclient."""
22

3-
from typing import cast
4-
53
from models_library.products import ProductName
64
from models_library.projects import ProjectID
75
from models_library.rest_pagination import PageOffsetInt
@@ -15,6 +13,7 @@
1513
PageLimitInt,
1614
)
1715
from models_library.users import UserID
16+
from pydantic import TypeAdapter
1817

1918
from ._base import BaseRpcApi
2019

@@ -51,8 +50,7 @@ async def list_projects_marked_as_jobs(
5150
filters: ListProjectsMarkedAsJobRpcFilters | None = None,
5251
) -> PageRpcProjectJobRpcGet:
5352
"""List projects marked as jobs."""
54-
return cast(
55-
PageRpcProjectJobRpcGet,
53+
return TypeAdapter(PageRpcProjectJobRpcGet).validate_python(
5654
await self._request(
5755
"list_projects_marked_as_jobs",
5856
product_name=product_name,
@@ -72,8 +70,7 @@ async def get_project_marked_as_job(
7270
job_parent_resource_name: str,
7371
) -> ProjectJobRpcGet:
7472
"""Get a project marked as a job."""
75-
return cast(
76-
ProjectJobRpcGet,
73+
return TypeAdapter(ProjectJobRpcGet).validate_python(
7774
await self._request(
7875
"get_project_marked_as_job",
7976
product_name=product_name,

0 commit comments

Comments
 (0)