Release: 2.0.25 | Release Date: January 2, 2024

# SQLAlchemy 2.0 Documentation

# Alternate Class Instrumentation

Extensible class instrumentation.

The `sqlalchemy.ext.instrumentation` package provides for alternate systems of class instrumentation within the ORM. Class instrumentation refers to how the ORM places attributes on the class which maintain data and track changes to that data, as well as event hooks installed on the class.

Note

The extension package is provided for the benefit of integration with other object management packages, which already perform their own instrumentation. It is not intended for general use.

For examples of how the instrumentation extension is used, see the example Attribute Instrumentation.

## API Reference

Object Name | Description
--- | ---
ExtendedInstrumentationRegistry | Extends `InstrumentationFactory` with additional bookkeeping, to accommodate multiple types of class managers.
instrumentation_finders | An extensible sequence of callables which return instrumentation implementations
INSTRUMENTATION_MANAGER | Attribute, elects custom instrumentation when present on a mapped class.
InstrumentationFactory | Factory for new ClassManager instances.
InstrumentationManager | User-defined class instrumentation extension.

sqlalchemy.ext.instrumentation.INSTRUMENTATION_MANAGER = '__sa_instrumentation_manager__'

  Attribute, elects custom instrumentation when present on a mapped class.

  Allows a class to specify a slightly or wildly different technique for tracking changes made to mapped attributes and collections.

  Only one instrumentation implementation is allowed in a given object inheritance hierarchy.

  The value of this attribute must be a callable and will be passed a class object. The callable must return one of:

  > - An instance of an `InstrumentationManager` or subclass

  > - An object implementing all or some of InstrumentationManager (TODO)

  > - A dictionary of callables, implementing all or some of the above (TODO)

  > - An instance of a `ClassManager` or subclass

  This attribute is consulted by SQLAlchemy instrumentation resolution, once the `sqlalchemy.ext.instrumentation` module has been imported. If custom finders are installed in the global instrumentation_finders list, they may or may not choose to honor this attribute.

class sqlalchemy.orm.instrumentation.InstrumentationFactory

  Factory for new ClassManager instances.

  Class signature

  class `sqlalchemy.orm.instrumentation.InstrumentationFactory` (`sqlalchemy.event.registry.EventTarget`)

class sqlalchemy.ext.instrumentation.InstrumentationManager

  User-defined class instrumentation extension.

  `InstrumentationManager` can be subclassed in order to change how class instrumentation proceeds. This class exists for the purposes of integration with other object management frameworks which would like to entirely modify the instrumentation methodology of the ORM, and is not intended for regular usage. For interception of class instrumentation events, see `InstrumentationEvents`.

  Members

  dict_getter(), get_instance_dict(), initialize_instance_dict(), install_descriptor(), install_member(), install_state(), instrument_attribute(), instrument_collection_class(), manage(), manager_getter(), post_configure_attribute(), remove_state(), state_getter(), uninstall_descriptor(), uninstall_member(), unregister()

  The API for this class should be considered as semi-stable, and may change slightly with new releases.

  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` dict_getter(class_)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` get_instance_dict(class_, instance)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` initialize_instance_dict(class_, instance)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` install_descriptor(class_, key, inst)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` install_member(class_, key, implementation)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` install_state(class_, instance, state)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` instrument_attribute(class_, key, inst)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` instrument_collection_class(class_, key, collection_class)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` manage(class_, manager)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` manager_getter(class_)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` post_configure_attribute(class_, key, inst)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` remove_state(class_, instance)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` state_getter(class_)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` uninstall_descriptor(class_, key)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` uninstall_member(class_, key)
  method `sqlalchemy.ext.instrumentation.InstrumentationManager.` unregister(class_, manager)

sqlalchemy.ext.instrumentation.instrumentation_finders = [<function find_native_user_instrumentation_hook>]

  An extensible sequence of callables which return instrumentation implementations

  When a class is registered, each callable will be passed a class object. If None is returned, the next finder in the sequence is consulted. Otherwise the return must be an instrumentation factory that follows the same guidelines as sqlalchemy.ext.instrumentation.INSTRUMENTATION_MANAGER.

  By default, the only finder is find_native_user_instrumentation_hook, which searches for INSTRUMENTATION_MANAGER. If all finders return None, standard ClassManager instrumentation is used.

class sqlalchemy.ext.instrumentation.ExtendedInstrumentationRegistry

  Extends `InstrumentationFactory` with additional bookkeeping, to accommodate multiple types of class managers.

  Class signature

  class `sqlalchemy.ext.instrumentation.ExtendedInstrumentationRegistry` (`sqlalchemy.orm.instrumentation.InstrumentationFactory`)