add index in warnings

This commit is contained in:
Emmanuel Garette 2019-02-13 22:49:27 +01:00
parent 526fe892d0
commit 0a3b0e913f
2 changed files with 17 additions and 10 deletions

View file

@ -159,11 +159,13 @@ class _CommonError:
val, val,
display_type, display_type,
opt, opt,
err_msg): err_msg,
index):
self.val = val self.val = val
self.display_type = display_type self.display_type = display_type
self.opt = weakref.ref(opt) self.opt = weakref.ref(opt)
self.err_msg = err_msg self.err_msg = err_msg
self.index = index
super().__init__(self.err_msg) super().__init__(self.err_msg)
def __str__(self): def __str__(self):

View file

@ -302,13 +302,15 @@ class Option(BaseOption):
warnings.warn_explicit(ValueWarning(_value, warnings.warn_explicit(ValueWarning(_value,
self._display_name, self._display_name,
self, self,
err_msg = '{0}'.format(err)), '{0}'.format(err),
_index),
ValueWarning, ValueWarning,
self.__class__.__name__, 0) self.__class__.__name__, 0)
else: else:
raise err raise err
try: try:
val = value val = value
err_index = force_index
if not self.impl_is_multi(): if not self.impl_is_multi():
do_validation(val, None) do_validation(val, None)
elif force_index is not None: elif force_index is not None:
@ -316,7 +318,7 @@ class Option(BaseOption):
if not isinstance(value, list): if not isinstance(value, list):
raise ValueError(_('which must be a list')) raise ValueError(_('which must be a list'))
_is_not_unique(value) _is_not_unique(value)
for idx, val in enumerate(value): for val in value:
do_validation(val, do_validation(val,
force_index) force_index)
else: else:
@ -325,19 +327,19 @@ class Option(BaseOption):
elif not isinstance(value, list): elif not isinstance(value, list):
raise ValueError(_('which must be a list')) raise ValueError(_('which must be a list'))
elif self.impl_is_submulti(): elif self.impl_is_submulti():
for idx, lval in enumerate(value): for err_index, lval in enumerate(value):
_is_not_unique(lval) _is_not_unique(lval)
if not isinstance(lval, list): if not isinstance(lval, list):
raise ValueError(_('which "{}" must be a list of list' raise ValueError(_('which "{}" must be a list of list'
'').format(lval)) '').format(lval))
for val in lval: for val in lval:
do_validation(val, do_validation(val,
idx) err_index)
else: else:
_is_not_unique(value) _is_not_unique(value)
for idx, val in enumerate(value): for err_index, val in enumerate(value):
do_validation(val, do_validation(val,
idx) err_index)
if not is_warnings_only or not check_error: if not is_warnings_only or not check_error:
self.valid_consistency(option_bag, self.valid_consistency(option_bag,
@ -350,11 +352,13 @@ class Option(BaseOption):
raise ValueOptionError(val, raise ValueOptionError(val,
self._display_name, self._display_name,
option_bag.ori_option, option_bag.ori_option,
'{0}'.format(err)) '{0}'.format(err),
err_index)
warnings.warn_explicit(ValueErrorWarning(val, warnings.warn_explicit(ValueErrorWarning(val,
self._display_name, self._display_name,
option_bag.ori_option, option_bag.ori_option,
'{0}'.format(err)), '{0}'.format(err),
err_index),
ValueErrorWarning, ValueErrorWarning,
self.__class__.__name__, 0) self.__class__.__name__, 0)
@ -635,7 +639,8 @@ class Option(BaseOption):
warnings.warn_explicit(ValueWarning(value, warnings.warn_explicit(ValueWarning(value,
self._display_name, self._display_name,
current_opt, current_opt,
"{}".format(err)), "{}".format(err),
option_bag.index),
ValueWarning, ValueWarning,
self.__class__.__name__, 0) self.__class__.__name__, 0)
else: else: