Hybrid Attribut raises a TypeError when the getter calls `datetime.date.strftime` on an DATE-attribute

Issue #2705 resolved
Former user created an issue

Please see the attached example.py: if you uncomment line 21, there is no error.

tested on versions: - '0.7.4' - '0.8.0'

Comments (1)

  1. Mike Bayer repo owner

    well this one there's an issue with your class' behavioral contract. You're saying Example(my_date=x), which means you're asking declarative to use the default constructor to establish this value, but it checks it by asking hasattr(Example, 'my_date').

    What happens if we do that?

    assert hasattr(Example, "my_date")
    

    it asserts False. Because, your hybrid is assuming that it's called only as an in-Python expression, not a SQL expression. The Example._date attribute doesn't have a strftime() method, you'd need to implement this as an equivalent SQL expression, otherwise there's not much point in using @hybrid_property, you would just use @property instead.

    So here it'll work if you put something that's compatible with Example.my_date:

        @hybrid_property
        def my_date(self):
            return self._date.strftime(DATE_FORMAT)
    
        @my_date.expression
        def my_date(self):
            return self._date
    

    I used pdb.set_trace() inside of def my_date() to figure this out.

  2. Log in to comment