From: Mike Bayer Date: Mon, 25 Jul 2011 00:41:42 +0000 (-0400) Subject: - rework Annotated to no longer use __cmp__(), supply an __eq__() that X-Git-Tag: rel_0_7_2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5c7cc274250c54430d9dceb05ad2a4a33e2378d7;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - rework Annotated to no longer use __cmp__(), supply an __eq__() that works with ColumnElement as well as works with non-__eq__() suppliers, works with sets, on Py3K as well. --- diff --git a/lib/sqlalchemy/sql/util.py b/lib/sqlalchemy/sql/util.py index ed0afef243..fa65070aa8 100644 --- a/lib/sqlalchemy/sql/util.py +++ b/lib/sqlalchemy/sql/util.py @@ -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__()) diff --git a/test/sql/test_selectable.py b/test/sql/test_selectable.py index 555271f16f..82d018af15 100644 --- a/test/sql/test_selectable.py +++ b/test/sql/test_selectable.py @@ -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):