From 5098b3f45878935445b4d7bb96395778b1fa4414 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sat, 21 Apr 2012 13:21:31 +0200 Subject: [PATCH] print a warning when a module adds an attribute to a CapBaseObject not prefixed with a _ --- 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 0678a83b..1922a071 100644 --- a/weboob/capabilities/base.py +++ b/weboob/capabilities/base.py @@ -29,7 +29,7 @@ from weboob.tools.ordereddict import OrderedDict __all__ = ['FieldNotFound', 'NotAvailable', 'NotLoaded', 'IBaseCap', 'Field', 'IntField', 'DecimalField', 'FloatField', 'StringField', - 'BytesField', 'DateField', 'DeltaField', 'CapBaseObject', 'empty'] + 'BytesField', 'DateField', 'DeltaField', 'empty', 'CapBaseObject'] def empty(value): @@ -64,6 +64,12 @@ class ConversionWarning(UserWarning): """ pass +class AttributeCreationWarning(UserWarning): + """ + A non-field attribute has been created with a name not + prefixed with a _. + """ + class NotAvailableMeta(type): def __str__(self): return unicode(self).decode('utf-8') @@ -266,6 +272,9 @@ class CapBaseObject(object): """ __metaclass__ = _CapBaseObjectMeta + + id = None + backend = None _fields = None def __init__(self, id, backend=None): @@ -342,6 +351,9 @@ class CapBaseObject(object): try: attr = (self._fields or {})[name] except KeyError: + if not name in dir(self) and not name.startswith('_'): + warnings.warn('Creating a non-field attribute %s. Please prefix it with _' % name, + AttributeCreationWarning, stacklevel=2) object.__setattr__(self, name, value) else: if not empty(value):