From f7e5f8e67ada6b8da249f946f3c22b71f4849c6b Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Tue, 19 Apr 2016 10:22:24 -0700 Subject: [PATCH] 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 --- oslo_versionedobjects/fields.py | 2 +- oslo_versionedobjects/tests/test_fields.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/oslo_versionedobjects/fields.py b/oslo_versionedobjects/fields.py index 670f911d..edda623d 100644 --- a/oslo_versionedobjects/fields.py +++ b/oslo_versionedobjects/fields.py @@ -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: diff --git a/oslo_versionedobjects/tests/test_fields.py b/oslo_versionedobjects/tests/test_fields.py index 4479bf01..7eb17a97 100644 --- a/oslo_versionedobjects/tests/test_fields.py +++ b/oslo_versionedobjects/tests/test_fields.py @@ -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):