11from __future__ import annotations
22from collections .abc import Callable
3- from scaleway_core .profile .profile import ProfileDefaults
43from dataclasses import dataclass
54from typing import Any , Dict , Generic , List , Optional , TypeVar
6- from _typeshed import SupportsKeysAndGetItem
5+
6+ from typing import TYPE_CHECKING
7+
8+ if TYPE_CHECKING :
9+ from _typeshed import SupportsKeysAndGetItem
10+
711
812T = TypeVar ("T" )
913
@@ -19,6 +23,12 @@ class OneOfPossibility(Generic[T]):
1923def resolve_one_of (
2024 possibilities : List [OneOfPossibility [Any ]], is_required : bool = False
2125) -> SupportsKeysAndGetItem [str , Any ]:
26+ """
27+ Resolves the ideal parameter and value amongst an optional list.
28+ Uses marshal_func if provided.
29+ """
30+
31+ # Try to resolve using non-None value
2232 for possibility in possibilities :
2333 if possibility .value is not None :
2434 if possibility .marshal_func is not None :
@@ -32,15 +42,20 @@ def resolve_one_of(
3242 for possibility in possibilities :
3343 if possibility .default is not None :
3444 if possibility .marshal_func is not None :
35- raise ValueError (
36- f"{ possibility .param } is missing a required value for marshal_func"
37- )
45+ # When no actual value, call with None as value
46+ return {
47+ possibility .param : possibility .marshal_func (
48+ None , possibility .default
49+ )
50+ }
3851 return {possibility .param : possibility .default }
3952
53+ # If required but unresolved, raise an error
4054 if is_required :
4155 possibilities_keys = " or " .join (
4256 [possibility .param for possibility in possibilities ]
4357 )
4458 raise ValueError (f"one of ${ possibilities_keys } must be present" )
4559
60+ # Else, return empty dict
4661 return {}
0 commit comments