I thought about it more. You are correct, there's no way to make this work well (I didn't think of proxying __dict__, I thought of placing pre-bound object proxies inside __dict__ for relations with interface mimicking a-la Werkzeug's object proxies, but those will be brittle and not as least-astonishing as I'd like them to be).
I will take this to the flask-restful community. I wish Python had a proper serialization customization API (__reduce__ is oldish, not quite a standard, and probably not flexible enough for many needs), but here is probably not quite the place for such wishes. Thanks, - Yaniv On Monday, December 17, 2012 7:50:02 PM UTC+2, Michael Bayer wrote: > > > On Dec 17, 2012, at 12:35 PM, ya...@aknin.name <javascript:> wrote: > > Hi, > > SQLAlchemy populates an instance's __dict__ with lazily related instances > when the attributes for these instances are first accessed (see > here<https://groups.google.com/d/topic/sqlalchemy/CQTxTo4wdLg/discussion>for > an ancient and huge discussion on the topic; I thought it's better to > start afresh). > > This breaks code that assumes __dict__ encapsulates object state (see > here<https://github.com/twilio/flask-restful/issues/20>for a real world > example when using > flask-restful). > > > If flask-restful thinks it can get an accurate picture of a Python > object's state via __dict__ access alone, it is mistaken. Descriptors > are an extremely common system of providing automation behind attribute > access, and SQLAlchemy builds upon this standard system. Any > serialization system should provide for hooks to customize how the > serialization occurs, and assuming flask-restful provides these hooks, > that's the solution to take. > > > While several workarounds may alleviate this, I think the best approach > here would be to create proxy objects to all relations in any instance's > __dict__. These proxies can be transparently replaced with the real thing > upon first access > > > The Python descriptor already provides a mechanism for proxying data that > may or may not be inside of __dict__. > > It is essential in a system like SQLAlchemy that the structure of __dict__ > remain completely simple. Adding another layer of proxying into it would > be a massive performance hit and produce tons of bugs where proxy objects > don't act like the real thing. flask-restful needs to provide hooks for > alternate systems of serialization, plain and simple. > > > > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/qoamBFdnGaAJ. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.