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>
This commit is contained in:
Doug Hellmann 2018-04-19 11:56:49 -04:00
parent 0e70c4c0d8
commit ad93ae1f07
2 changed files with 54 additions and 4 deletions

View File

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

View File

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