attributes.py 76 KB

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