]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- rework Annotated to no longer use __cmp__(), supply an __eq__() that
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 25 Jul 2011 00:41:42 +0000 (20:41 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 25 Jul 2011 00:41:42 +0000 (20:41 -0400)
works with ColumnElement as well as works with non-__eq__() suppliers,
works with sets, on Py3K as well.

lib/sqlalchemy/sql/util.py
test/sql/test_selectable.py

index ed0afef2431a3a47f9583df2006573073b35e0ca..fa65070aa852a17fc20f2276f8f30c05c02d666d 100644 (file)
@@ -369,8 +369,12 @@ class Annotated(object):
     def __hash__(self):
         return hash(self.__element)
 
-    def __cmp__(self, other):
-        return cmp(hash(self.__element), hash(other))
+    def __eq__(self, other):
+        if isinstance(self.__element, expression.ColumnOperators):
+            return self.__element.__class__.__eq__(self, other)
+        else:
+            return hash(other) == hash(self)
+
 
 # hard-generate Annotated subclasses.  this technique
 # is used instead of on-the-fly types (i.e. type.__new__())
index 555271f16f799ec4474453404da6ab8d74795194..82d018af15157b79f6259b75d1c47ec5bfb6b155 100644 (file)
@@ -846,6 +846,24 @@ class DerivedTest(fixtures.TestBase, AssertsExecutionResults):
         assert not t2.select().alias('foo').is_derived_from(t1)
 
 class AnnotationsTest(fixtures.TestBase):
+
+    def test_hashing(self):
+        t = table('t', column('x'))
+
+        a = t.alias()
+        s = t.select()
+        s2 = a.select()
+
+        for obj in [
+            t,
+            t.c.x,
+            a,
+            s,
+            s2
+        ]:
+            annot = obj._annotate({})
+            eq_(set([obj]), set([annot]))
+
     def test_custom_constructions(self):
         from sqlalchemy.schema import Column
         class MyColumn(Column):