summaryrefslogtreecommitdiff
path: root/test/base/test_utils.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2020-05-03 20:27:24 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2020-05-03 20:33:17 -0400
commit35552e88ca798b809c7391bae11890c1557a3dd2 (patch)
treef1a689be9de573d6ba885a0921c5b123fbaa8ff5 /test/base/test_utils.py
parent00d40775b73cf94fa5d1b765dac1e600e93e172f (diff)
downloadsqlalchemy-35552e88ca798b809c7391bae11890c1557a3dd2.tar.gz
Don't apply sets or similar to objects in IdentitySet
Modified the internal "identity set" implementation, which is a set that hashes objects on their id() rather than their hash values, to not actually call the ``__hash__()`` method of the objects, which are typically user-mapped objects. Some methods were calling this method as a side effect of the implementation. Fixes: #5304 Change-Id: I0ed8762f47622215a54dcad9f210377b1becf8e8
Diffstat (limited to 'test/base/test_utils.py')
-rw-r--r--test/base/test_utils.py27
1 files changed, 23 insertions, 4 deletions
diff --git a/test/base/test_utils.py b/test/base/test_utils.py
index 96a9f955a..a6d777c61 100644
--- a/test/base/test_utils.py
+++ b/test/base/test_utils.py
@@ -1058,6 +1058,13 @@ class HashOverride(object):
return hash(self.value)
+class NoHash(object):
+ def __init__(self, value=None):
+ self.value = value
+
+ __hash__ = None
+
+
class EqOverride(object):
def __init__(self, value=None):
self.value = value
@@ -1098,6 +1105,8 @@ class HashEqOverride(object):
class IdentitySetTest(fixtures.TestBase):
+ obj_type = object
+
def assert_eq(self, identityset, expected_iterable):
expected = sorted([id(o) for o in expected_iterable])
found = sorted([id(o) for o in identityset])
@@ -1127,7 +1136,7 @@ class IdentitySetTest(fixtures.TestBase):
ids.add(data[i])
self.assert_eq(ids, data)
- for type_ in (EqOverride, HashOverride, HashEqOverride):
+ for type_ in (NoHash, EqOverride, HashOverride, HashEqOverride):
data = [type_(1), type_(1), type_(2)]
ids = util.IdentitySet()
for i in list(range(3)) + list(range(3)):
@@ -1136,7 +1145,7 @@ class IdentitySetTest(fixtures.TestBase):
def test_dunder_sub2(self):
IdentitySet = util.IdentitySet
- o1, o2, o3 = object(), object(), object()
+ o1, o2, o3 = self.obj_type(), self.obj_type(), self.obj_type()
ids1 = IdentitySet([o1])
ids2 = IdentitySet([o1, o2, o3])
eq_(ids2 - ids1, IdentitySet([o2, o3]))
@@ -1549,7 +1558,13 @@ class IdentitySetTest(fixtures.TestBase):
pass # TODO
def _create_sets(self):
- o1, o2, o3, o4, o5 = object(), object(), object(), object(), object()
+ o1, o2, o3, o4, o5 = (
+ self.obj_type(),
+ self.obj_type(),
+ self.obj_type(),
+ self.obj_type(),
+ self.obj_type(),
+ )
super_ = util.IdentitySet([o1, o2, o3])
sub_ = util.IdentitySet([o2])
twin1 = util.IdentitySet([o3])
@@ -1573,7 +1588,7 @@ class IdentitySetTest(fixtures.TestBase):
def test_basic_sanity(self):
IdentitySet = util.IdentitySet
- o1, o2, o3 = object(), object(), object()
+ o1, o2, o3 = self.obj_type(), self.obj_type(), self.obj_type()
ids = IdentitySet([o1])
ids.discard(o1)
ids.discard(o1)
@@ -1638,6 +1653,10 @@ class IdentitySetTest(fixtures.TestBase):
assert_raises(TypeError, hash, ids)
+class NoHashIdentitySetTest(IdentitySetTest):
+ obj_type = NoHash
+
+
class OrderedIdentitySetTest(fixtures.TestBase):
def assert_eq(self, identityset, expected_iterable):
expected = [id(o) for o in expected_iterable]