Skip to content

Commit

Permalink
ImmutableValidatedObject: Support nested Mapping types
Browse files Browse the repository at this point in the history
  • Loading branch information
talyz committed Nov 15, 2023
1 parent 2cfc2cb commit 7d0d819
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions nixops/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
import re
import typing
import typeguard # type: ignore
import inspect
import shlex
import collections.abc
from inspect import isclass
from typing import (
Callable,
List,
Expand Down Expand Up @@ -149,21 +150,33 @@ def _transform_value(key: Any, value: Any) -> Any:
if not ann:
return value

if inspect.isclass(ann) and issubclass(ann, ImmutableValidatedObject):
if isclass(ann) and issubclass(ann, ImmutableValidatedObject):
value = ann(**value)

# Support Sequence[ImmutableValidatedObject]
if isinstance(value, tuple) and not isinstance(ann, str):
new_value = []
for v in value:
for subann in ann.__args__: # type: ignore
if inspect.isclass(subann) and issubclass(
subann, ImmutableValidatedObject
):
new_value.append(subann(**v))
else:
new_value.append(v)
value = tuple(new_value)
# Support containers of ImmutableValidatedObjects
match typing.get_origin(ann):

case collections.abc.Sequence:
new_value: Any = []
for v in value:
for subann in typing.get_args(ann):
if isclass(subann) and issubclass(
subann, ImmutableValidatedObject
):
new_value.append(subann(**v))
else:
new_value.append(v)
value = tuple(new_value)

case collections.abc.Mapping:
_, value_ann = typing.get_args(ann)
if isclass(value_ann) and issubclass(
value_ann, ImmutableValidatedObject
):
new_value = {}
for k, v in value.items():
new_value[k] = value_ann(**v)
value = new_value

typeguard.check_type(value, ann)

Expand Down

0 comments on commit 7d0d819

Please sign in to comment.