diff --git a/oslo_versionedobjects/fields.py b/oslo_versionedobjects/fields.py index 4b175741..0ee27f9d 100644 --- a/oslo_versionedobjects/fields.py +++ b/oslo_versionedobjects/fields.py @@ -347,7 +347,10 @@ class UUID(StringPattern): def coerce(obj, attr, value): # FIXME(danms): We should actually verify the UUIDness here with warnings.catch_warnings(): - warnings.simplefilter("once") + # Change the warning action only if no other filter exists + # for this warning to allow the client to define other action + # like 'error' for this warning. + warnings.filterwarnings(action="once", append=True) try: uuid.UUID(str(value)) except Exception: diff --git a/oslo_versionedobjects/tests/test_fields.py b/oslo_versionedobjects/tests/test_fields.py index ad36c2fe..77c7dd48 100644 --- a/oslo_versionedobjects/tests/test_fields.py +++ b/oslo_versionedobjects/tests/test_fields.py @@ -13,6 +13,7 @@ # under the License. import datetime +import warnings import iso8601 import mock @@ -296,6 +297,11 @@ class TestUUID(TestField): self.test_from_primitive() self.test_to_primitive() + def test_validation_warning_can_be_escalated_to_exception(self): + warnings.filterwarnings(action='error') + self.assertRaises(FutureWarning, self.field.coerce, 'obj', 'attr', + 'not a uuid') + def test_get_schema(self): field = fields.UUIDField() schema = field.get_schema()