attributes.py 78 KB

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