Allow escalation of UUID validation warning to error

Today UUIDField emits a warning if the provided value is not a valid
UUID. However this warning cannot be escalated to an exception through
the standard warnings module because the validation code also adds a
warning filter to the warning filter list that overrides the existing
filters.

This patch ensures that the 'once' warning filter only added to the
end of the filter list. This way if the client has already specified
another filter for this warning (e.g. and error filter) then that
filter will not be overriden.

Change-Id: I17cb96d16fcd91195478b738fbdda01b47cfd69d
Closes-Bug: #1746966
This commit is contained in:
Balazs Gibizer 2018-02-02 12:16:30 +01:00
parent 339b5f8e57
commit 0e3526710f
2 changed files with 10 additions and 1 deletions

View File

@ -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:

View File

@ -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()