diff options
author | Doug Hellmann <doug@doughellmann.com> | 2018-04-19 11:56:49 -0400 |
---|---|---|
committer | Doug Hellmann <doug@doughellmann.com> | 2018-04-20 17:04:07 -0400 |
commit | ad93ae1f0775f2b2991912ecf81e8c3c1bbb14c0 (patch) | |
tree | d2a678f8854e516713d90e3aca8658ffed457efc | |
parent | 0e70c4c0d877b07c7e037c3f817591b66baf8ff1 (diff) | |
download | oslo-config-ad93ae1f0775f2b2991912ecf81e8c3c1bbb14c0.tar.gz |
fix URI type equality comparison
It does not make sense to compare the value of two URIs for equality
when trying to determine if the *types* are equal, so change __eq__ to
look at the max_length and schemes, which do describe the type instead
of an instance of a URL.
Since the value property is not used for anything else, it is marked
deprecated so we can remove it in a future version.
Change-Id: Ia5ba7d13a0b46b357c9225d4f71f770642c14f61
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
-rw-r--r-- | oslo_config/tests/test_types.py | 29 | ||||
-rw-r--r-- | oslo_config/types.py | 29 |
2 files changed, 54 insertions, 4 deletions
diff --git a/oslo_config/tests/test_types.py b/oslo_config/tests/test_types.py index 666a11f..13ac650 100644 --- a/oslo_config/tests/test_types.py +++ b/oslo_config/tests/test_types.py @@ -856,6 +856,35 @@ class URITypeTests(TypeTestHelper, unittest.TestCase): self.assertConvertedValue('http://www.example.com', 'http://www.example.com') + def test_equality(self): + a = types.URI() + b = types.URI() + self.assertEqual(a, b) + + def test_equality_length(self): + a = types.URI(max_length=5) + b = types.URI(max_length=5) + self.assertEqual(a, b) + + def test_equality_length_not(self): + a = types.URI() + b = types.URI(max_length=5) + c = types.URI(max_length=10) + self.assertNotEqual(a, b) + self.assertNotEqual(c, b) + + def test_equality_schemes(self): + a = types.URI(schemes=['ftp']) + b = types.URI(schemes=['ftp']) + self.assertEqual(a, b) + + def test_equality_schemes_not(self): + a = types.URI() + b = types.URI(schemes=['ftp']) + c = types.URI(schemes=['http']) + self.assertNotEqual(a, b) + self.assertNotEqual(c, b) + class PortTypeTests(TypeTestHelper, unittest.TestCase): type = types.Port() diff --git a/oslo_config/types.py b/oslo_config/types.py index 06a81dd..74242c1 100644 --- a/oslo_config/types.py +++ b/oslo_config/types.py @@ -25,6 +25,7 @@ import re import warnings import abc +from debtcollector import removals import netaddr import rfc3986 import six @@ -891,17 +892,37 @@ class URI(ConfigType): raise ValueError("URI scheme '%s' not in %s" % (scheme, self.schemes)) - self.value = value + # NOTE(dhellmann): self.value is deprecated, and we don't want + # to trigger a deprecation warning ourselves so we modify + # self._value directly. + self._value = value return value + @removals.removed_property + def value(self): + return self._value + + @value.setter + def value(self, newval): + self._value = newval + + @value.deleter + def value(self): + del self._value + def __repr__(self): return 'URI' def __eq__(self, other): - return ( - (self.__class__ == other.__class__) and - (self.value == other.value) + to_compare = ['__class__', 'max_length', 'schemes'] + unset = object() + my_values = tuple( + getattr(self, name, unset) for name in to_compare + ) + other_values = tuple( + getattr(other, name, unset) for name in to_compare ) + return my_values == other_values def _formatter(self, value): return value |