Skip to content

Commit a92587d

Browse files
authored
Merge branch 'main' into fix_typing
2 parents f69cf95 + dc3746b commit a92587d

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

docs/module.rst

Whitespace-only changes.
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,51 @@
1+
from collections.abc import Callable
12
from dataclasses import dataclass
23
from typing import Any, Dict, Generic, List, Optional, TypeVar
4+
from _typeshed import SupportsKeysAndGetItem
5+
6+
from scaleway_core.profile import ProfileDefaults
37

48
T = TypeVar("T")
59

610

711
@dataclass
812
class OneOfPossibility(Generic[T]):
913
param: str
10-
1114
value: Optional[T]
12-
13-
default: Optional[T] = None
15+
default: Optional[T | ProfileDefaults] = None
16+
marshal_func: Optional[Callable[[T, T | None], Dict[str, Any]]] = None
1417

1518

1619
def resolve_one_of(
1720
possibilities: List[OneOfPossibility[Any]], is_required: bool = False
18-
) -> Dict[str, Any]:
21+
) -> SupportsKeysAndGetItem[str, Any]:
1922
"""
2023
Resolves the ideal parameter and value amongst an optional list.
24+
Uses marshal_func if provided.
2125
"""
2226

23-
# Get the first non-empty parameter
27+
# Try to resolve using non-None value
2428
for possibility in possibilities:
2529
if possibility.value is not None:
30+
if possibility.marshal_func is not None:
31+
return {
32+
possibility.param: possibility.marshal_func(
33+
possibility.value, possibility.default
34+
)
35+
}
2636
return {possibility.param: possibility.value}
2737

28-
# Get the first non-empty default
38+
# Try to resolve using non-None default
2939
for possibility in possibilities:
3040
if possibility.default is not None:
3141
return {possibility.param: possibility.default}
3242

33-
# If required, raise an error
43+
# If required but unresolved, raise an error
3444
if is_required:
3545
possibilities_keys = " or ".join(
3646
[possibility.param for possibility in possibilities]
3747
)
3848
raise ValueError(f"one of ${possibilities_keys} must be present")
3949

40-
# Else, return an empty dict
50+
# Else, return empty dict
4151
return {}

0 commit comments

Comments
 (0)