Return the :class:`.Session` to which the given instance belongs. This is essentially the same as the :attr:`.InstanceState.session` accessor. See that attribute for details.
(instance: object)
| 5286 | |
| 5287 | |
| 5288 | def object_session(instance: object) -> Optional[Session]: |
| 5289 | """Return the :class:`.Session` to which the given instance belongs. |
| 5290 | |
| 5291 | This is essentially the same as the :attr:`.InstanceState.session` |
| 5292 | accessor. See that attribute for details. |
| 5293 | |
| 5294 | """ |
| 5295 | |
| 5296 | try: |
| 5297 | state = attributes.instance_state(instance) |
| 5298 | except exc.NO_STATE as err: |
| 5299 | raise exc.UnmappedInstanceError(instance) from err |
| 5300 | else: |
| 5301 | return _state_session(state) |
| 5302 | |
| 5303 | |
| 5304 | _new_sessionid = util.counter() |