11from collections .abc import Callable
2+ from scaleway_core .profile .profile import ProfileDefaults
23from dataclasses import dataclass
34from typing import Any , Dict , Generic , List , Optional , TypeVar
45from _typeshed import SupportsKeysAndGetItem
56
6- from scaleway_core .profile import ProfileDefaults
7-
87T = TypeVar ("T" )
98
10-
119@dataclass
12- class OneOfPossibility ( Generic [ T ]) :
10+ class OneOfPossibility :
1311 param : str
14- value : Optional [T ]
15- default : Optional [T | ProfileDefaults ] = None
16- marshal_func : Optional [Callable [[T , T | None ], Dict [str , Any ]]] = None
17-
12+ value : Optional [Any ]
13+ default : Optional [ProfileDefaults ] = None
14+ marshal_func : Optional [Callable [[T , Optional [ProfileDefaults ]], Dict [str , Any ]]] = None
1815
1916def resolve_one_of (
20- possibilities : List [OneOfPossibility [ Any ] ], is_required : bool = False
17+ possibilities : List [OneOfPossibility ], is_required : bool = False
2118) -> SupportsKeysAndGetItem [str , Any ]:
22- """
23- Resolves the ideal parameter and value amongst an optional list.
24- Uses marshal_func if provided.
25- """
2619
27- # Try to resolve using non-None value
2820 for possibility in possibilities :
2921 if possibility .value is not None :
3022 if possibility .marshal_func is not None :
@@ -35,24 +27,19 @@ def resolve_one_of(
3527 }
3628 return {possibility .param : possibility .value }
3729
38- # Try to resolve using non-None default
3930 for possibility in possibilities :
4031 if possibility .default is not None :
4132 if possibility .marshal_func is not None :
42- # When no actual value, call with None as value
43- return {
44- possibility .param : possibility .marshal_func (
45- None , possibility .default
46- )
47- }
33+ raise ValueError (
34+ f"{ possibility .param } is missing a required value for marshal_func"
35+ )
4836 return {possibility .param : possibility .default }
4937
50- # If required but unresolved, raise an error
5138 if is_required :
5239 possibilities_keys = " or " .join (
5340 [possibility .param for possibility in possibilities ]
5441 )
5542 raise ValueError (f"one of ${ possibilities_keys } must be present" )
5643
57- # Else, return empty dict
5844 return {}
45+
0 commit comments