Fix issue with coercing valid_values to a tuple

A recent patch exposed valid_values via property, and cocerced the
result to a tuple to convey the immutable nature of them. This was
a good thought, but unfortunately breaks __repr__ stability, which
we use in the fixture to calculate the object hashes.

We could make the __repr__ dig into the raw types on the field
instead of using the property, but this is probably not the best and
smallest change at this point. If we can get everyone to start using
tuples for their value_values list, then we could land a change to
coerce and enforce that. Right now, we just need to fix the
unexpected regression.

Change-Id: I09b78e7f816fc26c69a3e9435c9d92d5acb5821b
This commit is contained in:
Dan Smith 2016-04-19 10:22:24 -07:00
parent 730659ed6f
commit f7e5f8e67a
2 changed files with 6 additions and 2 deletions

View File

@ -293,7 +293,7 @@ class Enum(String):
@property
def valid_values(self):
return tuple(self._valid_values)
return copy.copy(self._valid_values)
def coerce(self, obj, attr, value):
if value not in self._valid_values:

View File

@ -55,7 +55,7 @@ class FakeEnumAlt(fields.Enum):
PLATYPUS = "platypus"
AARDVARK = "aardvark"
ALL = (FROG, PLATYPUS, AARDVARK)
ALL = set([FROG, PLATYPUS, AARDVARK])
def __init__(self, **kwargs):
super(FakeEnumAlt, self).__init__(valid_values=FakeEnumAlt.ALL,
@ -256,6 +256,10 @@ class TestBaseEnum(TestField):
self.assertEqual(self.field.valid_values,
FakeEnum.ALL)
def test_valid_values_keeps_type(self):
self.assertIsInstance(self.field.valid_values, tuple)
self.assertIsInstance(FakeEnumAltField().valid_values, set)
class TestEnum(TestField):
def setUp(self):