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:
@@ -856,6 +856,35 @@ class URITypeTests(TypeTestHelper, unittest.TestCase):
|
|||||||
self.assertConvertedValue('http://www.example.com',
|
self.assertConvertedValue('http://www.example.com',
|
||||||
'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):
|
class PortTypeTests(TypeTestHelper, unittest.TestCase):
|
||||||
type = types.Port()
|
type = types.Port()
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import re
|
|||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
import abc
|
import abc
|
||||||
|
from debtcollector import removals
|
||||||
import netaddr
|
import netaddr
|
||||||
import rfc3986
|
import rfc3986
|
||||||
import six
|
import six
|
||||||
@@ -891,17 +892,37 @@ class URI(ConfigType):
|
|||||||
raise ValueError("URI scheme '%s' not in %s" %
|
raise ValueError("URI scheme '%s' not in %s" %
|
||||||
(scheme, self.schemes))
|
(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
|
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):
|
def __repr__(self):
|
||||||
return 'URI'
|
return 'URI'
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return (
|
to_compare = ['__class__', 'max_length', 'schemes']
|
||||||
(self.__class__ == other.__class__) and
|
unset = object()
|
||||||
(self.value == other.value)
|
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):
|
def _formatter(self, value):
|
||||||
return value
|
return value
|
||||||
|
|||||||
Reference in New Issue
Block a user