module documentation

Defines instrumentation for class attributes and their interaction with instances. This module is usually not directly visible to user applications, but defines a large part of the ORM's interactivity.

Class AdHocHasEntityNamespace Undocumented
Class AttributeEventToken A token propagated throughout the course of a chain of attribute events.
Class AttributeImpl internal implementation for instrumented attributes.
Class CollectionAttributeImpl A collection-holding attribute that instruments changes in membership.
Class HasCollectionAdapter Undocumented
Class History A 3-tuple of added, unchanged and deleted values, representing the changes which have occurred on an instrumented attribute.
Class InstrumentedAttribute Class bound instrumented attribute which adds basic :term:`descriptor` methods.
Class QueryableAttribute Base class for :term:`descriptor` objects that intercept attribute events on behalf of a :class:`.MapperProperty` object. The actual :class:`.MapperProperty` is accessible via the :attr:`.QueryableAttribute...
Class ScalarAttributeImpl represents a scalar value-holding InstrumentedAttribute.
Class ScalarObjectAttributeImpl represents a scalar-holding InstrumentedAttribute, where the target object is also instrumented.
Function backref_listeners Apply listeners to synchronize a two-way relationship.
Function create_proxied_attribute Create an QueryableAttribute / user descriptor hybrid.
Function del_attribute Delete the value of an attribute, firing history events.
Function flag_dirty Mark an instance as 'dirty' without any specific attribute mentioned.
Function flag_modified Mark an attribute on an instance as 'modified'.
Function get_attribute Get the value of an attribute, firing any callables required.
Function get_history Return a :class:`.History` record for the given object and attribute key.
Function get_state_history Undocumented
Function has_parent TODO
Function init_collection Initialize a collection attribute and return the collection adapter.
Function init_state_collection Initialize a collection attribute and return the collection adapter.
Function register_attribute Undocumented
Function register_attribute_impl Undocumented
Function register_descriptor Undocumented
Function set_attribute Set the value of an attribute, firing history events.
Function set_committed_value Set the value of an attribute with no history events.
Function unregister_attribute Undocumented
Constant HISTORY_BLANK Undocumented
Constant OP_APPEND Undocumented
Constant OP_BULK_REPLACE Undocumented
Constant OP_MODIFIED Undocumented
Constant OP_REMOVE Undocumented
Constant OP_REPLACE Undocumented
Function _is_collection_attribute_impl Undocumented
Function _queryable_attribute_unreduce Undocumented
Constant _NO_HISTORY Undocumented
Constant _NO_STATE_SYMBOLS Undocumented
Constant _UNKNOWN_ATTR_KEY Undocumented
Type Variable _T Undocumented
Type Alias _AllPendingType Undocumented
def backref_listeners(attribute: QueryableAttribute[Any], key: str, uselist: bool): (source)

Apply listeners to synchronize a two-way relationship.

def create_proxied_attribute(descriptor: Any) -> Callable[..., QueryableAttribute[Any]]: (source)

Create an QueryableAttribute / user descriptor hybrid. Returns a new QueryableAttribute type that delegates descriptor behavior and getattr() to the given descriptor.

def del_attribute(instance: object, key: str): (source)

Delete the value of an attribute, firing history events. This function may be used regardless of instrumentation applied directly to the class, i.e. no descriptors are required. Custom attribute management schemes will need to make usage of this method to establish attribute state as understood by SQLAlchemy.

def flag_dirty(instance: object): (source)

Mark an instance as 'dirty' without any specific attribute mentioned. This is a special operation that will allow the object to travel through the flush process for interception by events such as :meth:`.SessionEvents.before_flush`. Note that no SQL will be emitted in the flush process for an object that has no changes, even if marked dirty via this method. However, a :meth:`.SessionEvents.before_flush` handler will be able to see the object in the :attr:`.Session.dirty` collection and may establish changes on it, which will then be included in the SQL emitted. .. versionadded:: 1.2 .. seealso:: :func:`.attributes.flag_modified`

def flag_modified(instance: object, key: str): (source)

Mark an attribute on an instance as 'modified'. This sets the 'modified' flag on the instance and establishes an unconditional change event for the given attribute. The attribute must have a value present, else an :class:`.InvalidRequestError` is raised. To mark an object "dirty" without referring to any specific attribute so that it is considered within a flush, use the :func:`.attributes.flag_dirty` call. .. seealso:: :func:`.attributes.flag_dirty`

def get_attribute(instance: object, key: str) -> Any: (source)

Get the value of an attribute, firing any callables required. This function may be used regardless of instrumentation applied directly to the class, i.e. no descriptors are required. Custom attribute management schemes will need to make usage of this method to make usage of attribute state as understood by SQLAlchemy.

def get_history(obj: object, key: str, passive: PassiveFlag = PASSIVE_OFF) -> History: (source)

Return a :class:`.History` record for the given object and attribute key. This is the **pre-flush** history for a given attribute, which is reset each time the :class:`.Session` flushes changes to the current database transaction. .. note:: Prefer to use the :attr:`.AttributeState.history` and :meth:`.AttributeState.load_history` accessors to retrieve the :class:`.History` for instance attributes. :param obj: an object whose class is instrumented by the attributes package. :param key: string attribute name. :param passive: indicates loading behavior for the attribute if the value is not already present. This is a bitflag attribute, which defaults to the symbol :attr:`.PASSIVE_OFF` indicating all necessary SQL should be emitted. .. seealso:: :attr:`.AttributeState.history` :meth:`.AttributeState.load_history` - retrieve history using loader callables if the value is not locally present.

def get_state_history(state: InstanceState[Any], key: str, passive: PassiveFlag = PASSIVE_OFF) -> History: (source)

Undocumented

def has_parent(cls: Type[_O], obj: _O, key: str, optimistic: bool = False) -> bool: (source)

TODO

def init_collection(obj: object, key: str) -> CollectionAdapter: (source)

Initialize a collection attribute and return the collection adapter. This function is used to provide direct access to collection internals for a previously unloaded attribute. e.g.:: collection_adapter = init_collection(someobject, 'elements') for elem in values: collection_adapter.append_without_event(elem) For an easier way to do the above, see :func:`~sqlalchemy.orm.attributes.set_committed_value`. :param obj: a mapped object :param key: string attribute name where the collection is located.

def init_state_collection(state: InstanceState[Any], dict_: _InstanceDict, key: str) -> CollectionAdapter: (source)

Initialize a collection attribute and return the collection adapter. Discards any existing collection which may be there.

def register_attribute(class_: Type[_O], key: str, *, comparator: interfaces.PropComparator[_T], parententity: _InternalEntityType[_O], doc: Optional[str] = None, **kw: Any) -> InstrumentedAttribute[_T]: (source)

Undocumented

def register_attribute_impl(class_: Type[_O], key: str, uselist: bool = False, callable_: Optional[_LoaderCallable] = None, useobject: bool = False, impl_class: Optional[Type[AttributeImpl]] = None, backref: Optional[str] = None, **kw: Any) -> QueryableAttribute[Any]: (source)

Undocumented

def register_descriptor(class_: Type[Any], key: str, *, comparator: interfaces.PropComparator[_T], parententity: _InternalEntityType[Any], doc: Optional[str] = None) -> InstrumentedAttribute[_T]: (source)

Undocumented

def set_attribute(instance: object, key: str, value: Any, initiator: Optional[AttributeEventToken] = None): (source)

Set the value of an attribute, firing history events. This function may be used regardless of instrumentation applied directly to the class, i.e. no descriptors are required. Custom attribute management schemes will need to make usage of this method to establish attribute state as understood by SQLAlchemy. :param instance: the object that will be modified :param key: string name of the attribute :param value: value to assign :param initiator: an instance of :class:`.Event` that would have been propagated from a previous event listener. This argument is used when the :func:`.set_attribute` function is being used within an existing event listening function where an :class:`.Event` object is being supplied; the object may be used to track the origin of the chain of events. .. versionadded:: 1.2.3

def set_committed_value(instance, key, value): (source)

Set the value of an attribute with no history events. Cancels any previous history present. The value should be a scalar value for scalar-holding attributes, or an iterable for any collection-holding attribute. This is the same underlying method used when a lazy loader fires off and loads additional data from the database. In particular, this method can be used by application code which has loaded additional attributes or collections through separate queries, which can then be attached to an instance as though it were part of its original loaded state.

def unregister_attribute(class_: Type[Any], key: str): (source)

Undocumented

HISTORY_BLANK = (source)

Undocumented

Value
History((), (), ())
OP_APPEND = (source)

Undocumented

Value
util.symbol('APPEND')
OP_BULK_REPLACE = (source)

Undocumented

Value
util.symbol('BULK_REPLACE')
OP_MODIFIED = (source)

Undocumented

Value
util.symbol('MODIFIED')
OP_REMOVE = (source)

Undocumented

Value
util.symbol('REMOVE')
OP_REPLACE = (source)

Undocumented

Value
util.symbol('REPLACE')
def _is_collection_attribute_impl(impl: AttributeImpl) -> TypeGuard[CollectionAttributeImpl]: (source)

Undocumented

def _queryable_attribute_unreduce(key: str, mapped_class: Type[_O], parententity: _InternalEntityType[_O], entity: _ExternalEntityType[Any]) -> Any: (source)

Undocumented

_NO_HISTORY = (source)

Undocumented

Value
util.symbol('NO_HISTORY')
_NO_STATE_SYMBOLS = (source)

Undocumented

Value
frozenset([id(PASSIVE_NO_RESULT), id(NO_VALUE)])
_UNKNOWN_ATTR_KEY = (source)

Undocumented

Value
object()

Undocumented

Value
TypeVar('_T')
_AllPendingType = (source)

Undocumented

Value
Sequence[Tuple[Optional['InstanceState[Any]'], Optional[object]]]