Skip to content

Commit

Permalink
fix: the type of numeric keys for overrides
Browse files Browse the repository at this point in the history
This is a workaround to fix the types of numeric keys as they can only be represented as strings in the json overrides.
  • Loading branch information
Adamantios committed Aug 9, 2024
1 parent 1132bb3 commit bef8e5b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
18 changes: 17 additions & 1 deletion aea/configurations/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
#
# ------------------------------------------------------------------------------
"""Implementation of the configuration validation."""

import inspect
import json
import os
from collections import OrderedDict
from copy import deepcopy
from functools import reduce
from pathlib import Path
from typing import Any, Dict, Iterator, List, Optional, Tuple

Expand All @@ -36,7 +38,7 @@
from aea.configurations.constants import AGENT
from aea.configurations.data_types import ComponentId, ComponentType, PublicId
from aea.exceptions import AEAValidationError
from aea.helpers.base import dict_to_path_value
from aea.helpers.base import dict_to_path_value, update_nested_dict
from aea.helpers.env_vars import is_env_variable
from aea.helpers.io import open_file

Expand Down Expand Up @@ -301,6 +303,20 @@ def validate_data_with_pattern(
overrides = {tuple(path): value for path, value in dict_to_path_value(data)}
errors = []

# this is a workaround to fix the type of numeric keys as they can only be represented as strs in the json overrides
for path in original_config:
path_as_str = tuple(map(str, path))
if path_as_str in overrides and path not in overrides:
value = overrides[path_as_str]
del overrides[path_as_str]
up_to_last_key = data
for key in path_as_str[:-1]:
up_to_last_key = up_to_last_key[key]
del up_to_last_key[path_as_str[-1]]
overrides[path] = value
vals = reduce(lambda d, key: {key: d}, reversed(path), value)
update_nested_dict(data, vals)

def check_excludes(path: Tuple[str, ...]) -> bool:
for exclude in excludes_:
if len(exclude) > len(path): # pragma: nocover
Expand Down
10 changes: 10 additions & 0 deletions aea/helpers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1091,3 +1091,13 @@ def prepend_if_not_absolute(path: PathLike, prefix: PathLike) -> PathLike:
:return: the same path if absolute, else the prepended path.
"""
return path if Path(path).is_absolute() else Path(prefix) / path


def update_nested_dict(dict_: dict, nested_update: dict) -> dict:
"""Update a nested dictionary."""
for key, value in nested_update.items():
if isinstance(value, dict):
dict_[key] = update_nested_dict(dict_.get(key, {}), value)
else:
dict_[key] = value
return dict_

0 comments on commit bef8e5b

Please sign in to comment.