Skip to content

Commit 3814699

Browse files
committed
Bump to v0.0.9 and fix custom imports
1 parent dabd4d4 commit 3814699

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

utilsd/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
from .fileio import load, dump
55
from .logging import print_log, setup_logger
66

7-
__version__ = '0.0.8'
7+
__version__ = '0.0.9'

utilsd/config/python.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import inspect
1010
import json
1111
import os
12+
import warnings
1213
from argparse import SUPPRESS, ArgumentParser, ArgumentTypeError
1314
from dataclasses import fields, is_dataclass
1415
from enum import Enum
@@ -378,7 +379,11 @@ def build_fn(self, **kwargs):
378379
for k in kwargs:
379380
# silently overwrite the arguments with given ones.
380381
result[k] = kwargs[k]
381-
return self.type()(**result)
382+
try:
383+
return self.type()(**result)
384+
except:
385+
warnings.warn(f'Error when constructing {self.type()} with {result}.', RuntimeWarning)
386+
raise
382387

383388
return dataclasses.make_dataclass(class_name, fields, bases=(cls,), init=False,
384389
namespace={'type': type_fn, 'build': build_fn})

utilsd/fileio/config.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
BASE_KEY = '_base_'
2121
DELETE_KEY = '_delete_'
22+
CUSTOM_IMPORT_KEY = '_custom_imports_'
2223
RESERVED_KEYS = ['filename', 'text', 'pretty_text']
2324

2425

@@ -284,8 +285,10 @@ def fromfile(filename,
284285
import_custom_modules=True):
285286
cfg_dict, cfg_text = Config._file2dict(filename,
286287
use_predefined_variables)
287-
if import_custom_modules and cfg_dict.get('custom_imports', None):
288-
import_modules_from_strings(**cfg_dict['custom_imports'])
288+
if import_custom_modules and cfg_dict.get(CUSTOM_IMPORT_KEY, None):
289+
if not isinstance(cfg_dict[CUSTOM_IMPORT_KEY], list):
290+
raise ValueError('_custom_imports_ must be a list of import paths.')
291+
import_modules_from_strings(cfg_dict.pop(CUSTOM_IMPORT_KEY))
289292
return Config(cfg_dict, cfg_text=cfg_text, filename=filename)
290293

291294
@staticmethod

0 commit comments

Comments
 (0)