You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2282 lines
74KB

  1. # orm/attributes.py
  2. # Copyright (C) 2005-2021 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """Defines instrumentation for class attributes and their interaction
  8. with instances.
  9. This module is usually not directly visible to user applications, but
  10. defines a large part of the ORM's interactivity.
  11. """
  12. import operator
  13. from . import collections
  14. from . import exc as orm_exc
  15. from . import interfaces
  16. from .base import ATTR_EMPTY
  17. from .base import ATTR_WAS_SET
  18. from .base import CALLABLES_OK
  19. from .base import INIT_OK
  20. from .base import instance_dict
  21. from .base import instance_state
  22. from .base import instance_str
  23. from .base import LOAD_AGAINST_COMMITTED
  24. from .base import manager_of_class
  25. from .base import NEVER_SET # noqa
  26. from .base import NO_AUTOFLUSH
  27. from .base import NO_CHANGE # noqa
  28. from .base import NO_RAISE
  29. from .base import NO_VALUE
  30. from .base import NON_PERSISTENT_OK # noqa
  31. from .base import PASSIVE_CLASS_MISMATCH # noqa
  32. from .base import PASSIVE_NO_FETCH
  33. from .base import PASSIVE_NO_FETCH_RELATED # noqa
  34. from .base import PASSIVE_NO_INITIALIZE
  35. from .base import PASSIVE_NO_RESULT
  36. from .base import PASSIVE_OFF
  37. from .base import PASSIVE_ONLY_PERSISTENT
  38. from .base import PASSIVE_RETURN_NO_VALUE
  39. from .base import RELATED_OBJECT_OK # noqa
  40. from .base import SQL_OK # noqa
  41. from .base import state_str
  42. from .. import event
  43. from .. import exc
  44. from .. import inspection
  45. from .. import util
  46. from ..sql import base as sql_base
  47. from ..sql import roles
  48. from ..sql import traversals
  49. from ..sql import visitors
  50. class NoKey(str):
  51. pass
  52. NO_KEY = NoKey("no name")
  53. @inspection._self_inspects
  54. class QueryableAttribute(
  55. interfaces._MappedAttribute,
  56. interfaces.InspectionAttr,
  57. interfaces.PropComparator,
  58. traversals.HasCopyInternals,
  59. roles.JoinTargetRole,
  60. roles.OnClauseRole,
  61. sql_base.Immutable,
  62. sql_base.MemoizedHasCacheKey,
  63. ):
  64. """Base class for :term:`descriptor` objects that intercept
  65. attribute events on behalf of a :class:`.MapperProperty`
  66. object. The actual :class:`.MapperProperty` is accessible
  67. via the :attr:`.QueryableAttribute.property`
  68. attribute.
  69. .. seealso::
  70. :class:`.InstrumentedAttribute`
  71. :class:`.MapperProperty`
  72. :attr:`_orm.Mapper.all_orm_descriptors`
  73. :attr:`_orm.Mapper.attrs`
  74. """
  75. is_attribute = True
  76. # PropComparator has a __visit_name__ to participate within
  77. # traversals. Disambiguate the attribute vs. a comparator.
  78. __visit_name__ = "orm_instrumented_attribute"
  79. def __init__(
  80. self,
  81. class_,
  82. key,
  83. parententity,
  84. impl=None,
  85. comparator=None,
  86. of_type=None,
  87. extra_criteria=(),
  88. ):
  89. self.class_ = class_
  90. self.key = key
  91. self._parententity = parententity
  92. self.impl = impl
  93. self.comparator = comparator
  94. self._of_type = of_type
  95. self._extra_criteria = extra_criteria
  96. manager = manager_of_class(class_)
  97. # manager is None in the case of AliasedClass
  98. if manager:
  99. # propagate existing event listeners from
  100. # immediate superclass
  101. for base in manager._bases:
  102. if key in base:
  103. self.dispatch._update(base[key].dispatch)
  104. if base[key].dispatch._active_history:
  105. self.dispatch._active_history = True
  106. _cache_key_traversal = [
  107. ("key", visitors.ExtendedInternalTraversal.dp_string),
  108. ("_parententity", visitors.ExtendedInternalTraversal.dp_multi),
  109. ("_of_type", visitors.ExtendedInternalTraversal.dp_multi),
  110. ("_extra_criteria", visitors.InternalTraversal.dp_clauseelement_list),
  111. ]
  112. def __reduce__(self):
  113. # this method is only used in terms of the
  114. # sqlalchemy.ext.serializer extension
  115. return (
  116. _queryable_attribute_unreduce,
  117. (
  118. self.key,
  119. self._parententity.mapper.class_,
  120. self._parententity,
  121. self._parententity.entity,
  122. ),
  123. )
  124. @util.memoized_property
  125. def _supports_population(self):
  126. return self.impl.supports_population
  127. @property
  128. def _impl_uses_objects(self):
  129. return self.impl.uses_objects
  130. def get_history(self, instance, passive=PASSIVE_OFF):
  131. return self.impl.get_history(
  132. instance_state(instance), instance_dict(instance), passive
  133. )
  134. @util.memoized_property
  135. def info(self):
  136. """Return the 'info' dictionary for the underlying SQL element.
  137. The behavior here is as follows:
  138. * If the attribute is a column-mapped property, i.e.
  139. :class:`.ColumnProperty`, which is mapped directly
  140. to a schema-level :class:`_schema.Column` object, this attribute
  141. will return the :attr:`.SchemaItem.info` dictionary associated
  142. with the core-level :class:`_schema.Column` object.
  143. * If the attribute is a :class:`.ColumnProperty` but is mapped to
  144. any other kind of SQL expression other than a
  145. :class:`_schema.Column`,
  146. the attribute will refer to the :attr:`.MapperProperty.info`
  147. dictionary associated directly with the :class:`.ColumnProperty`,
  148. assuming the SQL expression itself does not have its own ``.info``
  149. attribute (which should be the case, unless a user-defined SQL
  150. construct has defined one).
  151. * If the attribute refers to any other kind of
  152. :class:`.MapperProperty`, including :class:`.RelationshipProperty`,
  153. the attribute will refer to the :attr:`.MapperProperty.info`
  154. dictionary associated with that :class:`.MapperProperty`.
  155. * To access the :attr:`.MapperProperty.info` dictionary of the
  156. :class:`.MapperProperty` unconditionally, including for a
  157. :class:`.ColumnProperty` that's associated directly with a
  158. :class:`_schema.Column`, the attribute can be referred to using
  159. :attr:`.QueryableAttribute.property` attribute, as
  160. ``MyClass.someattribute.property.info``.
  161. .. seealso::
  162. :attr:`.SchemaItem.info`
  163. :attr:`.MapperProperty.info`
  164. """
  165. return self.comparator.info
  166. @util.memoized_property
  167. def parent(self):
  168. """Return an inspection instance representing the parent.
  169. This will be either an instance of :class:`_orm.Mapper`
  170. or :class:`.AliasedInsp`, depending upon the nature
  171. of the parent entity which this attribute is associated
  172. with.
  173. """
  174. return inspection.inspect(self._parententity)
  175. @util.memoized_property
  176. def expression(self):
  177. """The SQL expression object represented by this
  178. :class:`.QueryableAttribute`.
  179. This will typically be an instance of a :class:`_sql.ColumnElement`
  180. subclass representing a column expression.
  181. """
  182. if self.key is NO_KEY:
  183. annotations = {"entity_namespace": self._entity_namespace}
  184. else:
  185. annotations = {
  186. "proxy_key": self.key,
  187. "proxy_owner": self._parententity,
  188. "entity_namespace": self._entity_namespace,
  189. }
  190. ce = self.comparator.__clause_element__()
  191. try:
  192. anno = ce._annotate
  193. except AttributeError as ae:
  194. util.raise_(
  195. exc.InvalidRequestError(
  196. 'When interpreting attribute "%s" as a SQL expression, '
  197. "expected __clause_element__() to return "
  198. "a ClauseElement object, got: %r" % (self, ce)
  199. ),
  200. from_=ae,
  201. )
  202. else:
  203. return anno(annotations)
  204. @property
  205. def _entity_namespace(self):
  206. return self._parententity
  207. @property
  208. def _annotations(self):
  209. return self.__clause_element__()._annotations
  210. def __clause_element__(self):
  211. return self.expression
  212. @property
  213. def _from_objects(self):
  214. return self.expression._from_objects
  215. def _bulk_update_tuples(self, value):
  216. """Return setter tuples for a bulk UPDATE."""
  217. return self.comparator._bulk_update_tuples(value)
  218. def adapt_to_entity(self, adapt_to_entity):
  219. assert not self._of_type
  220. return self.__class__(
  221. adapt_to_entity.entity,
  222. self.key,
  223. impl=self.impl,
  224. comparator=self.comparator.adapt_to_entity(adapt_to_entity),
  225. parententity=adapt_to_entity,
  226. )
  227. def of_type(self, entity):
  228. return QueryableAttribute(
  229. self.class_,
  230. self.key,
  231. self._parententity,
  232. impl=self.impl,
  233. comparator=self.comparator.of_type(entity),
  234. of_type=inspection.inspect(entity),
  235. extra_criteria=self._extra_criteria,
  236. )
  237. def and_(self, *other):
  238. return QueryableAttribute(
  239. self.class_,
  240. self.key,
  241. self._parententity,
  242. impl=self.impl,
  243. comparator=self.comparator.and_(*other),
  244. of_type=self._of_type,
  245. extra_criteria=self._extra_criteria + other,
  246. )
  247. def _clone(self, **kw):
  248. return QueryableAttribute(
  249. self.class_,
  250. self.key,
  251. self._parententity,
  252. impl=self.impl,
  253. comparator=self.comparator,
  254. of_type=self._of_type,
  255. extra_criteria=self._extra_criteria,
  256. )
  257. def label(self, name):
  258. return self.__clause_element__().label(name)
  259. def operate(self, op, *other, **kwargs):
  260. return op(self.comparator, *other, **kwargs)
  261. def reverse_operate(self, op, other, **kwargs):
  262. return op(other, self.comparator, **kwargs)
  263. def hasparent(self, state, optimistic=False):
  264. return self.impl.hasparent(state, optimistic=optimistic) is not False
  265. def __getattr__(self, key):
  266. try:
  267. return getattr(self.comparator, key)
  268. except AttributeError as err:
  269. util.raise_(
  270. AttributeError(
  271. "Neither %r object nor %r object associated with %s "
  272. "has an attribute %r"
  273. % (
  274. type(self).__name__,
  275. type(self.comparator).__name__,
  276. self,
  277. key,
  278. )
  279. ),
  280. replace_context=err,
  281. )
  282. def __str__(self):
  283. return "%s.%s" % (self.class_.__name__, self.key)
  284. @util.memoized_property
  285. def property(self):
  286. """Return the :class:`.MapperProperty` associated with this
  287. :class:`.QueryableAttribute`.
  288. Return values here will commonly be instances of
  289. :class:`.ColumnProperty` or :class:`.RelationshipProperty`.
  290. """
  291. return self.comparator.property
  292. def _queryable_attribute_unreduce(key, mapped_class, parententity, entity):
  293. # this method is only used in terms of the
  294. # sqlalchemy.ext.serializer extension
  295. if parententity.is_aliased_class:
  296. return entity._get_from_serialized(key, mapped_class, parententity)
  297. else:
  298. return getattr(entity, key)
  299. if util.py3k:
  300. from typing import TypeVar, Generic
  301. _T = TypeVar("_T")
  302. _Generic_T = Generic[_T]
  303. else:
  304. _Generic_T = type("_Generic_T", (), {})
  305. class Mapped(QueryableAttribute, _Generic_T):
  306. """Represent an ORM mapped :term:`descriptor` attribute for typing purposes.
  307. This class represents the complete descriptor interface for any class
  308. attribute that will have been :term:`instrumented` by the ORM
  309. :class:`_orm.Mapper` class. When used with typing stubs, it is the final
  310. type that would be used by a type checker such as mypy to provide the full
  311. behavioral contract for the attribute.
  312. .. tip::
  313. The :class:`_orm.Mapped` class represents attributes that are handled
  314. directly by the :class:`_orm.Mapper` class. It does not include other
  315. Python descriptor classes that are provided as extensions, including
  316. :ref:`hybrids_toplevel` and the :ref:`associationproxy_toplevel`.
  317. While these systems still make use of ORM-specific superclasses
  318. and structures, they are not :term:`instrumented` by the
  319. :class:`_orm.Mapper` and instead provide their own functionality
  320. when they are accessed on a class.
  321. When using the :ref:`SQLAlchemy Mypy plugin <mypy_toplevel>`, the
  322. :class:`_orm.Mapped` construct is used in typing annotations to indicate to
  323. the plugin those attributes that are expected to be mapped; the plugin also
  324. applies :class:`_orm.Mapped` as an annotation automatically when it scans
  325. through declarative mappings in :ref:`orm_declarative_table` style. For
  326. more indirect mapping styles such as
  327. :ref:`imperative table <orm_imperative_table_configuration>` it is
  328. typically applied explicitly to class level attributes that expect
  329. to be mapped based on a given :class:`_schema.Table` configuration.
  330. :class:`_orm.Mapped` is defined in the
  331. `sqlalchemy2-stubs <https://pypi.org/project/sqlalchemy2-stubs>`_ project
  332. as a :pep:`484` generic class which may subscribe to any arbitrary Python
  333. type, which represents the Python type handled by the attribute::
  334. class MyMappedClass(Base):
  335. __table_ = Table(
  336. "some_table", Base.metadata,
  337. Column("id", Integer, primary_key=True),
  338. Column("data", String(50)),
  339. Column("created_at", DateTime)
  340. )
  341. id : Mapped[int]
  342. data: Mapped[str]
  343. created_at: Mapped[datetime]
  344. For complete background on how to use :class:`_orm.Mapped` with
  345. pep-484 tools like Mypy, see the link below for background on SQLAlchemy's
  346. Mypy plugin.
  347. .. versionadded:: 1.4
  348. .. seealso::
  349. :ref:`mypy_toplevel` - complete background on Mypy integration
  350. """
  351. def __get__(self, instance, owner):
  352. raise NotImplementedError()
  353. def __set__(self, instance, value):
  354. raise NotImplementedError()
  355. def __delete__(self, instance):
  356. raise NotImplementedError()
  357. class InstrumentedAttribute(Mapped):
  358. """Class bound instrumented attribute which adds basic
  359. :term:`descriptor` methods.
  360. See :class:`.QueryableAttribute` for a description of most features.
  361. """
  362. inherit_cache = True
  363. def __set__(self, instance, value):
  364. self.impl.set(
  365. instance_state(instance), instance_dict(instance), value, None
  366. )
  367. def __delete__(self, instance):
  368. self.impl.delete(instance_state(instance), instance_dict(instance))
  369. def __get__(self, instance, owner):
  370. if instance is None:
  371. return self
  372. dict_ = instance_dict(instance)
  373. if self._supports_population and self.key in dict_:
  374. return dict_[self.key]
  375. else:
  376. try:
  377. state = instance_state(instance)
  378. except AttributeError as err:
  379. util.raise_(
  380. orm_exc.UnmappedInstanceError(instance),
  381. replace_context=err,
  382. )
  383. return self.impl.get(state, dict_)
  384. HasEntityNamespace = util.namedtuple(
  385. "HasEntityNamespace", ["entity_namespace"]
  386. )
  387. HasEntityNamespace.is_mapper = HasEntityNamespace.is_aliased_class = False
  388. def create_proxied_attribute(descriptor):
  389. """Create an QueryableAttribute / user descriptor hybrid.
  390. Returns a new QueryableAttribute type that delegates descriptor
  391. behavior and getattr() to the given descriptor.
  392. """
  393. # TODO: can move this to descriptor_props if the need for this
  394. # function is removed from ext/hybrid.py
  395. class Proxy(QueryableAttribute):
  396. """Presents the :class:`.QueryableAttribute` interface as a
  397. proxy on top of a Python descriptor / :class:`.PropComparator`
  398. combination.
  399. """
  400. _extra_criteria = ()
  401. def __init__(
  402. self,
  403. class_,
  404. key,
  405. descriptor,
  406. comparator,
  407. adapt_to_entity=None,
  408. doc=None,
  409. original_property=None,
  410. ):
  411. self.class_ = class_
  412. self.key = key
  413. self.descriptor = descriptor
  414. self.original_property = original_property
  415. self._comparator = comparator
  416. self._adapt_to_entity = adapt_to_entity
  417. self.__doc__ = doc
  418. _is_internal_proxy = True
  419. @property
  420. def _impl_uses_objects(self):
  421. return (
  422. self.original_property is not None
  423. and getattr(self.class_, self.key).impl.uses_objects
  424. )
  425. @property
  426. def _parententity(self):
  427. return inspection.inspect(self.class_, raiseerr=False)
  428. @property
  429. def _entity_namespace(self):
  430. if hasattr(self._comparator, "_parententity"):
  431. return self._comparator._parententity
  432. else:
  433. # used by hybrid attributes which try to remain
  434. # agnostic of any ORM concepts like mappers
  435. return HasEntityNamespace(self.class_)
  436. @property
  437. def property(self):
  438. return self.comparator.property
  439. @util.memoized_property
  440. def comparator(self):
  441. if callable(self._comparator):
  442. self._comparator = self._comparator()
  443. if self._adapt_to_entity:
  444. self._comparator = self._comparator.adapt_to_entity(
  445. self._adapt_to_entity
  446. )
  447. return self._comparator
  448. def adapt_to_entity(self, adapt_to_entity):
  449. return self.__class__(
  450. adapt_to_entity.entity,
  451. self.key,
  452. self.descriptor,
  453. self._comparator,
  454. adapt_to_entity,
  455. )
  456. def __get__(self, instance, owner):
  457. retval = self.descriptor.__get__(instance, owner)
  458. # detect if this is a plain Python @property, which just returns
  459. # itself for class level access. If so, then return us.
  460. # Otherwise, return the object returned by the descriptor.
  461. if retval is self.descriptor and instance is None:
  462. return self
  463. else:
  464. return retval
  465. def __str__(self):
  466. return "%s.%s" % (self.class_.__name__, self.key)
  467. def __getattr__(self, attribute):
  468. """Delegate __getattr__ to the original descriptor and/or
  469. comparator."""
  470. try:
  471. return getattr(descriptor, attribute)
  472. except AttributeError as err:
  473. if attribute == "comparator":
  474. util.raise_(
  475. AttributeError("comparator"), replace_context=err
  476. )
  477. try:
  478. # comparator itself might be unreachable
  479. comparator = self.comparator
  480. except AttributeError as err2:
  481. util.raise_(
  482. AttributeError(
  483. "Neither %r object nor unconfigured comparator "
  484. "object associated with %s has an attribute %r"
  485. % (type(descriptor).__name__, self, attribute)
  486. ),
  487. replace_context=err2,
  488. )
  489. else:
  490. try:
  491. return getattr(comparator, attribute)
  492. except AttributeError as err3:
  493. util.raise_(
  494. AttributeError(
  495. "Neither %r object nor %r object "
  496. "associated with %s has an attribute %r"
  497. % (
  498. type(descriptor).__name__,
  499. type(comparator).__name__,
  500. self,
  501. attribute,
  502. )
  503. ),
  504. replace_context=err3,
  505. )
  506. Proxy.__name__ = type(descriptor).__name__ + "Proxy"
  507. util.monkeypatch_proxied_specials(
  508. Proxy, type(descriptor), name="descriptor", from_instance=descriptor
  509. )
  510. return Proxy
  511. OP_REMOVE = util.symbol("REMOVE")
  512. OP_APPEND = util.symbol("APPEND")
  513. OP_REPLACE = util.symbol("REPLACE")
  514. OP_BULK_REPLACE = util.symbol("BULK_REPLACE")
  515. OP_MODIFIED = util.symbol("MODIFIED")
  516. class AttributeEvent(object):
  517. """A token propagated throughout the course of a chain of attribute
  518. events.
  519. Serves as an indicator of the source of the event and also provides
  520. a means of controlling propagation across a chain of attribute
  521. operations.
  522. The :class:`.Event` object is sent as the ``initiator`` argument
  523. when dealing with events such as :meth:`.AttributeEvents.append`,
  524. :meth:`.AttributeEvents.set`,
  525. and :meth:`.AttributeEvents.remove`.
  526. The :class:`.Event` object is currently interpreted by the backref
  527. event handlers, and is used to control the propagation of operations
  528. across two mutually-dependent attributes.
  529. .. versionadded:: 0.9.0
  530. :attribute impl: The :class:`.AttributeImpl` which is the current event
  531. initiator.
  532. :attribute op: The symbol :attr:`.OP_APPEND`, :attr:`.OP_REMOVE`,
  533. :attr:`.OP_REPLACE`, or :attr:`.OP_BULK_REPLACE`, indicating the
  534. source operation.
  535. """
  536. __slots__ = "impl", "op", "parent_token"
  537. def __init__(self, attribute_impl, op):
  538. self.impl = attribute_impl
  539. self.op = op
  540. self.parent_token = self.impl.parent_token
  541. def __eq__(self, other):
  542. return (
  543. isinstance(other, AttributeEvent)
  544. and other.impl is self.impl
  545. and other.op == self.op
  546. )
  547. @property
  548. def key(self):
  549. return self.impl.key
  550. def hasparent(self, state):
  551. return self.impl.hasparent(state)
  552. Event = AttributeEvent
  553. class AttributeImpl(object):
  554. """internal implementation for instrumented attributes."""
  555. def __init__(
  556. self,
  557. class_,
  558. key,
  559. callable_,
  560. dispatch,
  561. trackparent=False,
  562. compare_function=None,
  563. active_history=False,
  564. parent_token=None,
  565. load_on_unexpire=True,
  566. send_modified_events=True,
  567. accepts_scalar_loader=None,
  568. **kwargs
  569. ):
  570. r"""Construct an AttributeImpl.
  571. :param \class_: associated class
  572. :param key: string name of the attribute
  573. :param \callable_:
  574. optional function which generates a callable based on a parent
  575. instance, which produces the "default" values for a scalar or
  576. collection attribute when it's first accessed, if not present
  577. already.
  578. :param trackparent:
  579. if True, attempt to track if an instance has a parent attached
  580. to it via this attribute.
  581. :param compare_function:
  582. a function that compares two values which are normally
  583. assignable to this attribute.
  584. :param active_history:
  585. indicates that get_history() should always return the "old" value,
  586. even if it means executing a lazy callable upon attribute change.
  587. :param parent_token:
  588. Usually references the MapperProperty, used as a key for
  589. the hasparent() function to identify an "owning" attribute.
  590. Allows multiple AttributeImpls to all match a single
  591. owner attribute.
  592. :param load_on_unexpire:
  593. if False, don't include this attribute in a load-on-expired
  594. operation, i.e. the "expired_attribute_loader" process.
  595. The attribute can still be in the "expired" list and be
  596. considered to be "expired". Previously, this flag was called
  597. "expire_missing" and is only used by a deferred column
  598. attribute.
  599. :param send_modified_events:
  600. if False, the InstanceState._modified_event method will have no
  601. effect; this means the attribute will never show up as changed in a
  602. history entry.
  603. """
  604. self.class_ = class_
  605. self.key = key
  606. self.callable_ = callable_
  607. self.dispatch = dispatch
  608. self.trackparent = trackparent
  609. self.parent_token = parent_token or self
  610. self.send_modified_events = send_modified_events
  611. if compare_function is None:
  612. self.is_equal = operator.eq
  613. else:
  614. self.is_equal = compare_function
  615. if accepts_scalar_loader is not None:
  616. self.accepts_scalar_loader = accepts_scalar_loader
  617. else:
  618. self.accepts_scalar_loader = self.default_accepts_scalar_loader
  619. if active_history:
  620. self.dispatch._active_history = True
  621. self.load_on_unexpire = load_on_unexpire
  622. self._modified_token = Event(self, OP_MODIFIED)
  623. __slots__ = (
  624. "class_",
  625. "key",
  626. "callable_",
  627. "dispatch",
  628. "trackparent",
  629. "parent_token",
  630. "send_modified_events",
  631. "is_equal",
  632. "load_on_unexpire",
  633. "_modified_token",
  634. "accepts_scalar_loader",
  635. )
  636. def __str__(self):
  637. return "%s.%s" % (self.class_.__name__, self.key)
  638. def _get_active_history(self):
  639. """Backwards compat for impl.active_history"""
  640. return self.dispatch._active_history
  641. def _set_active_history(self, value):
  642. self.dispatch._active_history = value
  643. active_history = property(_get_active_history, _set_active_history)
  644. def hasparent(self, state, optimistic=False):
  645. """Return the boolean value of a `hasparent` flag attached to
  646. the given state.
  647. The `optimistic` flag determines what the default return value
  648. should be if no `hasparent` flag can be located.
  649. As this function is used to determine if an instance is an
  650. *orphan*, instances that were loaded from storage should be
  651. assumed to not be orphans, until a True/False value for this
  652. flag is set.
  653. An instance attribute that is loaded by a callable function
  654. will also not have a `hasparent` flag.
  655. """
  656. msg = "This AttributeImpl is not configured to track parents."
  657. assert self.trackparent, msg
  658. return (
  659. state.parents.get(id(self.parent_token), optimistic) is not False
  660. )
  661. def sethasparent(self, state, parent_state, value):
  662. """Set a boolean flag on the given item corresponding to
  663. whether or not it is attached to a parent object via the
  664. attribute represented by this ``InstrumentedAttribute``.
  665. """
  666. msg = "This AttributeImpl is not configured to track parents."
  667. assert self.trackparent, msg
  668. id_ = id(self.parent_token)
  669. if value:
  670. state.parents[id_] = parent_state
  671. else:
  672. if id_ in state.parents:
  673. last_parent = state.parents[id_]
  674. if (
  675. last_parent is not False
  676. and last_parent.key != parent_state.key
  677. ):
  678. if last_parent.obj() is None:
  679. raise orm_exc.StaleDataError(
  680. "Removing state %s from parent "
  681. "state %s along attribute '%s', "
  682. "but the parent record "
  683. "has gone stale, can't be sure this "
  684. "is the most recent parent."
  685. % (
  686. state_str(state),
  687. state_str(parent_state),
  688. self.key,
  689. )
  690. )
  691. return
  692. state.parents[id_] = False
  693. def get_history(self, state, dict_, passive=PASSIVE_OFF):
  694. raise NotImplementedError()
  695. def get_all_pending(self, state, dict_, passive=PASSIVE_NO_INITIALIZE):
  696. """Return a list of tuples of (state, obj)
  697. for all objects in this attribute's current state
  698. + history.
  699. Only applies to object-based attributes.
  700. This is an inlining of existing functionality
  701. which roughly corresponds to:
  702. get_state_history(
  703. state,
  704. key,
  705. passive=PASSIVE_NO_INITIALIZE).sum()
  706. """
  707. raise NotImplementedError()
  708. def _default_value(self, state, dict_):
  709. """Produce an empty value for an uninitialized scalar attribute."""
  710. assert self.key not in dict_, (
  711. "_default_value should only be invoked for an "
  712. "uninitialized or expired attribute"
  713. )
  714. value = None
  715. for fn in self.dispatch.init_scalar:
  716. ret = fn(state, value, dict_)
  717. if ret is not ATTR_EMPTY:
  718. value = ret
  719. return value
  720. def get(self, state, dict_, passive=PASSIVE_OFF):
  721. """Retrieve a value from the given object.
  722. If a callable is assembled on this object's attribute, and
  723. passive is False, the callable will be executed and the
  724. resulting value will be set as the new value for this attribute.
  725. """
  726. if self.key in dict_:
  727. return dict_[self.key]
  728. else:
  729. # if history present, don't load
  730. key = self.key
  731. if (
  732. key not in state.committed_state
  733. or state.committed_state[key] is NO_VALUE
  734. ):
  735. if not passive & CALLABLES_OK:
  736. return PASSIVE_NO_RESULT
  737. if (
  738. self.accepts_scalar_loader
  739. and self.load_on_unexpire
  740. and key in state.expired_attributes
  741. ):
  742. value = state._load_expired(state, passive)
  743. elif key in state.callables:
  744. callable_ = state.callables[key]
  745. value = callable_(state, passive)
  746. elif self.callable_:
  747. value = self.callable_(state, passive)
  748. else:
  749. value = ATTR_EMPTY
  750. if value is PASSIVE_NO_RESULT or value is NO_VALUE:
  751. return value
  752. elif value is ATTR_WAS_SET:
  753. try:
  754. return dict_[key]
  755. except KeyError as err:
  756. # TODO: no test coverage here.
  757. util.raise_(
  758. KeyError(
  759. "Deferred loader for attribute "
  760. "%r failed to populate "
  761. "correctly" % key
  762. ),
  763. replace_context=err,
  764. )
  765. elif value is not ATTR_EMPTY:
  766. return self.set_committed_value(state, dict_, value)
  767. if not passive & INIT_OK:
  768. return NO_VALUE
  769. else:
  770. return self._default_value(state, dict_)
  771. def append(self, state, dict_, value, initiator, passive=PASSIVE_OFF):
  772. self.set(state, dict_, value, initiator, passive=passive)
  773. def remove(self, state, dict_, value, initiator, passive=PASSIVE_OFF):
  774. self.set(
  775. state, dict_, None, initiator, passive=passive, check_old=value
  776. )
  777. def pop(self, state, dict_, value, initiator, passive=PASSIVE_OFF):
  778. self.set(
  779. state,
  780. dict_,
  781. None,
  782. initiator,
  783. passive=passive,
  784. check_old=value,
  785. pop=True,
  786. )
  787. def set(
  788. self,
  789. state,
  790. dict_,
  791. value,
  792. initiator,
  793. passive=PASSIVE_OFF,
  794. check_old=None,
  795. pop=False,
  796. ):
  797. raise NotImplementedError()
  798. def get_committed_value(self, state, dict_, passive=PASSIVE_OFF):
  799. """return the unchanged value of this attribute"""
  800. if self.key in state.committed_state:
  801. value = state.committed_state[self.key]
  802. if value is NO_VALUE:
  803. return None
  804. else:
  805. return value
  806. else:
  807. return self.get(state, dict_, passive=passive)
  808. def set_committed_value(self, state, dict_, value):
  809. """set an attribute value on the given instance and 'commit' it."""
  810. dict_[self.key] = value
  811. state._commit(dict_, [self.key])
  812. return value
  813. class ScalarAttributeImpl(AttributeImpl):
  814. """represents a scalar value-holding InstrumentedAttribute."""
  815. default_accepts_scalar_loader = True
  816. uses_objects = False
  817. supports_population = True
  818. collection = False
  819. dynamic = False
  820. __slots__ = "_replace_token", "_append_token", "_remove_token"
  821. def __init__(self, *arg, **kw):
  822. super(ScalarAttributeImpl, self).__init__(*arg, **kw)
  823. self._replace_token = self._append_token = Event(self, OP_REPLACE)
  824. self._remove_token = Event(self, OP_REMOVE)
  825. def delete(self, state, dict_):
  826. if self.dispatch._active_history:
  827. old = self.get(state, dict_, PASSIVE_RETURN_NO_VALUE)
  828. else:
  829. old = dict_.get(self.key, NO_VALUE)
  830. if self.dispatch.remove:
  831. self.fire_remove_event(state, dict_, old, self._remove_token)
  832. state._modified_event(dict_, self, old)
  833. existing = dict_.pop(self.key, NO_VALUE)
  834. if (
  835. existing is NO_VALUE
  836. and old is NO_VALUE
  837. and not state.expired
  838. and self.key not in state.expired_attributes
  839. ):
  840. raise AttributeError("%s object does not have a value" % self)
  841. def get_history(self, state, dict_, passive=PASSIVE_OFF):
  842. if self.key in dict_:
  843. return History.from_scalar_attribute(self, state, dict_[self.key])
  844. elif self.key in state.committed_state:
  845. return History.from_scalar_attribute(self, state, NO_VALUE)
  846. else:
  847. if passive & INIT_OK:
  848. passive ^= INIT_OK
  849. current = self.get(state, dict_, passive=passive)
  850. if current is PASSIVE_NO_RESULT:
  851. return HISTORY_BLANK
  852. else:
  853. return History.from_scalar_attribute(self, state, current)
  854. def set(
  855. self,
  856. state,
  857. dict_,
  858. value,
  859. initiator,
  860. passive=PASSIVE_OFF,
  861. check_old=None,
  862. pop=False,
  863. ):
  864. if self.dispatch._active_history:
  865. old = self.get(state, dict_, PASSIVE_RETURN_NO_VALUE)
  866. else:
  867. old = dict_.get(self.key, NO_VALUE)
  868. if self.dispatch.set:
  869. value = self.fire_replace_event(
  870. state, dict_, value, old, initiator
  871. )
  872. state._modified_event(dict_, self, old)
  873. dict_[self.key] = value
  874. def fire_replace_event(self, state, dict_, value, previous, initiator):
  875. for fn in self.dispatch.set:
  876. value = fn(
  877. state, value, previous, initiator or self._replace_token
  878. )
  879. return value
  880. def fire_remove_event(self, state, dict_, value, initiator):
  881. for fn in self.dispatch.remove:
  882. fn(state, value, initiator or self._remove_token)
  883. @property
  884. def type(self):
  885. self.property.columns[0].type
  886. class ScalarObjectAttributeImpl(ScalarAttributeImpl):
  887. """represents a scalar-holding InstrumentedAttribute,
  888. where the target object is also instrumented.
  889. Adds events to delete/set operations.
  890. """
  891. default_accepts_scalar_loader = False
  892. uses_objects = True
  893. supports_population = True
  894. collection = False
  895. __slots__ = ()
  896. def delete(self, state, dict_):
  897. if self.dispatch._active_history:
  898. old = self.get(
  899. state,
  900. dict_,
  901. passive=PASSIVE_ONLY_PERSISTENT
  902. | NO_AUTOFLUSH
  903. | LOAD_AGAINST_COMMITTED,
  904. )
  905. else:
  906. old = self.get(
  907. state,
  908. dict_,
  909. passive=PASSIVE_NO_FETCH ^ INIT_OK
  910. | LOAD_AGAINST_COMMITTED
  911. | NO_RAISE,
  912. )
  913. self.fire_remove_event(state, dict_, old, self._remove_token)
  914. existing = dict_.pop(self.key, NO_VALUE)
  915. # if the attribute is expired, we currently have no way to tell
  916. # that an object-attribute was expired vs. not loaded. So
  917. # for this test, we look to see if the object has a DB identity.
  918. if (
  919. existing is NO_VALUE
  920. and old is not PASSIVE_NO_RESULT
  921. and state.key is None
  922. ):
  923. raise AttributeError("%s object does not have a value" % self)
  924. def get_history(self, state, dict_, passive=PASSIVE_OFF):
  925. if self.key in dict_:
  926. return History.from_object_attribute(self, state, dict_[self.key])
  927. else:
  928. if passive & INIT_OK:
  929. passive ^= INIT_OK
  930. current = self.get(state, dict_, passive=passive)
  931. if current is PASSIVE_NO_RESULT:
  932. return HISTORY_BLANK
  933. else:
  934. return History.from_object_attribute(self, state, current)
  935. def get_all_pending(self, state, dict_, passive=PASSIVE_NO_INITIALIZE):
  936. if self.key in dict_:
  937. current = dict_[self.key]
  938. elif passive & CALLABLES_OK:
  939. current = self.get(state, dict_, passive=passive)
  940. else:
  941. return []
  942. # can't use __hash__(), can't use __eq__() here
  943. if (
  944. current is not None
  945. and current is not PASSIVE_NO_RESULT
  946. and current is not NO_VALUE
  947. ):
  948. ret = [(instance_state(current), current)]
  949. else:
  950. ret = [(None, None)]
  951. if self.key in state.committed_state:
  952. original = state.committed_state[self.key]
  953. if (
  954. original is not None
  955. and original is not PASSIVE_NO_RESULT
  956. and original is not NO_VALUE
  957. and original is not current
  958. ):
  959. ret.append((instance_state(original), original))
  960. return ret
  961. def set(
  962. self,
  963. state,
  964. dict_,
  965. value,
  966. initiator,
  967. passive=PASSIVE_OFF,
  968. check_old=None,
  969. pop=False,
  970. ):
  971. """Set a value on the given InstanceState."""
  972. if self.dispatch._active_history:
  973. old = self.get(
  974. state,
  975. dict_,
  976. passive=PASSIVE_ONLY_PERSISTENT
  977. | NO_AUTOFLUSH
  978. | LOAD_AGAINST_COMMITTED,
  979. )
  980. else:
  981. old = self.get(
  982. state,
  983. dict_,
  984. passive=PASSIVE_NO_FETCH ^ INIT_OK
  985. | LOAD_AGAINST_COMMITTED
  986. | NO_RAISE,
  987. )
  988. if (
  989. check_old is not None
  990. and old is not PASSIVE_NO_RESULT
  991. and check_old is not old
  992. ):
  993. if pop:
  994. return
  995. else:
  996. raise ValueError(
  997. "Object %s not associated with %s on attribute '%s'"
  998. % (instance_str(check_old), state_str(state), self.key)
  999. )
  1000. value = self.fire_replace_event(state, dict_, value, old, initiator)
  1001. dict_[self.key] = value
  1002. def fire_remove_event(self, state, dict_, value, initiator):
  1003. if self.trackparent and value is not None:
  1004. self.sethasparent(instance_state(value), state, False)
  1005. for fn in self.dispatch.remove:
  1006. fn(state, value, initiator or self._remove_token)
  1007. state._modified_event(dict_, self, value)
  1008. def fire_replace_event(self, state, dict_, value, previous, initiator):
  1009. if self.trackparent:
  1010. if previous is not value and previous not in (
  1011. None,
  1012. PASSIVE_NO_RESULT,
  1013. NO_VALUE,
  1014. ):
  1015. self.sethasparent(instance_state(previous), state, False)
  1016. for fn in self.dispatch.set:
  1017. value = fn(
  1018. state, value, previous, initiator or self._replace_token
  1019. )
  1020. state._modified_event(dict_, self, previous)
  1021. if self.trackparent:
  1022. if value is not None:
  1023. self.sethasparent(instance_state(value), state, True)
  1024. return value
  1025. class CollectionAttributeImpl(AttributeImpl):
  1026. """A collection-holding attribute that instruments changes in membership.
  1027. Only handles collections of instrumented objects.
  1028. InstrumentedCollectionAttribute holds an arbitrary, user-specified
  1029. container object (defaulting to a list) and brokers access to the
  1030. CollectionAdapter, a "view" onto that object that presents consistent bag
  1031. semantics to the orm layer independent of the user data implementation.
  1032. """
  1033. default_accepts_scalar_loader = False
  1034. uses_objects = True
  1035. supports_population = True
  1036. collection = True
  1037. dynamic = False
  1038. __slots__ = (
  1039. "copy",
  1040. "collection_factory",
  1041. "_append_token",
  1042. "_remove_token",
  1043. "_bulk_replace_token",
  1044. "_duck_typed_as",
  1045. )
  1046. def __init__(
  1047. self,
  1048. class_,
  1049. key,
  1050. callable_,
  1051. dispatch,
  1052. typecallable=None,
  1053. trackparent=False,
  1054. copy_function=None,
  1055. compare_function=None,
  1056. **kwargs
  1057. ):
  1058. super(CollectionAttributeImpl, self).__init__(
  1059. class_,
  1060. key,
  1061. callable_,
  1062. dispatch,
  1063. trackparent=trackparent,
  1064. compare_function=compare_function,
  1065. **kwargs
  1066. )
  1067. if copy_function is None:
  1068. copy_function = self.__copy
  1069. self.copy = copy_function
  1070. self.collection_factory = typecallable
  1071. self._append_token = Event(self, OP_APPEND)
  1072. self._remove_token = Event(self, OP_REMOVE)
  1073. self._bulk_replace_token = Event(self, OP_BULK_REPLACE)
  1074. self._duck_typed_as = util.duck_type_collection(
  1075. self.collection_factory()
  1076. )
  1077. if getattr(self.collection_factory, "_sa_linker", None):
  1078. @event.listens_for(self, "init_collection")
  1079. def link(target, collection, collection_adapter):
  1080. collection._sa_linker(collection_adapter)
  1081. @event.listens_for(self, "dispose_collection")
  1082. def unlink(target, collection, collection_adapter):
  1083. collection._sa_linker(None)
  1084. def __copy(self, item):
  1085. return [y for y in collections.collection_adapter(item)]
  1086. def get_history(self, state, dict_, passive=PASSIVE_OFF):
  1087. current = self.get(state, dict_, passive=passive)
  1088. if current is PASSIVE_NO_RESULT:
  1089. return HISTORY_BLANK
  1090. else:
  1091. return History.from_collection(self, state, current)
  1092. def get_all_pending(self, state, dict_, passive=PASSIVE_NO_INITIALIZE):
  1093. # NOTE: passive is ignored here at the moment
  1094. if self.key not in dict_:
  1095. return []
  1096. current = dict_[self.key]
  1097. current = getattr(current, "_sa_adapter")
  1098. if self.key in state.committed_state:
  1099. original = state.committed_state[self.key]
  1100. if original is not NO_VALUE:
  1101. current_states = [
  1102. ((c is not None) and instance_state(c) or None, c)
  1103. for c in current
  1104. ]
  1105. original_states = [
  1106. ((c is not None) and instance_state(c) or None, c)
  1107. for c in original
  1108. ]
  1109. current_set = dict(current_states)
  1110. original_set = dict(original_states)
  1111. return (
  1112. [
  1113. (s, o)
  1114. for s, o in current_states
  1115. if s not in original_set
  1116. ]
  1117. + [(s, o) for s, o in current_states if s in original_set]
  1118. + [
  1119. (s, o)
  1120. for s, o in original_states
  1121. if s not in current_set
  1122. ]
  1123. )
  1124. return [(instance_state(o), o) for o in current]
  1125. def fire_append_event(self, state, dict_, value, initiator):
  1126. for fn in self.dispatch.append:
  1127. value = fn(state, value, initiator or self._append_token)
  1128. state._modified_event(dict_, self, NO_VALUE, True)
  1129. if self.trackparent and value is not None:
  1130. self.sethasparent(instance_state(value), state, True)
  1131. return value
  1132. def fire_append_wo_mutation_event(self, state, dict_, value, initiator):
  1133. for fn in self.dispatch.append_wo_mutation:
  1134. value = fn(state, value, initiator or self._append_token)
  1135. return value
  1136. def fire_pre_remove_event(self, state, dict_, initiator):
  1137. """A special event used for pop() operations.
  1138. The "remove" event needs to have the item to be removed passed to
  1139. it, which in the case of pop from a set, we don't have a way to access
  1140. the item before the operation. the event is used for all pop()
  1141. operations (even though set.pop is the one where it is really needed).
  1142. """
  1143. state._modified_event(dict_, self, NO_VALUE, True)
  1144. def fire_remove_event(self, state, dict_, value, initiator):
  1145. if self.trackparent and value is not None:
  1146. self.sethasparent(instance_state(value), state, False)
  1147. for fn in self.dispatch.remove:
  1148. fn(state, value, initiator or self._remove_token)
  1149. state._modified_event(dict_, self, NO_VALUE, True)
  1150. def delete(self, state, dict_):
  1151. if self.key not in dict_:
  1152. return
  1153. state._modified_event(dict_, self, NO_VALUE, True)
  1154. collection = self.get_collection(state, state.dict)
  1155. collection.clear_with_event()
  1156. # key is always present because we checked above. e.g.
  1157. # del is a no-op if collection not present.
  1158. del dict_[self.key]
  1159. def _default_value(self, state, dict_):
  1160. """Produce an empty collection for an un-initialized attribute"""
  1161. assert self.key not in dict_, (
  1162. "_default_value should only be invoked for an "
  1163. "uninitialized or expired attribute"
  1164. )
  1165. if self.key in state._empty_collections:
  1166. return state._empty_collections[self.key]
  1167. adapter, user_data = self._initialize_collection(state)
  1168. adapter._set_empty(user_data)
  1169. return user_data
  1170. def _initialize_collection(self, state):
  1171. adapter, collection = state.manager.initialize_collection(
  1172. self.key, state, self.collection_factory
  1173. )
  1174. self.dispatch.init_collection(state, collection, adapter)
  1175. return adapter, collection
  1176. def append(self, state, dict_, value, initiator, passive=PASSIVE_OFF):
  1177. collection = self.get_collection(state, dict_, passive=passive)
  1178. if collection is PASSIVE_NO_RESULT:
  1179. value = self.fire_append_event(state, dict_, value, initiator)
  1180. assert (
  1181. self.key not in dict_
  1182. ), "Collection was loaded during event handling."
  1183. state._get_pending_mutation(self.key).append(value)
  1184. else:
  1185. collection.append_with_event(value, initiator)
  1186. def remove(self, state, dict_, value, initiator, passive=PASSIVE_OFF):
  1187. collection = self.get_collection(state, state.dict, passive=passive)
  1188. if collection is PASSIVE_NO_RESULT:
  1189. self.fire_remove_event(state, dict_, value, initiator)
  1190. assert (
  1191. self.key not in dict_
  1192. ), "Collection was loaded during event handling."
  1193. state._get_pending_mutation(self.key).remove(value)
  1194. else:
  1195. collection.remove_with_event(value, initiator)
  1196. def pop(self, state, dict_, value, initiator, passive=PASSIVE_OFF):
  1197. try:
  1198. # TODO: better solution here would be to add
  1199. # a "popper" role to collections.py to complement
  1200. # "remover".
  1201. self.remove(state, dict_, value, initiator, passive=passive)
  1202. except (ValueError, KeyError, IndexError):
  1203. pass
  1204. def set(
  1205. self,
  1206. state,
  1207. dict_,
  1208. value,
  1209. initiator=None,
  1210. passive=PASSIVE_OFF,
  1211. check_old=None,
  1212. pop=False,
  1213. _adapt=True,
  1214. ):
  1215. iterable = orig_iterable = value
  1216. # pulling a new collection first so that an adaptation exception does
  1217. # not trigger a lazy load of the old collection.
  1218. new_collection, user_data = self._initialize_collection(state)
  1219. if _adapt:
  1220. if new_collection._converter is not None:
  1221. iterable = new_collection._converter(iterable)
  1222. else:
  1223. setting_type = util.duck_type_collection(iterable)
  1224. receiving_type = self._duck_typed_as
  1225. if setting_type is not receiving_type:
  1226. given = (
  1227. iterable is None
  1228. and "None"
  1229. or iterable.__class__.__name__
  1230. )
  1231. wanted = self._duck_typed_as.__name__
  1232. raise TypeError(
  1233. "Incompatible collection type: %s is not %s-like"
  1234. % (given, wanted)
  1235. )
  1236. # If the object is an adapted collection, return the (iterable)
  1237. # adapter.
  1238. if hasattr(iterable, "_sa_iterator"):
  1239. iterable = iterable._sa_iterator()
  1240. elif setting_type is dict:
  1241. if util.py3k:
  1242. iterable = iterable.values()
  1243. else:
  1244. iterable = getattr(
  1245. iterable, "itervalues", iterable.values
  1246. )()
  1247. else:
  1248. iterable = iter(iterable)
  1249. new_values = list(iterable)
  1250. evt = self._bulk_replace_token
  1251. self.dispatch.bulk_replace(state, new_values, evt)
  1252. old = self.get(state, dict_, passive=PASSIVE_ONLY_PERSISTENT)
  1253. if old is PASSIVE_NO_RESULT:
  1254. old = self._default_value(state, dict_)
  1255. elif old is orig_iterable:
  1256. # ignore re-assignment of the current collection, as happens
  1257. # implicitly with in-place operators (foo.collection |= other)
  1258. return
  1259. # place a copy of "old" in state.committed_state
  1260. state._modified_event(dict_, self, old, True)
  1261. old_collection = old._sa_adapter
  1262. dict_[self.key] = user_data
  1263. collections.bulk_replace(
  1264. new_values, old_collection, new_collection, initiator=evt
  1265. )
  1266. self._dispose_previous_collection(state, old, old_collection, True)
  1267. def _dispose_previous_collection(
  1268. self, state, collection, adapter, fire_event
  1269. ):
  1270. del collection._sa_adapter
  1271. # discarding old collection make sure it is not referenced in empty
  1272. # collections.
  1273. state._empty_collections.pop(self.key, None)
  1274. if fire_event:
  1275. self.dispatch.dispose_collection(state, collection, adapter)
  1276. def _invalidate_collection(self, collection):
  1277. adapter = getattr(collection, "_sa_adapter")
  1278. adapter.invalidated = True
  1279. def set_committed_value(self, state, dict_, value):
  1280. """Set an attribute value on the given instance and 'commit' it."""
  1281. collection, user_data = self._initialize_collection(state)
  1282. if value:
  1283. collection.append_multiple_without_event(value)
  1284. state.dict[self.key] = user_data
  1285. state._commit(dict_, [self.key])
  1286. if self.key in state._pending_mutations:
  1287. # pending items exist. issue a modified event,
  1288. # add/remove new items.
  1289. state._modified_event(dict_, self, user_data, True)
  1290. pending = state._pending_mutations.pop(self.key)
  1291. added = pending.added_items
  1292. removed = pending.deleted_items
  1293. for item in added:
  1294. collection.append_without_event(item)
  1295. for item in removed:
  1296. collection.remove_without_event(item)
  1297. return user_data
  1298. def get_collection(
  1299. self, state, dict_, user_data=None, passive=PASSIVE_OFF
  1300. ):
  1301. """Retrieve the CollectionAdapter associated with the given state.
  1302. if user_data is None, retrieves it from the state using normal
  1303. "get()" rules, which will fire lazy callables or return the "empty"
  1304. collection value.
  1305. """
  1306. if user_data is None:
  1307. user_data = self.get(state, dict_, passive=passive)
  1308. if user_data is PASSIVE_NO_RESULT:
  1309. return user_data
  1310. return user_data._sa_adapter
  1311. def backref_listeners(attribute, key, uselist):
  1312. """Apply listeners to synchronize a two-way relationship."""
  1313. # use easily recognizable names for stack traces.
  1314. # in the sections marked "tokens to test for a recursive loop",
  1315. # this is somewhat brittle and very performance-sensitive logic
  1316. # that is specific to how we might arrive at each event. a marker
  1317. # that can target us directly to arguments being invoked against
  1318. # the impl might be simpler, but could interfere with other systems.
  1319. parent_token = attribute.impl.parent_token
  1320. parent_impl = attribute.impl
  1321. def _acceptable_key_err(child_state, initiator, child_impl):
  1322. raise ValueError(
  1323. "Bidirectional attribute conflict detected: "
  1324. 'Passing object %s to attribute "%s" '
  1325. 'triggers a modify event on attribute "%s" '
  1326. 'via the backref "%s".'
  1327. % (
  1328. state_str(child_state),
  1329. initiator.parent_token,
  1330. child_impl.parent_token,
  1331. attribute.impl.parent_token,
  1332. )
  1333. )
  1334. def emit_backref_from_scalar_set_event(state, child, oldchild, initiator):
  1335. if oldchild is child:
  1336. return child
  1337. if (
  1338. oldchild is not None
  1339. and oldchild is not PASSIVE_NO_RESULT
  1340. and oldchild is not NO_VALUE
  1341. ):
  1342. # With lazy=None, there's no guarantee that the full collection is
  1343. # present when updating via a backref.
  1344. old_state, old_dict = (
  1345. instance_state(oldchild),
  1346. instance_dict(oldchild),
  1347. )
  1348. impl = old_state.manager[key].impl
  1349. # tokens to test for a recursive loop.
  1350. if not impl.collection and not impl.dynamic:
  1351. check_recursive_token = impl._replace_token
  1352. else:
  1353. check_recursive_token = impl._remove_token
  1354. if initiator is not check_recursive_token:
  1355. impl.pop(
  1356. old_state,
  1357. old_dict,
  1358. state.obj(),
  1359. parent_impl._append_token,
  1360. passive=PASSIVE_NO_FETCH,
  1361. )
  1362. if child is not None:
  1363. child_state, child_dict = (
  1364. instance_state(child),
  1365. instance_dict(child),
  1366. )
  1367. child_impl = child_state.manager[key].impl
  1368. if (
  1369. initiator.parent_token is not parent_token
  1370. and initiator.parent_token is not child_impl.parent_token
  1371. ):
  1372. _acceptable_key_err(state, initiator, child_impl)
  1373. # tokens to test for a recursive loop.
  1374. check_append_token = child_impl._append_token
  1375. check_bulk_replace_token = (
  1376. child_impl._bulk_replace_token
  1377. if child_impl.collection
  1378. else None
  1379. )
  1380. if (
  1381. initiator is not check_append_token
  1382. and initiator is not check_bulk_replace_token
  1383. ):
  1384. child_impl.append(
  1385. child_state,
  1386. child_dict,
  1387. state.obj(),
  1388. initiator,
  1389. passive=PASSIVE_NO_FETCH,
  1390. )
  1391. return child
  1392. def emit_backref_from_collection_append_event(state, child, initiator):
  1393. if child is None:
  1394. return
  1395. child_state, child_dict = instance_state(child), instance_dict(child)
  1396. child_impl = child_state.manager[key].impl
  1397. if (
  1398. initiator.parent_token is not parent_token
  1399. and initiator.parent_token is not child_impl.parent_token
  1400. ):
  1401. _acceptable_key_err(state, initiator, child_impl)
  1402. # tokens to test for a recursive loop.
  1403. check_append_token = child_impl._append_token
  1404. check_bulk_replace_token = (
  1405. child_impl._bulk_replace_token if child_impl.collection else None
  1406. )
  1407. if (
  1408. initiator is not check_append_token
  1409. and initiator is not check_bulk_replace_token
  1410. ):
  1411. child_impl.append(
  1412. child_state,
  1413. child_dict,
  1414. state.obj(),
  1415. initiator,
  1416. passive=PASSIVE_NO_FETCH,
  1417. )
  1418. return child
  1419. def emit_backref_from_collection_remove_event(state, child, initiator):
  1420. if (
  1421. child is not None
  1422. and child is not PASSIVE_NO_RESULT
  1423. and child is not NO_VALUE
  1424. ):
  1425. child_state, child_dict = (
  1426. instance_state(child),
  1427. instance_dict(child),
  1428. )
  1429. child_impl = child_state.manager[key].impl
  1430. # tokens to test for a recursive loop.
  1431. if not child_impl.collection and not child_impl.dynamic:
  1432. check_remove_token = child_impl._remove_token
  1433. check_replace_token = child_impl._replace_token
  1434. check_for_dupes_on_remove = uselist and not parent_impl.dynamic
  1435. else:
  1436. check_remove_token = child_impl._remove_token
  1437. check_replace_token = (
  1438. child_impl._bulk_replace_token
  1439. if child_impl.collection
  1440. else None
  1441. )
  1442. check_for_dupes_on_remove = False
  1443. if (
  1444. initiator is not check_remove_token
  1445. and initiator is not check_replace_token
  1446. ):
  1447. if not check_for_dupes_on_remove or not util.has_dupes(
  1448. # when this event is called, the item is usually
  1449. # present in the list, except for a pop() operation.
  1450. state.dict[parent_impl.key],
  1451. child,
  1452. ):
  1453. child_impl.pop(
  1454. child_state,
  1455. child_dict,
  1456. state.obj(),
  1457. initiator,
  1458. passive=PASSIVE_NO_FETCH,
  1459. )
  1460. if uselist:
  1461. event.listen(
  1462. attribute,
  1463. "append",
  1464. emit_backref_from_collection_append_event,
  1465. retval=True,
  1466. raw=True,
  1467. )
  1468. else:
  1469. event.listen(
  1470. attribute,
  1471. "set",
  1472. emit_backref_from_scalar_set_event,
  1473. retval=True,
  1474. raw=True,
  1475. )
  1476. # TODO: need coverage in test/orm/ of remove event
  1477. event.listen(
  1478. attribute,
  1479. "remove",
  1480. emit_backref_from_collection_remove_event,
  1481. retval=True,
  1482. raw=True,
  1483. )
  1484. _NO_HISTORY = util.symbol("NO_HISTORY")
  1485. _NO_STATE_SYMBOLS = frozenset([id(PASSIVE_NO_RESULT), id(NO_VALUE)])
  1486. class History(util.namedtuple("History", ["added", "unchanged", "deleted"])):
  1487. """A 3-tuple of added, unchanged and deleted values,
  1488. representing the changes which have occurred on an instrumented
  1489. attribute.
  1490. The easiest way to get a :class:`.History` object for a particular
  1491. attribute on an object is to use the :func:`_sa.inspect` function::
  1492. from sqlalchemy import inspect
  1493. hist = inspect(myobject).attrs.myattribute.history
  1494. Each tuple member is an iterable sequence:
  1495. * ``added`` - the collection of items added to the attribute (the first
  1496. tuple element).
  1497. * ``unchanged`` - the collection of items that have not changed on the
  1498. attribute (the second tuple element).
  1499. * ``deleted`` - the collection of items that have been removed from the
  1500. attribute (the third tuple element).
  1501. """
  1502. def __bool__(self):
  1503. return self != HISTORY_BLANK
  1504. __nonzero__ = __bool__
  1505. def empty(self):
  1506. """Return True if this :class:`.History` has no changes
  1507. and no existing, unchanged state.
  1508. """
  1509. return not bool((self.added or self.deleted) or self.unchanged)
  1510. def sum(self):
  1511. """Return a collection of added + unchanged + deleted."""
  1512. return (
  1513. (self.added or []) + (self.unchanged or []) + (self.deleted or [])
  1514. )
  1515. def non_deleted(self):
  1516. """Return a collection of added + unchanged."""
  1517. return (self.added or []) + (self.unchanged or [])
  1518. def non_added(self):
  1519. """Return a collection of unchanged + deleted."""
  1520. return (self.unchanged or []) + (self.deleted or [])
  1521. def has_changes(self):
  1522. """Return True if this :class:`.History` has changes."""
  1523. return bool(self.added or self.deleted)
  1524. def as_state(self):
  1525. return History(
  1526. [
  1527. (c is not None) and instance_state(c) or None
  1528. for c in self.added
  1529. ],
  1530. [
  1531. (c is not None) and instance_state(c) or None
  1532. for c in self.unchanged
  1533. ],
  1534. [
  1535. (c is not None) and instance_state(c) or None
  1536. for c in self.deleted
  1537. ],
  1538. )
  1539. @classmethod
  1540. def from_scalar_attribute(cls, attribute, state, current):
  1541. original = state.committed_state.get(attribute.key, _NO_HISTORY)
  1542. if original is _NO_HISTORY:
  1543. if current is NO_VALUE:
  1544. return cls((), (), ())
  1545. else:
  1546. return cls((), [current], ())
  1547. # don't let ClauseElement expressions here trip things up
  1548. elif (
  1549. current is not NO_VALUE
  1550. and attribute.is_equal(current, original) is True
  1551. ):
  1552. return cls((), [current], ())
  1553. else:
  1554. # current convention on native scalars is to not
  1555. # include information
  1556. # about missing previous value in "deleted", but
  1557. # we do include None, which helps in some primary
  1558. # key situations
  1559. if id(original) in _NO_STATE_SYMBOLS:
  1560. deleted = ()
  1561. # indicate a "del" operation occurred when we don't have
  1562. # the previous value as: ([None], (), ())
  1563. if id(current) in _NO_STATE_SYMBOLS:
  1564. current = None
  1565. else:
  1566. deleted = [original]
  1567. if current is NO_VALUE:
  1568. return cls((), (), deleted)
  1569. else:
  1570. return cls([current], (), deleted)
  1571. @classmethod
  1572. def from_object_attribute(cls, attribute, state, current):
  1573. original = state.committed_state.get(attribute.key, _NO_HISTORY)
  1574. if original is _NO_HISTORY:
  1575. if current is NO_VALUE:
  1576. return cls((), (), ())
  1577. else:
  1578. return cls((), [current], ())
  1579. elif current is original and current is not NO_VALUE:
  1580. return cls((), [current], ())
  1581. else:
  1582. # current convention on related objects is to not
  1583. # include information
  1584. # about missing previous value in "deleted", and
  1585. # to also not include None - the dependency.py rules
  1586. # ignore the None in any case.
  1587. if id(original) in _NO_STATE_SYMBOLS or original is None:
  1588. deleted = ()
  1589. # indicate a "del" operation occurred when we don't have
  1590. # the previous value as: ([None], (), ())
  1591. if id(current) in _NO_STATE_SYMBOLS:
  1592. current = None
  1593. else:
  1594. deleted = [original]
  1595. if current is NO_VALUE:
  1596. return cls((), (), deleted)
  1597. else:
  1598. return cls([current], (), deleted)
  1599. @classmethod
  1600. def from_collection(cls, attribute, state, current):
  1601. original = state.committed_state.get(attribute.key, _NO_HISTORY)
  1602. if current is NO_VALUE:
  1603. return cls((), (), ())
  1604. current = getattr(current, "_sa_adapter")
  1605. if original is NO_VALUE:
  1606. return cls(list(current), (), ())
  1607. elif original is _NO_HISTORY:
  1608. return cls((), list(current), ())
  1609. else:
  1610. current_states = [
  1611. ((c is not None) and instance_state(c) or None, c)
  1612. for c in current
  1613. ]
  1614. original_states = [
  1615. ((c is not None) and instance_state(c) or None, c)
  1616. for c in original
  1617. ]
  1618. current_set = dict(current_states)
  1619. original_set = dict(original_states)
  1620. return cls(
  1621. [o for s, o in current_states if s not in original_set],
  1622. [o for s, o in current_states if s in original_set],
  1623. [o for s, o in original_states if s not in current_set],
  1624. )
  1625. HISTORY_BLANK = History(None, None, None)
  1626. def get_history(obj, key, passive=PASSIVE_OFF):
  1627. """Return a :class:`.History` record for the given object
  1628. and attribute key.
  1629. This is the **pre-flush** history for a given attribute, which is
  1630. reset each time the :class:`.Session` flushes changes to the
  1631. current database transaction.
  1632. .. note::
  1633. Prefer to use the :attr:`.AttributeState.history` and
  1634. :meth:`.AttributeState.load_history` accessors to retrieve the
  1635. :class:`.History` for instance attributes.
  1636. :param obj: an object whose class is instrumented by the
  1637. attributes package.
  1638. :param key: string attribute name.
  1639. :param passive: indicates loading behavior for the attribute
  1640. if the value is not already present. This is a
  1641. bitflag attribute, which defaults to the symbol
  1642. :attr:`.PASSIVE_OFF` indicating all necessary SQL
  1643. should be emitted.
  1644. .. seealso::
  1645. :attr:`.AttributeState.history`
  1646. :meth:`.AttributeState.load_history` - retrieve history
  1647. using loader callables if the value is not locally present.
  1648. """
  1649. return get_state_history(instance_state(obj), key, passive)
  1650. def get_state_history(state, key, passive=PASSIVE_OFF):
  1651. return state.get_history(key, passive)
  1652. def has_parent(cls, obj, key, optimistic=False):
  1653. """TODO"""
  1654. manager = manager_of_class(cls)
  1655. state = instance_state(obj)
  1656. return manager.has_parent(state, key, optimistic)
  1657. def register_attribute(class_, key, **kw):
  1658. comparator = kw.pop("comparator", None)
  1659. parententity = kw.pop("parententity", None)
  1660. doc = kw.pop("doc", None)
  1661. desc = register_descriptor(class_, key, comparator, parententity, doc=doc)
  1662. register_attribute_impl(class_, key, **kw)
  1663. return desc
  1664. def register_attribute_impl(
  1665. class_,
  1666. key,
  1667. uselist=False,
  1668. callable_=None,
  1669. useobject=False,
  1670. impl_class=None,
  1671. backref=None,
  1672. **kw
  1673. ):
  1674. manager = manager_of_class(class_)
  1675. if uselist:
  1676. factory = kw.pop("typecallable", None)
  1677. typecallable = manager.instrument_collection_class(
  1678. key, factory or list
  1679. )
  1680. else:
  1681. typecallable = kw.pop("typecallable", None)
  1682. dispatch = manager[key].dispatch
  1683. if impl_class:
  1684. impl = impl_class(class_, key, typecallable, dispatch, **kw)
  1685. elif uselist:
  1686. impl = CollectionAttributeImpl(
  1687. class_, key, callable_, dispatch, typecallable=typecallable, **kw
  1688. )
  1689. elif useobject:
  1690. impl = ScalarObjectAttributeImpl(
  1691. class_, key, callable_, dispatch, **kw
  1692. )
  1693. else:
  1694. impl = ScalarAttributeImpl(class_, key, callable_, dispatch, **kw)
  1695. manager[key].impl = impl
  1696. if backref:
  1697. backref_listeners(manager[key], backref, uselist)
  1698. manager.post_configure_attribute(key)
  1699. return manager[key]
  1700. def register_descriptor(
  1701. class_, key, comparator=None, parententity=None, doc=None
  1702. ):
  1703. manager = manager_of_class(class_)
  1704. descriptor = InstrumentedAttribute(
  1705. class_, key, comparator=comparator, parententity=parententity
  1706. )
  1707. descriptor.__doc__ = doc
  1708. manager.instrument_attribute(key, descriptor)
  1709. return descriptor
  1710. def unregister_attribute(class_, key):
  1711. manager_of_class(class_).uninstrument_attribute(key)
  1712. def init_collection(obj, key):
  1713. """Initialize a collection attribute and return the collection adapter.
  1714. This function is used to provide direct access to collection internals
  1715. for a previously unloaded attribute. e.g.::
  1716. collection_adapter = init_collection(someobject, 'elements')
  1717. for elem in values:
  1718. collection_adapter.append_without_event(elem)
  1719. For an easier way to do the above, see
  1720. :func:`~sqlalchemy.orm.attributes.set_committed_value`.
  1721. :param obj: a mapped object
  1722. :param key: string attribute name where the collection is located.
  1723. """
  1724. state = instance_state(obj)
  1725. dict_ = state.dict
  1726. return init_state_collection(state, dict_, key)
  1727. def init_state_collection(state, dict_, key):
  1728. """Initialize a collection attribute and return the collection adapter.
  1729. Discards any existing collection which may be there.
  1730. """
  1731. attr = state.manager[key].impl
  1732. old = dict_.pop(key, None) # discard old collection
  1733. if old is not None:
  1734. old_collection = old._sa_adapter
  1735. attr._dispose_previous_collection(state, old, old_collection, False)
  1736. user_data = attr._default_value(state, dict_)
  1737. adapter = attr.get_collection(state, dict_, user_data)
  1738. adapter._reset_empty()
  1739. return adapter
  1740. def set_committed_value(instance, key, value):
  1741. """Set the value of an attribute with no history events.
  1742. Cancels any previous history present. The value should be
  1743. a scalar value for scalar-holding attributes, or
  1744. an iterable for any collection-holding attribute.
  1745. This is the same underlying method used when a lazy loader
  1746. fires off and loads additional data from the database.
  1747. In particular, this method can be used by application code
  1748. which has loaded additional attributes or collections through
  1749. separate queries, which can then be attached to an instance
  1750. as though it were part of its original loaded state.
  1751. """
  1752. state, dict_ = instance_state(instance), instance_dict(instance)
  1753. state.manager[key].impl.set_committed_value(state, dict_, value)
  1754. def set_attribute(instance, key, value, initiator=None):
  1755. """Set the value of an attribute, firing history events.
  1756. This function may be used regardless of instrumentation
  1757. applied directly to the class, i.e. no descriptors are required.
  1758. Custom attribute management schemes will need to make usage
  1759. of this method to establish attribute state as understood
  1760. by SQLAlchemy.
  1761. :param instance: the object that will be modified
  1762. :param key: string name of the attribute
  1763. :param value: value to assign
  1764. :param initiator: an instance of :class:`.Event` that would have
  1765. been propagated from a previous event listener. This argument
  1766. is used when the :func:`.set_attribute` function is being used within
  1767. an existing event listening function where an :class:`.Event` object
  1768. is being supplied; the object may be used to track the origin of the
  1769. chain of events.
  1770. .. versionadded:: 1.2.3
  1771. """
  1772. state, dict_ = instance_state(instance), instance_dict(instance)
  1773. state.manager[key].impl.set(state, dict_, value, initiator)
  1774. def get_attribute(instance, key):
  1775. """Get the value of an attribute, firing any callables required.
  1776. This function may be used regardless of instrumentation
  1777. applied directly to the class, i.e. no descriptors are required.
  1778. Custom attribute management schemes will need to make usage
  1779. of this method to make usage of attribute state as understood
  1780. by SQLAlchemy.
  1781. """
  1782. state, dict_ = instance_state(instance), instance_dict(instance)
  1783. return state.manager[key].impl.get(state, dict_)
  1784. def del_attribute(instance, key):
  1785. """Delete the value of an attribute, firing history events.
  1786. This function may be used regardless of instrumentation
  1787. applied directly to the class, i.e. no descriptors are required.
  1788. Custom attribute management schemes will need to make usage
  1789. of this method to establish attribute state as understood
  1790. by SQLAlchemy.
  1791. """
  1792. state, dict_ = instance_state(instance), instance_dict(instance)
  1793. state.manager[key].impl.delete(state, dict_)
  1794. def flag_modified(instance, key):
  1795. """Mark an attribute on an instance as 'modified'.
  1796. This sets the 'modified' flag on the instance and
  1797. establishes an unconditional change event for the given attribute.
  1798. The attribute must have a value present, else an
  1799. :class:`.InvalidRequestError` is raised.
  1800. To mark an object "dirty" without referring to any specific attribute
  1801. so that it is considered within a flush, use the
  1802. :func:`.attributes.flag_dirty` call.
  1803. .. seealso::
  1804. :func:`.attributes.flag_dirty`
  1805. """
  1806. state, dict_ = instance_state(instance), instance_dict(instance)
  1807. impl = state.manager[key].impl
  1808. impl.dispatch.modified(state, impl._modified_token)
  1809. state._modified_event(dict_, impl, NO_VALUE, is_userland=True)
  1810. def flag_dirty(instance):
  1811. """Mark an instance as 'dirty' without any specific attribute mentioned.
  1812. This is a special operation that will allow the object to travel through
  1813. the flush process for interception by events such as
  1814. :meth:`.SessionEvents.before_flush`. Note that no SQL will be emitted in
  1815. the flush process for an object that has no changes, even if marked dirty
  1816. via this method. However, a :meth:`.SessionEvents.before_flush` handler
  1817. will be able to see the object in the :attr:`.Session.dirty` collection and
  1818. may establish changes on it, which will then be included in the SQL
  1819. emitted.
  1820. .. versionadded:: 1.2
  1821. .. seealso::
  1822. :func:`.attributes.flag_modified`
  1823. """
  1824. state, dict_ = instance_state(instance), instance_dict(instance)
  1825. state._modified_event(dict_, None, NO_VALUE, is_userland=True)