Skip to content

Commit

Permalink
typed dict classes should have from_dict as default arg
Browse files Browse the repository at this point in the history
  • Loading branch information
andre-merzky committed Apr 6, 2024
1 parent cf31a3a commit b5af2d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
15 changes: 11 additions & 4 deletions src/radical/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,23 @@ class Config(TypedDict):

# --------------------------------------------------------------------------
#
def __init__(self, module=None, category=None, name=None, cfg=None,
from_dict=None, path=None, expand=True, env=None,
_internal=False):
def __init__(self, from_dict=None,
module=None, category=None, name=None,
cfg=None, path=None, expand=True,
env=None, _internal=False):
"""
Load a config (json) file from the module's config tree, and overload
any user specific config settings if found.
Parameters
----------
from_dict: alias for cfg, to satisfy base class constructor
module: used to determine the module's config file location
- default: `radical.utils`
category: name of config to be loaded from module's config path
name: specify a specific configuration to be used
path: path to app config json to be used for initialization
cfg: application config dict to be used for initialization
from_dict: alias for cfg, to satisfy base class constructor
expand: enable / disable environment var expansion
- default: True
env: environment dictionary to be used for expansion
Expand All @@ -215,6 +216,12 @@ def __init__(self, module=None, category=None, name=None, cfg=None,
configuration hierarchy.
"""

# if the `from_dict` is given but is a string, we interpret it as
# `module` parameter.
if from_dict and isinstance(from_dict, str):
module = from_dict
from_dict = None

if from_dict:
# if we could only overload constructors by signature... :-/
# As it is, we have to emulate that...
Expand Down
22 changes: 8 additions & 14 deletions src/radical/utils/typeddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,27 +488,21 @@ def _query(self, key, default=None, last_key=True):

# ------------------------------------------------------------------------------
#
def _as_dict_value(v, _annotate=False):
if isinstance(v, TypedDict):
ret = copy.deepcopy(v)
if _annotate:
ret['_type'] = type(v).__name__
return ret
else:
return as_dict(v, _annotate)


def as_dict(src, _annotate=False):
'''
Iterate given object, apply `as_dict()` to all typed
values, and return the result (effectively a shallow copy).
'''
if isinstance(src, dict):
tgt = {k: _as_dict_value(v, _annotate) for k, v in src.items()}
if isinstance(src, TypedDict):
tgt = {k: as_dict(v, _annotate) for k, v in src.items()}
if _annotate:
tgt['_type'] = type(src).__name__
elif isinstance(src, collections.abc.Iterable):
tgt = [_as_dict_value(x, _annotate) for x in src]
elif isinstance(src, dict):
tgt = {k: as_dict(v, _annotate) for k, v in src.items()}
elif isinstance(src, list):
tgt = [as_dict(x, _annotate) for x in src]
elif isinstance(src, tuple):
tgt = tuple([as_dict(x, _annotate) for x in src])
else:
tgt = src
return tgt
Expand Down

0 comments on commit b5af2d0

Please sign in to comment.