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)
| 5448 | |
| 5449 | |
| 5450 | def object_session(instance: object) -> Optional[Session]: |
| 5451 | """Return the :class:`.Session` to which the given instance belongs. |
| 5452 | |
| 5453 | This is essentially the same as the :attr:`.InstanceState.session` |
| 5454 | accessor. See that attribute for details. |
| 5455 | |
| 5456 | """ |
| 5457 | |
| 5458 | try: |
| 5459 | state = attributes.instance_state(instance) |
| 5460 | except exc.NO_STATE as err: |
| 5461 | raise exc.UnmappedInstanceError(instance) from err |
| 5462 | else: |
| 5463 | return _state_session(state) |
| 5464 | |
| 5465 | |
| 5466 | _new_sessionid = util.counter() |