question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hybrid decorators' "mutator decorators" actually mutate state, preventing specialising in a subclass

See original GitHub issue

Migrated issue, originally created by Markus Meskanen (@Mahi)

As it stands, hybrid_method and hybrid_property both mutate their state with the modifier decorators (setter, deleter, expression, comparator) in the following manner:

class hybrid_property:
    ...

    def setter(self, fset):
        self.fset = fset
        return self

But this prevents me from specialising the property in a subclass:

class Foo(Model):
    first_name = Column(String)
    last_name = Column(String)

    @hybrid_property
    def name(self):
        return self.first_name + ' ' + self.last_name

    @name.setter
    def name(self, value):
        self.first_name, self.last_name = value.split(' ', maxsplit=1)


class Bar(Foo):
    
    @Foo.name.setter
    def name(self, value):
        if run_name_check(value) > 0:  # Error code received
            raise InvalidName
        super().name.fset(value)

Since that would result into modifying the original Foo.name:

>>> f = Foo(first_name='Markus', last_name='Meskanen')
>>> f.name = 'Markus^% Meskanen'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
__main__.InvalidName

The correct way to implement these mutator decorators is to have them return a copy instance instead:

class hybrid_property:
    ...
    def setter(self, fset):
        return type(self)(self.fget, fset, self.fdel, self.expr)

I would PR such fix myself, but I’m in a hurry and couldn’t quicky figure out how the expressions and comparators work exactly, and wasn’t sure what is the intended behavior after this suggested fix would be applied.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
sqlalchemy-botcommented, Nov 27, 2018

Michael Bayer (@zzzeek) wrote:

Allow reuse of hybrid_property across subclasses

The :class:sqlalchemy.ext.hybrid.hybrid_property class now supports calling mutators like @setter, @expression etc. multiple times across subclasses, and now provides a @getter mutator, so that a particular hybrid can be repurposed across subclasses or other classes. This now matches the behavior of @property in standard Python.

Co-authored-by: Mike Bayer mike_mp@zzzcomputing.com Fixes: #3911 Fixes: #3912 Change-Id: Iff033d8ccaae20ded9289cbfa789c376759381f5

→ caeb274e287f

0reactions
sqlalchemy-botcommented, Nov 27, 2018

Changes by Michael Bayer (@zzzeek):

  • changed status to closed
Read more comments on GitHub >

github_iconTop Results From Across the Web

zzzeek / sqlalchemy_old / issues / #3912 - Hybrid decorators ...
Hybrid decorators ' "mutator decorators" actually mutate state, preventing specialising in a subclass. Issue #3912 resolved. Markus Meskanen created an issue ...
Read more >
1.2 Milestone · GitHub
Hybrid decorators ' "mutator decorators" actually mutate state, preventing specialising in a subclass feature sqlalchemy.ext extension modules, most of which ...
Read more >
CS 2112 Fall 2021 Lecture and Recitation Notes - Cornell CS
In practice, people often draw object diagrams using a hybrid approach in which ... Abstractions that do not have any mutators that can...
Read more >
Untitled
Gimnazjum w jerzmanowa, Mangalashtak once more full songs, Pjp4, Quad mz atv panther 50, Novasure post surgery, Iowa state university wind ensemble, ...
Read more >
Using capabilities for strict runtime invariant checking
The two main forms of invariant protocols are visible state ... use a small number of decorator-like design patterns to avoid exposing those ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found