Hybrid Property vs Hybrid Method Expression Names

411 views
Skip to first unread message

ja...@cryptosense.com

unread,
Sep 13, 2018, 9:45:56 AM9/13/18
to sqlalchemy
Hi,

I've been adding a hybrid property to a model as per the documentation at http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html. In particular, the behavior is complicated enough that I need to define the expression version of the property separate from the normal version.

My model looks like this:

class MyModel(object):
   
# ...

   
@hybrid_property
   
def my_property(self):
       
# Do some stuff at the instance level

   
@my_property.expression
   
def my_property(cls):
       
# Do some stuff at the class level

This works as expected, but if I change the name of the class function it suddenly stops working - based on the error it seems like the renamed class function is being ignored and the instance function is being used instead. Weirdly, this does not happen when using hybrid methods rather than properties - so the following works as expected:

class MyModel(object):
   
# ...

   
@hybrid_method
   
def my_property(self):
       
# Do some stuff at the instance level

   
@my_property.expression
   
def my_property_expression(cls):
       
# Do some stuff at the class level

This is confusing - from the look of the code, the two decorators seem to work in the same way, so I am not sure why the renaming matters to the property and not the method. Is this expected behaviour?

Thanks,

James



ja...@cryptosense.com

unread,
Sep 13, 2018, 9:55:27 AM9/13/18
to sqlalchemy
Update: I have just found http://docs.sqlalchemy.org/en/latest/changelog/migration_12.html#hybrid-attributes-support-reuse-among-subclasses-redefinition-of-getter which documents that getters and setters must have the same name as the original expression.

Can I just check that it is expected for expressions to have this requirement? If so, is it worth opening a docs issue to add this to the main hybrid property docs?

Mike Bayer

unread,
Sep 13, 2018, 11:45:05 AM9/13/18
to sqlal...@googlegroups.com
this is the mechanics of Python, when you say:

@mything.foobar
def _myotherthing(...)


you are assigning to the name "_myotherthing". Since
@hybrid_property now creates a copy when any modifier is called, the
original hybrid you have at "mything" was not changed.

All the documentation examples at
http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html
indicate using the same method name for each modification. I can
add more dragons to the hybrid docs as well clarifying that this
naming scheme is intentional and required, if that helps.



>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+...@googlegroups.com.
> To post to this group, send email to sqlal...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

ja...@cryptosense.com

unread,
Sep 14, 2018, 4:32:52 AM9/14/18
to sqlalchemy
Thanks for the help - I had missed the "copy vs modifying in place" difference between hybrid_method and hybrid_property.

I think adding another dragon would be helpful here, probably located in http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html#defining-expression-behavior-distinct-from-attribute-behavior. I'm happy to move this thread into a docs issue if that would be helpful.

ja...@cryptosense.com

unread,
Oct 4, 2018, 4:07:39 AM10/4/18
to sqlalchemy
To follow this up - what would be the best way to get these extra dragons in? I would be happy to submit a PR or something if that is easier.

Mike Bayer

unread,
Oct 4, 2018, 10:20:01 AM10/4/18
to sqlal...@googlegroups.com
a PR would at least remind me to look into it even if i dont use your
suggested verbiage as is, thanks!
Reply all
Reply to author
Forward
0 new messages