Skip to content

Commit 7d0d819

Browse files
committed
ImmutableValidatedObject: Support nested Mapping types
1 parent 2cfc2cb commit 7d0d819

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

nixops/util.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
import re
1818
import typing
1919
import typeguard # type: ignore
20-
import inspect
2120
import shlex
21+
import collections.abc
22+
from inspect import isclass
2223
from typing import (
2324
Callable,
2425
List,
@@ -149,21 +150,33 @@ def _transform_value(key: Any, value: Any) -> Any:
149150
if not ann:
150151
return value
151152

152-
if inspect.isclass(ann) and issubclass(ann, ImmutableValidatedObject):
153+
if isclass(ann) and issubclass(ann, ImmutableValidatedObject):
153154
value = ann(**value)
154155

155-
# Support Sequence[ImmutableValidatedObject]
156-
if isinstance(value, tuple) and not isinstance(ann, str):
157-
new_value = []
158-
for v in value:
159-
for subann in ann.__args__: # type: ignore
160-
if inspect.isclass(subann) and issubclass(
161-
subann, ImmutableValidatedObject
162-
):
163-
new_value.append(subann(**v))
164-
else:
165-
new_value.append(v)
166-
value = tuple(new_value)
156+
# Support containers of ImmutableValidatedObjects
157+
match typing.get_origin(ann):
158+
159+
case collections.abc.Sequence:
160+
new_value: Any = []
161+
for v in value:
162+
for subann in typing.get_args(ann):
163+
if isclass(subann) and issubclass(
164+
subann, ImmutableValidatedObject
165+
):
166+
new_value.append(subann(**v))
167+
else:
168+
new_value.append(v)
169+
value = tuple(new_value)
170+
171+
case collections.abc.Mapping:
172+
_, value_ann = typing.get_args(ann)
173+
if isclass(value_ann) and issubclass(
174+
value_ann, ImmutableValidatedObject
175+
):
176+
new_value = {}
177+
for k, v in value.items():
178+
new_value[k] = value_ann(**v)
179+
value = new_value
167180

168181
typeguard.check_type(value, ann)
169182

0 commit comments

Comments
 (0)