New Features and Improvements - ORM
===================================
+.. _change_4257:
+
+info dictionary added to InstanceState
+--------------------------------------
+
+Added the ``.info`` dictionary to the :class:`.InstanceState` class, the object
+that comes from calling :func:`.inspect` on a mapped object. This allows custom
+recipes to add additional information about an object that will be carried
+along with that object's full lifecycle in memory::
+
+ from sqlalchemy import inspect
+
+ u1 = User(id=7, name='ed')
+
+ inspect(u1).info['user_info'] = '7|ed'
+
+
+:ticket:`4257`
+
Key Behavioral Changes - ORM
=============================
--- /dev/null
+.. change::
+ :tags: feature, orm
+ :tickets: 4257
+
+ Added ``.info`` dictionary to the :class:`.InstanceState` class, the object
+ that comes from calling :func:`.inspect` on a mapped object.
+
+ .. seealso::
+
+ :ref:`change_4257`
@inspection._self_inspects
-class InstanceState(interfaces.InspectionAttr):
+class InstanceState(interfaces.InspectionAttrInfo):
"""tracks state information at the instance level.
The :class:`.InstanceState` is a key object used by the
(k, self.__dict__[k]) for k in (
'committed_state', '_pending_mutations', 'modified',
'expired', 'callables', 'key', 'parents', 'load_options',
- 'class_', 'expired_attributes'
+ 'class_', 'expired_attributes', 'info'
) if k in self.__dict__
)
if self.load_path:
self.parents = state_dict.get('parents', {})
self.modified = state_dict.get('modified', False)
self.expired = state_dict.get('expired', False)
+ if 'info' in state_dict:
+ self.info.update(state_dict['info'])
if 'callables' in state_dict:
self.callables = state_dict['callables']
insp = inspect(u1)
is_(insp, instance_state(u1))
+ def test_instance_state_info(self):
+ User = self.classes.User
+ u1 = User()
+ insp = inspect(u1)
+ insp.info['some_key'] = 'value'
+ eq_(inspect(u1).info['some_key'], 'value')
+
def test_instance_state_attr(self):
User = self.classes.User
u1 = User(name='ed')
eq_(state.identity_token, None)
eq_(state.identity_key, (User, (1,), None))
+ def test_state_info_pickle(self):
+ users = self.tables.users
+ mapper(User, users)
+
+ u1 = User(id=1, name='ed')
+
+ sa.inspect(u1).info['some_key'] = 'value'
+
+ state_dict = sa.inspect(u1).__getstate__()
+
+ state = sa_state.InstanceState.__new__(sa_state.InstanceState)
+ state.__setstate__(state_dict)
+
+ u2 = state.obj()
+ eq_(sa.inspect(u2).info['some_key'], 'value')
+
@testing.requires.non_broken_pickle
def test_options_with_descriptors(self):
users, addresses, dingalings = (self.tables.users,