summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/ext
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2021-04-07 10:26:31 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2021-04-07 10:26:31 -0400
commit475a1ada5d3ac88f12080ef8672a8fda70c7e76e (patch)
tree9279db69a507c329530f0ba227a88a9c0308ff9c /lib/sqlalchemy/ext
parentd1847bbe268d48d34f4781b317ac8e6bf13d44bd (diff)
downloadsqlalchemy-475a1ada5d3ac88f12080ef8672a8fda70c7e76e.tar.gz
Check for hybrid's attribute name and support no name
Fixed regression where the ORM compilation scheme would assume the function name of a hybrid property would be the same as the attribute name in such a way that an ``AttributeError`` would be raised, when it would attempt to determine the correct name for each element in a result tuple. A similar issue exists in 1.3 but only impacts the names of tuple rows. The fix here adds a check that the hybrid's function name is actually present in the ``__dict__`` of the class or its superclasses before assigning this name; otherwise, the hybrid is considered to be "unnamed" and ORM result tuples will use the naming scheme of the underlying expression. Fixes: #6215 Change-Id: I584c0c05efec957f4dcaccf5df371399a57dffe9
Diffstat (limited to 'lib/sqlalchemy/ext')
-rw-r--r--lib/sqlalchemy/ext/hybrid.py41
1 files changed, 40 insertions, 1 deletions
diff --git a/lib/sqlalchemy/ext/hybrid.py b/lib/sqlalchemy/ext/hybrid.py
index 5fcb4fac0..378e6c18b 100644
--- a/lib/sqlalchemy/ext/hybrid.py
+++ b/lib/sqlalchemy/ext/hybrid.py
@@ -263,6 +263,34 @@ is supported, for more complex SET expressions it will usually be necessary
to use either the "fetch" or False synchronization strategy as illustrated
above.
+.. note:: For ORM bulk updates to work with hybrids, the function name
+ of the hybrid must match that of how it is accessed. Something
+ like this wouldn't work::
+
+ class Interval(object):
+ # ...
+
+ def _get(self):
+ return self.end - self.start
+
+ def _set(self, value):
+ self.end = self.start + value
+
+ def _update_expr(cls, value):
+ return [
+ (cls.end, cls.start + value)
+ ]
+
+ length = hybrid_property(
+ fget=_get, fset=_set, update_expr=_update_expr
+ )
+
+ The Python descriptor protocol does not provide any reliable way for
+ a descriptor to know what attribute name it was accessed as, and
+ the UPDATE scheme currently relies upon being able to access the
+ attribute from an instance by name in order to perform the instance
+ synchronization step.
+
.. versionadded:: 1.2 added support for bulk updates to hybrid properties.
Working with Relationships
@@ -1098,9 +1126,20 @@ class hybrid_property(interfaces.InspectionAttrInfo):
proxy_attr = attributes.create_proxied_attribute(self)
def expr_comparator(owner):
+ # because this is the descriptor protocol, we don't really know
+ # what our attribute name is. so search for it through the
+ # MRO.
+ for lookup in owner.__mro__:
+ if self.__name__ in lookup.__dict__:
+ if lookup.__dict__[self.__name__] is self:
+ name = self.__name__
+ break
+ else:
+ name = attributes.NO_KEY
+
return proxy_attr(
owner,
- self.__name__,
+ name,
self,
comparator(owner),
doc=comparator.__doc__ or self.__doc__,