From 331b580fb89bdd110e38ac34cc5d0b48eafe263b Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Mon, 26 Mar 2012 23:34:23 +0200 Subject: [PATCH] Warn when implicit conversions happen To display them, you can use the python -W argument or the PYTHONWARNINGS environment variable. Maybe debug mode should display them. --- weboob/capabilities/base.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/weboob/capabilities/base.py b/weboob/capabilities/base.py index 3844ac1f..a5316c8a 100644 --- a/weboob/capabilities/base.py +++ b/weboob/capabilities/base.py @@ -18,6 +18,7 @@ # along with weboob. If not, see . +import warnings import datetime from dateutil.parser import parse as parse_dt from copy import deepcopy @@ -44,6 +45,12 @@ class FieldNotFound(Exception): Exception.__init__(self, u'Field "%s" not found for object %s' % (field, obj)) +class ConversionWarning(UserWarning): + """ + A field's type was changed when setting it. + Ideally, the module should use the right type before setting it. + """ + pass class NotAvailableMeta(type): def __str__(self): @@ -316,7 +323,12 @@ class CapBaseObject(object): if value not in (NotLoaded, NotAvailable, None): try: # Try to convert value to the wanted one. - value = attr.convert(value) + nvalue = attr.convert(value) + # If the value was converted + if nvalue is not value: + warnings.warn('Value %s was converted from %s to %s' % + (name, type(value), type(nvalue)), ConversionWarning, stacklevel=2) + value = nvalue except Exception: # error during conversion, it will probably not # match the wanted following types, so we'll