warn developers when can't convert value into primitive

We try to raise ValueError directly when can't convert the value into
proimitive in If9e8dd5cc2634168910d5f9f8d9302aeefa16097, but revert it
due to some reasons The better way is that notify consuming projects
with the future API behavior change before the change. It will raise
ValureError in version 3.0.

Partial-Bug: #1593641
Change-Id: I2eff07cd25f0565b380cb6e76628a896c8d0ec61
This commit is contained in:
ChangBo Guo(gcb) 2017-08-22 10:38:43 +08:00
parent 8989ad3052
commit fecad3c31f
2 changed files with 7 additions and 1 deletions

View File

@ -36,6 +36,7 @@ import inspect
import itertools
import json
import uuid
import warnings
from oslo_utils import encodeutils
from oslo_utils import importutils
@ -167,6 +168,9 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
# __iter__ defined but it isn't callable as list().
return six.text_type(value)
# TODO(gcb) raise ValueError in version 3.0
warnings.warn("Cannot convert %r to primitive, will raise ValueError "
"instead of warning in version 3.0" % (value,))
return value

View File

@ -243,7 +243,8 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
p = jsonutils.to_primitive(x)
self.assertEqual({'a': 1, 'b': 2, 'c': 3}, p)
def test_instance(self):
@mock.patch('warnings.warn')
def test_instance(self, warn_mock):
class MysteryClass(object):
a = 10
@ -255,6 +256,7 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
jsonutils.to_primitive(x, convert_instances=True))
self.assertEqual(x, jsonutils.to_primitive(x))
warn_mock.assert_called_once()
def test_typeerror(self):
x = bytearray # Class, not instance