Re: [Sqlalchemy-tickets] [sqlalchemy] #2872: AliasedClass.__getattr__() creates malformed Queryable
Brought to you by:
zzzeek
|
From: sqlalchemy <mi...@zz...> - 2013-11-23 05:30:02
|
#2872: AliasedClass.__getattr__() creates malformed QueryableAttribute
---------------------------+-------------------------------
Reporter: elic | Owner: zzzeek
Type: defect | Status: new
Priority: high | Milestone: 0.9.0
Component: orm | Severity: major - 1-3 hours
Resolution: | Keywords:
Progress State: in queue |
---------------------------+-------------------------------
Comment (by zzzeek):
here's a patch I find even more interesting, that is, let the contract of
`PropComparator.adapt_to_entity()` do the work. the code here has really
been kind of ad-hoc, thinking about it now might be shedding some clarity
on what it's really supposed to be doing. the tests all pass with this
except for a few that have this odd reversal of expressions thing going
on.
{{{
#!diff
diff --git a/lib/sqlalchemy/orm/attributes.py
b/lib/sqlalchemy/orm/attributes.py
index 6071b56..a46977e 100644
--- a/lib/sqlalchemy/orm/attributes.py
+++ b/lib/sqlalchemy/orm/attributes.py
@@ -149,6 +149,12 @@ class QueryableAttribute(interfaces._MappedAttribute,
return self.comparator._query_clause_element()
+ def adapt_to_entity(self, adapt_to_entity):
+ assert not self._of_type
+ return self.__class__(adapt_to_entity.entity, self.key,
impl=self.impl,
+
comparator=self.comparator.adapt_to_entity(adapt_to_entity),
+ parententity=adapt_to_entity)
+
def of_type(self, cls):
return QueryableAttribute(
self.class_,
diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py
index 9737072..1b8f53c 100644
--- a/lib/sqlalchemy/orm/util.py
+++ b/lib/sqlalchemy/orm/util.py
@@ -331,8 +331,10 @@ class AliasedClass(object):
else:
raise AttributeError(key)
- if isinstance(attr, attributes.QueryableAttribute):
- return _aliased_insp._adapt_prop(attr, key)
+ if isinstance(attr, PropComparator):
+ ret = attr.adapt_to_entity(_aliased_insp)
+ setattr(self, key, ret)
+ return ret
elif hasattr(attr, 'func_code'):
is_method = getattr(_aliased_insp._target, key, None)
if is_method and is_method.__self__ is not None:
@@ -343,7 +345,8 @@ class AliasedClass(object):
ret = attr.__get__(None, self)
if isinstance(ret, PropComparator):
return ret.adapt_to_entity(_aliased_insp)
- return ret
+ else:
+ return ret
else:
return attr
@@ -465,17 +468,6 @@ class AliasedInsp(_InspectionAttr):
'parentmapper': self.mapper}
)
- def _adapt_prop(self, existing, key):
- comparator = existing.comparator.adapt_to_entity(self)
- queryattr = attributes.QueryableAttribute(
- self.entity, key,
- impl=existing.impl,
- parententity=self,
- comparator=comparator)
- setattr(self.entity, key, queryattr)
- return queryattr
-
-
def _entity_for_mapper(self, mapper):
self_poly = self.with_polymorphic_mappers
if mapper in self_poly:
}}}
--
Ticket URL: <http://www.sqlalchemy.org/trac/ticket/2872#comment:2>
sqlalchemy <http://www.sqlalchemy.org/>
The Database Toolkit for Python
|