From 659243ba8f4d29d82caa801da78b1543a06e9892 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Tue, 4 Aug 2020 16:49:21 +0200 Subject: [PATCH] better support for warnings_only --- tiramisu/autolib.py | 35 +++++++++++++++++++++-------------- tiramisu/option/baseoption.py | 2 +- tiramisu/option/option.py | 21 +++++++-------------- tiramisu/value.py | 3 ++- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/tiramisu/autolib.py b/tiramisu/autolib.py index bc1bf86..8108849 100644 --- a/tiramisu/autolib.py +++ b/tiramisu/autolib.py @@ -156,7 +156,9 @@ class Calculation: option_bag: OptionBag, leadership_must_have_index: bool=False, orig_value: Any=undefined, - allow_raises=False) -> Any: + allow_value_error=False, + force_value_warning=False, + ) -> Any: return await carry_out_calculation(option_bag.option, callback=self.function, callback_params=self.params, @@ -164,7 +166,9 @@ class Calculation: config_bag=option_bag.config_bag, leadership_must_have_index=leadership_must_have_index, orig_value=orig_value, - allow_raises=allow_raises) + allow_value_error=allow_value_error, + force_value_warning=force_value_warning, + ) async def help(self, option_bag: OptionBag, @@ -361,18 +365,18 @@ async def carry_out_calculation(option, config_bag: Optional[ConfigBag], orig_value=undefined, leadership_must_have_index: bool=False, - allow_raises: int=False): + allow_value_error: bool=False, + force_value_warning: bool=False, + ): """a function that carries out a calculation for an option's value :param option: the option :param callback: the name of the callback function - :type callback: str :param callback_params: the callback's parameters (only keyword parameters are allowed) - :type callback_params: dict :param index: if an option is multi, only calculates the nth value - :type index: int - :param allow_raises: to know if carry_out_calculation is used to validate a value + :param allow_value_error: to know if carry_out_calculation can return ValueError or ValueWarning (for example if it's a validation) + :param force_value_warning: transform valueError to ValueWarning object The callback_params is a dict. Key is used to build args (if key is '') and kwargs (otherwise). Values are tuple of: @@ -411,7 +415,8 @@ async def carry_out_calculation(option, continue ret = calculate(option, callback, - allow_raises, + allow_value_error, + force_value_warning, args, kwargs) if isinstance(ret, list) and not option.impl_is_dynoptiondescription() and \ @@ -436,9 +441,11 @@ async def carry_out_calculation(option, def calculate(option, callback: Callable, - allow_raises: bool, + allow_value_error: bool, + force_value_warning: bool, args, - kwargs): + kwargs, + ): """wrapper that launches the 'callback' :param callback: callback function @@ -448,12 +455,12 @@ def calculate(option, """ try: return callback(*args, **kwargs) - except ValueError as err: - if allow_raises: + except (ValueError, ValueWarning) as err: + if allow_value_error: + if force_value_warning: + raise ValueWarning(str(err)) raise err error = err - except ValueWarning as err: - raise err except Exception as err: # import traceback # traceback.print_exc() diff --git a/tiramisu/option/baseoption.py b/tiramisu/option/baseoption.py index 8440b2f..40a3909 100644 --- a/tiramisu/option/baseoption.py +++ b/tiramisu/option/baseoption.py @@ -106,7 +106,7 @@ class Base: context_od) -> Set[str]: ret = set(getattr(self, '_dependencies', STATIC_TUPLE)) if context_od and hasattr(context_od, '_dependencies'): - # if context is set in options, add those options + # add options that have context is set in calculation return set(context_od._dependencies) | ret return ret diff --git a/tiramisu/option/option.py b/tiramisu/option/option.py index 7b42d6e..fde3fa0 100644 --- a/tiramisu/option/option.py +++ b/tiramisu/option/option.py @@ -84,7 +84,9 @@ class Option(BaseOption): is_multi = True _multi = submulti else: - raise ValueError(_('invalid multi type "{}"').format(multi)) + raise ValueError(_('invalid multi type "{}" for "{}"').format(multi, + name, + )) if _multi != 1: _setattr(self, '_multi', _multi) if multi is not False and default is None: @@ -335,7 +337,9 @@ class Option(BaseOption): if ((check_error and not calc_is_warnings_only) or (not check_error and calc_is_warnings_only)): try: - kwargs = {'allow_raises': True} + kwargs = {'allow_value_error': True, + 'force_value_warning': calc_is_warnings_only, + } if _index is not None and option_bag.index == _index: soption_bag = option_bag else: @@ -346,17 +350,6 @@ class Option(BaseOption): await validator.execute(soption_bag, leadership_must_have_index=True, **kwargs) - except ValueError as err: - if calc_is_warnings_only: - warnings.warn_explicit(ValueWarning(val, - self._display_name, - self, - '{0}'.format(err), - _index), - ValueWarning, - self.__class__.__name__, 306) - else: - raise err except ValueWarning as warn: warnings.warn_explicit(ValueWarning(val, self._display_name, @@ -364,7 +357,7 @@ class Option(BaseOption): '{0}'.format(warn), _index), ValueWarning, - self.__class__.__name__, 316) + self.__class__.__name__, 356) async def do_validation(_value, _index): diff --git a/tiramisu/value.py b/tiramisu/value.py index 73c81ce..f492d0f 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -241,7 +241,8 @@ class Values: await option_bag.config_bag.context.cfgimpl_reset_cache(option_bag) async def calculate_value(self, - option_bag: OptionBag) -> Any: + option_bag: OptionBag, + ) -> Any: # if value has callback, calculate value callback, callback_params = option_bag.option.impl_get_callback()