events.py 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269
  1. # orm/events.py
  2. # Copyright (C) 2005-2025 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. """ORM event interfaces."""
  8. from __future__ import annotations
  9. from typing import Any
  10. from typing import Callable
  11. from typing import Collection
  12. from typing import Dict
  13. from typing import Generic
  14. from typing import Iterable
  15. from typing import Optional
  16. from typing import Sequence
  17. from typing import Set
  18. from typing import Type
  19. from typing import TYPE_CHECKING
  20. from typing import TypeVar
  21. from typing import Union
  22. import weakref
  23. from . import instrumentation
  24. from . import interfaces
  25. from . import mapperlib
  26. from .attributes import QueryableAttribute
  27. from .base import _mapper_or_none
  28. from .base import NO_KEY
  29. from .instrumentation import ClassManager
  30. from .instrumentation import InstrumentationFactory
  31. from .query import BulkDelete
  32. from .query import BulkUpdate
  33. from .query import Query
  34. from .scoping import scoped_session
  35. from .session import Session
  36. from .session import sessionmaker
  37. from .. import event
  38. from .. import exc
  39. from .. import util
  40. from ..event import EventTarget
  41. from ..event.registry import _ET
  42. from ..util.compat import inspect_getfullargspec
  43. if TYPE_CHECKING:
  44. from weakref import ReferenceType
  45. from ._typing import _InstanceDict
  46. from ._typing import _InternalEntityType
  47. from ._typing import _O
  48. from ._typing import _T
  49. from .attributes import Event
  50. from .base import EventConstants
  51. from .session import ORMExecuteState
  52. from .session import SessionTransaction
  53. from .unitofwork import UOWTransaction
  54. from ..engine import Connection
  55. from ..event.base import _Dispatch
  56. from ..event.base import _HasEventsDispatch
  57. from ..event.registry import _EventKey
  58. from ..orm.collections import CollectionAdapter
  59. from ..orm.context import QueryContext
  60. from ..orm.decl_api import DeclarativeAttributeIntercept
  61. from ..orm.decl_api import DeclarativeMeta
  62. from ..orm.mapper import Mapper
  63. from ..orm.state import InstanceState
  64. _KT = TypeVar("_KT", bound=Any)
  65. _ET2 = TypeVar("_ET2", bound=EventTarget)
  66. class InstrumentationEvents(event.Events[InstrumentationFactory]):
  67. """Events related to class instrumentation events.
  68. The listeners here support being established against
  69. any new style class, that is any object that is a subclass
  70. of 'type'. Events will then be fired off for events
  71. against that class. If the "propagate=True" flag is passed
  72. to event.listen(), the event will fire off for subclasses
  73. of that class as well.
  74. The Python ``type`` builtin is also accepted as a target,
  75. which when used has the effect of events being emitted
  76. for all classes.
  77. Note the "propagate" flag here is defaulted to ``True``,
  78. unlike the other class level events where it defaults
  79. to ``False``. This means that new subclasses will also
  80. be the subject of these events, when a listener
  81. is established on a superclass.
  82. """
  83. _target_class_doc = "SomeBaseClass"
  84. _dispatch_target = InstrumentationFactory
  85. @classmethod
  86. def _accept_with(
  87. cls,
  88. target: Union[
  89. InstrumentationFactory,
  90. Type[InstrumentationFactory],
  91. ],
  92. identifier: str,
  93. ) -> Optional[
  94. Union[
  95. InstrumentationFactory,
  96. Type[InstrumentationFactory],
  97. ]
  98. ]:
  99. if isinstance(target, type):
  100. return _InstrumentationEventsHold(target) # type: ignore [return-value] # noqa: E501
  101. else:
  102. return None
  103. @classmethod
  104. def _listen(
  105. cls, event_key: _EventKey[_T], propagate: bool = True, **kw: Any
  106. ) -> None:
  107. target, identifier, fn = (
  108. event_key.dispatch_target,
  109. event_key.identifier,
  110. event_key._listen_fn,
  111. )
  112. def listen(target_cls: type, *arg: Any) -> Optional[Any]:
  113. listen_cls = target()
  114. # if weakref were collected, however this is not something
  115. # that normally happens. it was occurring during test teardown
  116. # between mapper/registry/instrumentation_manager, however this
  117. # interaction was changed to not rely upon the event system.
  118. if listen_cls is None:
  119. return None
  120. if propagate and issubclass(target_cls, listen_cls):
  121. return fn(target_cls, *arg)
  122. elif not propagate and target_cls is listen_cls:
  123. return fn(target_cls, *arg)
  124. else:
  125. return None
  126. def remove(ref: ReferenceType[_T]) -> None:
  127. key = event.registry._EventKey( # type: ignore [type-var]
  128. None,
  129. identifier,
  130. listen,
  131. instrumentation._instrumentation_factory,
  132. )
  133. getattr(
  134. instrumentation._instrumentation_factory.dispatch, identifier
  135. ).remove(key)
  136. target = weakref.ref(target.class_, remove)
  137. event_key.with_dispatch_target(
  138. instrumentation._instrumentation_factory
  139. ).with_wrapper(listen).base_listen(**kw)
  140. @classmethod
  141. def _clear(cls) -> None:
  142. super()._clear()
  143. instrumentation._instrumentation_factory.dispatch._clear()
  144. def class_instrument(self, cls: ClassManager[_O]) -> None:
  145. """Called after the given class is instrumented.
  146. To get at the :class:`.ClassManager`, use
  147. :func:`.manager_of_class`.
  148. """
  149. def class_uninstrument(self, cls: ClassManager[_O]) -> None:
  150. """Called before the given class is uninstrumented.
  151. To get at the :class:`.ClassManager`, use
  152. :func:`.manager_of_class`.
  153. """
  154. def attribute_instrument(
  155. self, cls: ClassManager[_O], key: _KT, inst: _O
  156. ) -> None:
  157. """Called when an attribute is instrumented."""
  158. class _InstrumentationEventsHold:
  159. """temporary marker object used to transfer from _accept_with() to
  160. _listen() on the InstrumentationEvents class.
  161. """
  162. def __init__(self, class_: type) -> None:
  163. self.class_ = class_
  164. dispatch = event.dispatcher(InstrumentationEvents)
  165. class InstanceEvents(event.Events[ClassManager[Any]]):
  166. """Define events specific to object lifecycle.
  167. e.g.::
  168. from sqlalchemy import event
  169. def my_load_listener(target, context):
  170. print("on load!")
  171. event.listen(SomeClass, "load", my_load_listener)
  172. Available targets include:
  173. * mapped classes
  174. * unmapped superclasses of mapped or to-be-mapped classes
  175. (using the ``propagate=True`` flag)
  176. * :class:`_orm.Mapper` objects
  177. * the :class:`_orm.Mapper` class itself indicates listening for all
  178. mappers.
  179. Instance events are closely related to mapper events, but
  180. are more specific to the instance and its instrumentation,
  181. rather than its system of persistence.
  182. When using :class:`.InstanceEvents`, several modifiers are
  183. available to the :func:`.event.listen` function.
  184. :param propagate=False: When True, the event listener should
  185. be applied to all inheriting classes as well as the
  186. class which is the target of this listener.
  187. :param raw=False: When True, the "target" argument passed
  188. to applicable event listener functions will be the
  189. instance's :class:`.InstanceState` management
  190. object, rather than the mapped instance itself.
  191. :param restore_load_context=False: Applies to the
  192. :meth:`.InstanceEvents.load` and :meth:`.InstanceEvents.refresh`
  193. events. Restores the loader context of the object when the event
  194. hook is complete, so that ongoing eager load operations continue
  195. to target the object appropriately. A warning is emitted if the
  196. object is moved to a new loader context from within one of these
  197. events if this flag is not set.
  198. .. versionadded:: 1.3.14
  199. """
  200. _target_class_doc = "SomeClass"
  201. _dispatch_target = ClassManager
  202. @classmethod
  203. def _new_classmanager_instance(
  204. cls,
  205. class_: Union[DeclarativeAttributeIntercept, DeclarativeMeta, type],
  206. classmanager: ClassManager[_O],
  207. ) -> None:
  208. _InstanceEventsHold.populate(class_, classmanager)
  209. @classmethod
  210. @util.preload_module("sqlalchemy.orm")
  211. def _accept_with(
  212. cls,
  213. target: Union[
  214. ClassManager[Any],
  215. Type[ClassManager[Any]],
  216. ],
  217. identifier: str,
  218. ) -> Optional[Union[ClassManager[Any], Type[ClassManager[Any]]]]:
  219. orm = util.preloaded.orm
  220. if isinstance(target, ClassManager):
  221. return target
  222. elif isinstance(target, mapperlib.Mapper):
  223. return target.class_manager
  224. elif target is orm.mapper: # type: ignore [attr-defined]
  225. util.warn_deprecated(
  226. "The `sqlalchemy.orm.mapper()` symbol is deprecated and "
  227. "will be removed in a future release. For the mapper-wide "
  228. "event target, use the 'sqlalchemy.orm.Mapper' class.",
  229. "2.0",
  230. )
  231. return ClassManager
  232. elif isinstance(target, type):
  233. if issubclass(target, mapperlib.Mapper):
  234. return ClassManager
  235. else:
  236. manager = instrumentation.opt_manager_of_class(target)
  237. if manager:
  238. return manager
  239. else:
  240. return _InstanceEventsHold(target) # type: ignore [return-value] # noqa: E501
  241. return None
  242. @classmethod
  243. def _listen(
  244. cls,
  245. event_key: _EventKey[ClassManager[Any]],
  246. raw: bool = False,
  247. propagate: bool = False,
  248. restore_load_context: bool = False,
  249. **kw: Any,
  250. ) -> None:
  251. target, fn = (event_key.dispatch_target, event_key._listen_fn)
  252. if not raw or restore_load_context:
  253. def wrap(
  254. state: InstanceState[_O], *arg: Any, **kw: Any
  255. ) -> Optional[Any]:
  256. if not raw:
  257. target: Any = state.obj()
  258. else:
  259. target = state
  260. if restore_load_context:
  261. runid = state.runid
  262. try:
  263. return fn(target, *arg, **kw)
  264. finally:
  265. if restore_load_context:
  266. state.runid = runid
  267. event_key = event_key.with_wrapper(wrap)
  268. event_key.base_listen(propagate=propagate, **kw)
  269. if propagate:
  270. for mgr in target.subclass_managers(True):
  271. event_key.with_dispatch_target(mgr).base_listen(propagate=True)
  272. @classmethod
  273. def _clear(cls) -> None:
  274. super()._clear()
  275. _InstanceEventsHold._clear()
  276. def first_init(self, manager: ClassManager[_O], cls: Type[_O]) -> None:
  277. """Called when the first instance of a particular mapping is called.
  278. This event is called when the ``__init__`` method of a class
  279. is called the first time for that particular class. The event
  280. invokes before ``__init__`` actually proceeds as well as before
  281. the :meth:`.InstanceEvents.init` event is invoked.
  282. """
  283. def init(self, target: _O, args: Any, kwargs: Any) -> None:
  284. """Receive an instance when its constructor is called.
  285. This method is only called during a userland construction of
  286. an object, in conjunction with the object's constructor, e.g.
  287. its ``__init__`` method. It is not called when an object is
  288. loaded from the database; see the :meth:`.InstanceEvents.load`
  289. event in order to intercept a database load.
  290. The event is called before the actual ``__init__`` constructor
  291. of the object is called. The ``kwargs`` dictionary may be
  292. modified in-place in order to affect what is passed to
  293. ``__init__``.
  294. :param target: the mapped instance. If
  295. the event is configured with ``raw=True``, this will
  296. instead be the :class:`.InstanceState` state-management
  297. object associated with the instance.
  298. :param args: positional arguments passed to the ``__init__`` method.
  299. This is passed as a tuple and is currently immutable.
  300. :param kwargs: keyword arguments passed to the ``__init__`` method.
  301. This structure *can* be altered in place.
  302. .. seealso::
  303. :meth:`.InstanceEvents.init_failure`
  304. :meth:`.InstanceEvents.load`
  305. """
  306. def init_failure(self, target: _O, args: Any, kwargs: Any) -> None:
  307. """Receive an instance when its constructor has been called,
  308. and raised an exception.
  309. This method is only called during a userland construction of
  310. an object, in conjunction with the object's constructor, e.g.
  311. its ``__init__`` method. It is not called when an object is loaded
  312. from the database.
  313. The event is invoked after an exception raised by the ``__init__``
  314. method is caught. After the event
  315. is invoked, the original exception is re-raised outwards, so that
  316. the construction of the object still raises an exception. The
  317. actual exception and stack trace raised should be present in
  318. ``sys.exc_info()``.
  319. :param target: the mapped instance. If
  320. the event is configured with ``raw=True``, this will
  321. instead be the :class:`.InstanceState` state-management
  322. object associated with the instance.
  323. :param args: positional arguments that were passed to the ``__init__``
  324. method.
  325. :param kwargs: keyword arguments that were passed to the ``__init__``
  326. method.
  327. .. seealso::
  328. :meth:`.InstanceEvents.init`
  329. :meth:`.InstanceEvents.load`
  330. """
  331. def _sa_event_merge_wo_load(
  332. self, target: _O, context: QueryContext
  333. ) -> None:
  334. """receive an object instance after it was the subject of a merge()
  335. call, when load=False was passed.
  336. The target would be the already-loaded object in the Session which
  337. would have had its attributes overwritten by the incoming object. This
  338. overwrite operation does not use attribute events, instead just
  339. populating dict directly. Therefore the purpose of this event is so
  340. that extensions like sqlalchemy.ext.mutable know that object state has
  341. changed and incoming state needs to be set up for "parents" etc.
  342. This functionality is acceptable to be made public in a later release.
  343. .. versionadded:: 1.4.41
  344. """
  345. def load(self, target: _O, context: QueryContext) -> None:
  346. """Receive an object instance after it has been created via
  347. ``__new__``, and after initial attribute population has
  348. occurred.
  349. This typically occurs when the instance is created based on
  350. incoming result rows, and is only called once for that
  351. instance's lifetime.
  352. .. warning::
  353. During a result-row load, this event is invoked when the
  354. first row received for this instance is processed. When using
  355. eager loading with collection-oriented attributes, the additional
  356. rows that are to be loaded / processed in order to load subsequent
  357. collection items have not occurred yet. This has the effect
  358. both that collections will not be fully loaded, as well as that
  359. if an operation occurs within this event handler that emits
  360. another database load operation for the object, the "loading
  361. context" for the object can change and interfere with the
  362. existing eager loaders still in progress.
  363. Examples of what can cause the "loading context" to change within
  364. the event handler include, but are not necessarily limited to:
  365. * accessing deferred attributes that weren't part of the row,
  366. will trigger an "undefer" operation and refresh the object
  367. * accessing attributes on a joined-inheritance subclass that
  368. weren't part of the row, will trigger a refresh operation.
  369. As of SQLAlchemy 1.3.14, a warning is emitted when this occurs. The
  370. :paramref:`.InstanceEvents.restore_load_context` option may be
  371. used on the event to prevent this warning; this will ensure that
  372. the existing loading context is maintained for the object after the
  373. event is called::
  374. @event.listens_for(SomeClass, "load", restore_load_context=True)
  375. def on_load(instance, context):
  376. instance.some_unloaded_attribute
  377. .. versionchanged:: 1.3.14 Added
  378. :paramref:`.InstanceEvents.restore_load_context`
  379. and :paramref:`.SessionEvents.restore_load_context` flags which
  380. apply to "on load" events, which will ensure that the loading
  381. context for an object is restored when the event hook is
  382. complete; a warning is emitted if the load context of the object
  383. changes without this flag being set.
  384. The :meth:`.InstanceEvents.load` event is also available in a
  385. class-method decorator format called :func:`_orm.reconstructor`.
  386. :param target: the mapped instance. If
  387. the event is configured with ``raw=True``, this will
  388. instead be the :class:`.InstanceState` state-management
  389. object associated with the instance.
  390. :param context: the :class:`.QueryContext` corresponding to the
  391. current :class:`_query.Query` in progress. This argument may be
  392. ``None`` if the load does not correspond to a :class:`_query.Query`,
  393. such as during :meth:`.Session.merge`.
  394. .. seealso::
  395. :ref:`mapped_class_load_events`
  396. :meth:`.InstanceEvents.init`
  397. :meth:`.InstanceEvents.refresh`
  398. :meth:`.SessionEvents.loaded_as_persistent`
  399. """ # noqa: E501
  400. def refresh(
  401. self, target: _O, context: QueryContext, attrs: Optional[Iterable[str]]
  402. ) -> None:
  403. """Receive an object instance after one or more attributes have
  404. been refreshed from a query.
  405. Contrast this to the :meth:`.InstanceEvents.load` method, which
  406. is invoked when the object is first loaded from a query.
  407. .. note:: This event is invoked within the loader process before
  408. eager loaders may have been completed, and the object's state may
  409. not be complete. Additionally, invoking row-level refresh
  410. operations on the object will place the object into a new loader
  411. context, interfering with the existing load context. See the note
  412. on :meth:`.InstanceEvents.load` for background on making use of the
  413. :paramref:`.InstanceEvents.restore_load_context` parameter, in
  414. order to resolve this scenario.
  415. :param target: the mapped instance. If
  416. the event is configured with ``raw=True``, this will
  417. instead be the :class:`.InstanceState` state-management
  418. object associated with the instance.
  419. :param context: the :class:`.QueryContext` corresponding to the
  420. current :class:`_query.Query` in progress.
  421. :param attrs: sequence of attribute names which
  422. were populated, or None if all column-mapped, non-deferred
  423. attributes were populated.
  424. .. seealso::
  425. :ref:`mapped_class_load_events`
  426. :meth:`.InstanceEvents.load`
  427. """
  428. def refresh_flush(
  429. self,
  430. target: _O,
  431. flush_context: UOWTransaction,
  432. attrs: Optional[Iterable[str]],
  433. ) -> None:
  434. """Receive an object instance after one or more attributes that
  435. contain a column-level default or onupdate handler have been refreshed
  436. during persistence of the object's state.
  437. This event is the same as :meth:`.InstanceEvents.refresh` except
  438. it is invoked within the unit of work flush process, and includes
  439. only non-primary-key columns that have column level default or
  440. onupdate handlers, including Python callables as well as server side
  441. defaults and triggers which may be fetched via the RETURNING clause.
  442. .. note::
  443. While the :meth:`.InstanceEvents.refresh_flush` event is triggered
  444. for an object that was INSERTed as well as for an object that was
  445. UPDATEd, the event is geared primarily towards the UPDATE process;
  446. it is mostly an internal artifact that INSERT actions can also
  447. trigger this event, and note that **primary key columns for an
  448. INSERTed row are explicitly omitted** from this event. In order to
  449. intercept the newly INSERTed state of an object, the
  450. :meth:`.SessionEvents.pending_to_persistent` and
  451. :meth:`.MapperEvents.after_insert` are better choices.
  452. :param target: the mapped instance. If
  453. the event is configured with ``raw=True``, this will
  454. instead be the :class:`.InstanceState` state-management
  455. object associated with the instance.
  456. :param flush_context: Internal :class:`.UOWTransaction` object
  457. which handles the details of the flush.
  458. :param attrs: sequence of attribute names which
  459. were populated.
  460. .. seealso::
  461. :ref:`mapped_class_load_events`
  462. :ref:`orm_server_defaults`
  463. :ref:`metadata_defaults_toplevel`
  464. """
  465. def expire(self, target: _O, attrs: Optional[Iterable[str]]) -> None:
  466. """Receive an object instance after its attributes or some subset
  467. have been expired.
  468. 'keys' is a list of attribute names. If None, the entire
  469. state was expired.
  470. :param target: the mapped instance. If
  471. the event is configured with ``raw=True``, this will
  472. instead be the :class:`.InstanceState` state-management
  473. object associated with the instance.
  474. :param attrs: sequence of attribute
  475. names which were expired, or None if all attributes were
  476. expired.
  477. """
  478. def pickle(self, target: _O, state_dict: _InstanceDict) -> None:
  479. """Receive an object instance when its associated state is
  480. being pickled.
  481. :param target: the mapped instance. If
  482. the event is configured with ``raw=True``, this will
  483. instead be the :class:`.InstanceState` state-management
  484. object associated with the instance.
  485. :param state_dict: the dictionary returned by
  486. :class:`.InstanceState.__getstate__`, containing the state
  487. to be pickled.
  488. """
  489. def unpickle(self, target: _O, state_dict: _InstanceDict) -> None:
  490. """Receive an object instance after its associated state has
  491. been unpickled.
  492. :param target: the mapped instance. If
  493. the event is configured with ``raw=True``, this will
  494. instead be the :class:`.InstanceState` state-management
  495. object associated with the instance.
  496. :param state_dict: the dictionary sent to
  497. :class:`.InstanceState.__setstate__`, containing the state
  498. dictionary which was pickled.
  499. """
  500. class _EventsHold(event.RefCollection[_ET]):
  501. """Hold onto listeners against unmapped, uninstrumented classes.
  502. Establish _listen() for that class' mapper/instrumentation when
  503. those objects are created for that class.
  504. """
  505. all_holds: weakref.WeakKeyDictionary[Any, Any]
  506. def __init__(
  507. self,
  508. class_: Union[DeclarativeAttributeIntercept, DeclarativeMeta, type],
  509. ) -> None:
  510. self.class_ = class_
  511. @classmethod
  512. def _clear(cls) -> None:
  513. cls.all_holds.clear()
  514. class HoldEvents(Generic[_ET2]):
  515. _dispatch_target: Optional[Type[_ET2]] = None
  516. @classmethod
  517. def _listen(
  518. cls,
  519. event_key: _EventKey[_ET2],
  520. raw: bool = False,
  521. propagate: bool = False,
  522. retval: bool = False,
  523. **kw: Any,
  524. ) -> None:
  525. target = event_key.dispatch_target
  526. if target.class_ in target.all_holds:
  527. collection = target.all_holds[target.class_]
  528. else:
  529. collection = target.all_holds[target.class_] = {}
  530. event.registry._stored_in_collection(event_key, target)
  531. collection[event_key._key] = (
  532. event_key,
  533. raw,
  534. propagate,
  535. retval,
  536. kw,
  537. )
  538. if propagate:
  539. stack = list(target.class_.__subclasses__())
  540. while stack:
  541. subclass = stack.pop(0)
  542. stack.extend(subclass.__subclasses__())
  543. subject = target.resolve(subclass)
  544. if subject is not None:
  545. # we are already going through __subclasses__()
  546. # so leave generic propagate flag False
  547. event_key.with_dispatch_target(subject).listen(
  548. raw=raw, propagate=False, retval=retval, **kw
  549. )
  550. def remove(self, event_key: _EventKey[_ET]) -> None:
  551. target = event_key.dispatch_target
  552. if isinstance(target, _EventsHold):
  553. collection = target.all_holds[target.class_]
  554. del collection[event_key._key]
  555. @classmethod
  556. def populate(
  557. cls,
  558. class_: Union[DeclarativeAttributeIntercept, DeclarativeMeta, type],
  559. subject: Union[ClassManager[_O], Mapper[_O]],
  560. ) -> None:
  561. for subclass in class_.__mro__:
  562. if subclass in cls.all_holds:
  563. collection = cls.all_holds[subclass]
  564. for (
  565. event_key,
  566. raw,
  567. propagate,
  568. retval,
  569. kw,
  570. ) in collection.values():
  571. if propagate or subclass is class_:
  572. # since we can't be sure in what order different
  573. # classes in a hierarchy are triggered with
  574. # populate(), we rely upon _EventsHold for all event
  575. # assignment, instead of using the generic propagate
  576. # flag.
  577. event_key.with_dispatch_target(subject).listen(
  578. raw=raw, propagate=False, retval=retval, **kw
  579. )
  580. class _InstanceEventsHold(_EventsHold[_ET]):
  581. all_holds: weakref.WeakKeyDictionary[Any, Any] = (
  582. weakref.WeakKeyDictionary()
  583. )
  584. def resolve(self, class_: Type[_O]) -> Optional[ClassManager[_O]]:
  585. return instrumentation.opt_manager_of_class(class_)
  586. class HoldInstanceEvents(_EventsHold.HoldEvents[_ET], InstanceEvents): # type: ignore [misc] # noqa: E501
  587. pass
  588. dispatch = event.dispatcher(HoldInstanceEvents)
  589. class MapperEvents(event.Events[mapperlib.Mapper[Any]]):
  590. """Define events specific to mappings.
  591. e.g.::
  592. from sqlalchemy import event
  593. def my_before_insert_listener(mapper, connection, target):
  594. # execute a stored procedure upon INSERT,
  595. # apply the value to the row to be inserted
  596. target.calculated_value = connection.execute(
  597. text("select my_special_function(%d)" % target.special_number)
  598. ).scalar()
  599. # associate the listener function with SomeClass,
  600. # to execute during the "before_insert" hook
  601. event.listen(SomeClass, "before_insert", my_before_insert_listener)
  602. Available targets include:
  603. * mapped classes
  604. * unmapped superclasses of mapped or to-be-mapped classes
  605. (using the ``propagate=True`` flag)
  606. * :class:`_orm.Mapper` objects
  607. * the :class:`_orm.Mapper` class itself indicates listening for all
  608. mappers.
  609. Mapper events provide hooks into critical sections of the
  610. mapper, including those related to object instrumentation,
  611. object loading, and object persistence. In particular, the
  612. persistence methods :meth:`~.MapperEvents.before_insert`,
  613. and :meth:`~.MapperEvents.before_update` are popular
  614. places to augment the state being persisted - however, these
  615. methods operate with several significant restrictions. The
  616. user is encouraged to evaluate the
  617. :meth:`.SessionEvents.before_flush` and
  618. :meth:`.SessionEvents.after_flush` methods as more
  619. flexible and user-friendly hooks in which to apply
  620. additional database state during a flush.
  621. When using :class:`.MapperEvents`, several modifiers are
  622. available to the :func:`.event.listen` function.
  623. :param propagate=False: When True, the event listener should
  624. be applied to all inheriting mappers and/or the mappers of
  625. inheriting classes, as well as any
  626. mapper which is the target of this listener.
  627. :param raw=False: When True, the "target" argument passed
  628. to applicable event listener functions will be the
  629. instance's :class:`.InstanceState` management
  630. object, rather than the mapped instance itself.
  631. :param retval=False: when True, the user-defined event function
  632. must have a return value, the purpose of which is either to
  633. control subsequent event propagation, or to otherwise alter
  634. the operation in progress by the mapper. Possible return
  635. values are:
  636. * ``sqlalchemy.orm.interfaces.EXT_CONTINUE`` - continue event
  637. processing normally.
  638. * ``sqlalchemy.orm.interfaces.EXT_STOP`` - cancel all subsequent
  639. event handlers in the chain.
  640. * other values - the return value specified by specific listeners.
  641. """
  642. _target_class_doc = "SomeClass"
  643. _dispatch_target = mapperlib.Mapper
  644. @classmethod
  645. def _new_mapper_instance(
  646. cls,
  647. class_: Union[DeclarativeAttributeIntercept, DeclarativeMeta, type],
  648. mapper: Mapper[_O],
  649. ) -> None:
  650. _MapperEventsHold.populate(class_, mapper)
  651. @classmethod
  652. @util.preload_module("sqlalchemy.orm")
  653. def _accept_with(
  654. cls,
  655. target: Union[mapperlib.Mapper[Any], Type[mapperlib.Mapper[Any]]],
  656. identifier: str,
  657. ) -> Optional[Union[mapperlib.Mapper[Any], Type[mapperlib.Mapper[Any]]]]:
  658. orm = util.preloaded.orm
  659. if target is orm.mapper: # type: ignore [attr-defined]
  660. util.warn_deprecated(
  661. "The `sqlalchemy.orm.mapper()` symbol is deprecated and "
  662. "will be removed in a future release. For the mapper-wide "
  663. "event target, use the 'sqlalchemy.orm.Mapper' class.",
  664. "2.0",
  665. )
  666. return mapperlib.Mapper
  667. elif isinstance(target, type):
  668. if issubclass(target, mapperlib.Mapper):
  669. return target
  670. else:
  671. mapper = _mapper_or_none(target)
  672. if mapper is not None:
  673. return mapper
  674. else:
  675. return _MapperEventsHold(target)
  676. else:
  677. return target
  678. @classmethod
  679. def _listen(
  680. cls,
  681. event_key: _EventKey[_ET],
  682. raw: bool = False,
  683. retval: bool = False,
  684. propagate: bool = False,
  685. **kw: Any,
  686. ) -> None:
  687. target, identifier, fn = (
  688. event_key.dispatch_target,
  689. event_key.identifier,
  690. event_key._listen_fn,
  691. )
  692. if (
  693. identifier in ("before_configured", "after_configured")
  694. and target is not mapperlib.Mapper
  695. ):
  696. util.warn(
  697. "'before_configured' and 'after_configured' ORM events "
  698. "only invoke with the Mapper class "
  699. "as the target."
  700. )
  701. if not raw or not retval:
  702. if not raw:
  703. meth = getattr(cls, identifier)
  704. try:
  705. target_index = (
  706. inspect_getfullargspec(meth)[0].index("target") - 1
  707. )
  708. except ValueError:
  709. target_index = None
  710. def wrap(*arg: Any, **kw: Any) -> Any:
  711. if not raw and target_index is not None:
  712. arg = list(arg) # type: ignore [assignment]
  713. arg[target_index] = arg[target_index].obj() # type: ignore [index] # noqa: E501
  714. if not retval:
  715. fn(*arg, **kw)
  716. return interfaces.EXT_CONTINUE
  717. else:
  718. return fn(*arg, **kw)
  719. event_key = event_key.with_wrapper(wrap)
  720. if propagate:
  721. for mapper in target.self_and_descendants:
  722. event_key.with_dispatch_target(mapper).base_listen(
  723. propagate=True, **kw
  724. )
  725. else:
  726. event_key.base_listen(**kw)
  727. @classmethod
  728. def _clear(cls) -> None:
  729. super()._clear()
  730. _MapperEventsHold._clear()
  731. def instrument_class(self, mapper: Mapper[_O], class_: Type[_O]) -> None:
  732. r"""Receive a class when the mapper is first constructed,
  733. before instrumentation is applied to the mapped class.
  734. This event is the earliest phase of mapper construction.
  735. Most attributes of the mapper are not yet initialized. To
  736. receive an event within initial mapper construction where basic
  737. state is available such as the :attr:`_orm.Mapper.attrs` collection,
  738. the :meth:`_orm.MapperEvents.after_mapper_constructed` event may
  739. be a better choice.
  740. This listener can either be applied to the :class:`_orm.Mapper`
  741. class overall, or to any un-mapped class which serves as a base
  742. for classes that will be mapped (using the ``propagate=True`` flag)::
  743. Base = declarative_base()
  744. @event.listens_for(Base, "instrument_class", propagate=True)
  745. def on_new_class(mapper, cls_):
  746. "..."
  747. :param mapper: the :class:`_orm.Mapper` which is the target
  748. of this event.
  749. :param class\_: the mapped class.
  750. .. seealso::
  751. :meth:`_orm.MapperEvents.after_mapper_constructed`
  752. """
  753. def after_mapper_constructed(
  754. self, mapper: Mapper[_O], class_: Type[_O]
  755. ) -> None:
  756. """Receive a class and mapper when the :class:`_orm.Mapper` has been
  757. fully constructed.
  758. This event is called after the initial constructor for
  759. :class:`_orm.Mapper` completes. This occurs after the
  760. :meth:`_orm.MapperEvents.instrument_class` event and after the
  761. :class:`_orm.Mapper` has done an initial pass of its arguments
  762. to generate its collection of :class:`_orm.MapperProperty` objects,
  763. which are accessible via the :meth:`_orm.Mapper.get_property`
  764. method and the :attr:`_orm.Mapper.iterate_properties` attribute.
  765. This event differs from the
  766. :meth:`_orm.MapperEvents.before_mapper_configured` event in that it
  767. is invoked within the constructor for :class:`_orm.Mapper`, rather
  768. than within the :meth:`_orm.registry.configure` process. Currently,
  769. this event is the only one which is appropriate for handlers that
  770. wish to create additional mapped classes in response to the
  771. construction of this :class:`_orm.Mapper`, which will be part of the
  772. same configure step when :meth:`_orm.registry.configure` next runs.
  773. .. versionadded:: 2.0.2
  774. .. seealso::
  775. :ref:`examples_versioning` - an example which illustrates the use
  776. of the :meth:`_orm.MapperEvents.before_mapper_configured`
  777. event to create new mappers to record change-audit histories on
  778. objects.
  779. """
  780. def before_mapper_configured(
  781. self, mapper: Mapper[_O], class_: Type[_O]
  782. ) -> None:
  783. """Called right before a specific mapper is to be configured.
  784. This event is intended to allow a specific mapper to be skipped during
  785. the configure step, by returning the :attr:`.orm.interfaces.EXT_SKIP`
  786. symbol which indicates to the :func:`.configure_mappers` call that this
  787. particular mapper (or hierarchy of mappers, if ``propagate=True`` is
  788. used) should be skipped in the current configuration run. When one or
  789. more mappers are skipped, the "new mappers" flag will remain set,
  790. meaning the :func:`.configure_mappers` function will continue to be
  791. called when mappers are used, to continue to try to configure all
  792. available mappers.
  793. In comparison to the other configure-level events,
  794. :meth:`.MapperEvents.before_configured`,
  795. :meth:`.MapperEvents.after_configured`, and
  796. :meth:`.MapperEvents.mapper_configured`, the
  797. :meth:`.MapperEvents.before_mapper_configured` event provides for a
  798. meaningful return value when it is registered with the ``retval=True``
  799. parameter.
  800. .. versionadded:: 1.3
  801. e.g.::
  802. from sqlalchemy.orm import EXT_SKIP
  803. Base = declarative_base()
  804. DontConfigureBase = declarative_base()
  805. @event.listens_for(
  806. DontConfigureBase,
  807. "before_mapper_configured",
  808. retval=True,
  809. propagate=True,
  810. )
  811. def dont_configure(mapper, cls):
  812. return EXT_SKIP
  813. .. seealso::
  814. :meth:`.MapperEvents.before_configured`
  815. :meth:`.MapperEvents.after_configured`
  816. :meth:`.MapperEvents.mapper_configured`
  817. """
  818. def mapper_configured(self, mapper: Mapper[_O], class_: Type[_O]) -> None:
  819. r"""Called when a specific mapper has completed its own configuration
  820. within the scope of the :func:`.configure_mappers` call.
  821. The :meth:`.MapperEvents.mapper_configured` event is invoked
  822. for each mapper that is encountered when the
  823. :func:`_orm.configure_mappers` function proceeds through the current
  824. list of not-yet-configured mappers.
  825. :func:`_orm.configure_mappers` is typically invoked
  826. automatically as mappings are first used, as well as each time
  827. new mappers have been made available and new mapper use is
  828. detected.
  829. When the event is called, the mapper should be in its final
  830. state, but **not including backrefs** that may be invoked from
  831. other mappers; they might still be pending within the
  832. configuration operation. Bidirectional relationships that
  833. are instead configured via the
  834. :paramref:`.orm.relationship.back_populates` argument
  835. *will* be fully available, since this style of relationship does not
  836. rely upon other possibly-not-configured mappers to know that they
  837. exist.
  838. For an event that is guaranteed to have **all** mappers ready
  839. to go including backrefs that are defined only on other
  840. mappings, use the :meth:`.MapperEvents.after_configured`
  841. event; this event invokes only after all known mappings have been
  842. fully configured.
  843. The :meth:`.MapperEvents.mapper_configured` event, unlike
  844. :meth:`.MapperEvents.before_configured` or
  845. :meth:`.MapperEvents.after_configured`,
  846. is called for each mapper/class individually, and the mapper is
  847. passed to the event itself. It also is called exactly once for
  848. a particular mapper. The event is therefore useful for
  849. configurational steps that benefit from being invoked just once
  850. on a specific mapper basis, which don't require that "backref"
  851. configurations are necessarily ready yet.
  852. :param mapper: the :class:`_orm.Mapper` which is the target
  853. of this event.
  854. :param class\_: the mapped class.
  855. .. seealso::
  856. :meth:`.MapperEvents.before_configured`
  857. :meth:`.MapperEvents.after_configured`
  858. :meth:`.MapperEvents.before_mapper_configured`
  859. """
  860. # TODO: need coverage for this event
  861. def before_configured(self) -> None:
  862. """Called before a series of mappers have been configured.
  863. The :meth:`.MapperEvents.before_configured` event is invoked
  864. each time the :func:`_orm.configure_mappers` function is
  865. invoked, before the function has done any of its work.
  866. :func:`_orm.configure_mappers` is typically invoked
  867. automatically as mappings are first used, as well as each time
  868. new mappers have been made available and new mapper use is
  869. detected.
  870. This event can **only** be applied to the :class:`_orm.Mapper` class,
  871. and not to individual mappings or mapped classes. It is only invoked
  872. for all mappings as a whole::
  873. from sqlalchemy.orm import Mapper
  874. @event.listens_for(Mapper, "before_configured")
  875. def go(): ...
  876. Contrast this event to :meth:`.MapperEvents.after_configured`,
  877. which is invoked after the series of mappers has been configured,
  878. as well as :meth:`.MapperEvents.before_mapper_configured`
  879. and :meth:`.MapperEvents.mapper_configured`, which are both invoked
  880. on a per-mapper basis.
  881. Theoretically this event is called once per
  882. application, but is actually called any time new mappers
  883. are to be affected by a :func:`_orm.configure_mappers`
  884. call. If new mappings are constructed after existing ones have
  885. already been used, this event will likely be called again. To ensure
  886. that a particular event is only called once and no further, the
  887. ``once=True`` argument (new in 0.9.4) can be applied::
  888. from sqlalchemy.orm import mapper
  889. @event.listens_for(mapper, "before_configured", once=True)
  890. def go(): ...
  891. .. seealso::
  892. :meth:`.MapperEvents.before_mapper_configured`
  893. :meth:`.MapperEvents.mapper_configured`
  894. :meth:`.MapperEvents.after_configured`
  895. """
  896. def after_configured(self) -> None:
  897. """Called after a series of mappers have been configured.
  898. The :meth:`.MapperEvents.after_configured` event is invoked
  899. each time the :func:`_orm.configure_mappers` function is
  900. invoked, after the function has completed its work.
  901. :func:`_orm.configure_mappers` is typically invoked
  902. automatically as mappings are first used, as well as each time
  903. new mappers have been made available and new mapper use is
  904. detected.
  905. Contrast this event to the :meth:`.MapperEvents.mapper_configured`
  906. event, which is called on a per-mapper basis while the configuration
  907. operation proceeds; unlike that event, when this event is invoked,
  908. all cross-configurations (e.g. backrefs) will also have been made
  909. available for any mappers that were pending.
  910. Also contrast to :meth:`.MapperEvents.before_configured`,
  911. which is invoked before the series of mappers has been configured.
  912. This event can **only** be applied to the :class:`_orm.Mapper` class,
  913. and not to individual mappings or
  914. mapped classes. It is only invoked for all mappings as a whole::
  915. from sqlalchemy.orm import Mapper
  916. @event.listens_for(Mapper, "after_configured")
  917. def go(): ...
  918. Theoretically this event is called once per
  919. application, but is actually called any time new mappers
  920. have been affected by a :func:`_orm.configure_mappers`
  921. call. If new mappings are constructed after existing ones have
  922. already been used, this event will likely be called again. To ensure
  923. that a particular event is only called once and no further, the
  924. ``once=True`` argument (new in 0.9.4) can be applied::
  925. from sqlalchemy.orm import mapper
  926. @event.listens_for(mapper, "after_configured", once=True)
  927. def go(): ...
  928. .. seealso::
  929. :meth:`.MapperEvents.before_mapper_configured`
  930. :meth:`.MapperEvents.mapper_configured`
  931. :meth:`.MapperEvents.before_configured`
  932. """
  933. def before_insert(
  934. self, mapper: Mapper[_O], connection: Connection, target: _O
  935. ) -> None:
  936. """Receive an object instance before an INSERT statement
  937. is emitted corresponding to that instance.
  938. .. note:: this event **only** applies to the
  939. :ref:`session flush operation <session_flushing>`
  940. and does **not** apply to the ORM DML operations described at
  941. :ref:`orm_expression_update_delete`. To intercept ORM
  942. DML events, use :meth:`_orm.SessionEvents.do_orm_execute`.
  943. This event is used to modify local, non-object related
  944. attributes on the instance before an INSERT occurs, as well
  945. as to emit additional SQL statements on the given
  946. connection.
  947. The event is often called for a batch of objects of the
  948. same class before their INSERT statements are emitted at
  949. once in a later step. In the extremely rare case that
  950. this is not desirable, the :class:`_orm.Mapper` object can be
  951. configured with ``batch=False``, which will cause
  952. batches of instances to be broken up into individual
  953. (and more poorly performing) event->persist->event
  954. steps.
  955. .. warning::
  956. Mapper-level flush events only allow **very limited operations**,
  957. on attributes local to the row being operated upon only,
  958. as well as allowing any SQL to be emitted on the given
  959. :class:`_engine.Connection`. **Please read fully** the notes
  960. at :ref:`session_persistence_mapper` for guidelines on using
  961. these methods; generally, the :meth:`.SessionEvents.before_flush`
  962. method should be preferred for general on-flush changes.
  963. :param mapper: the :class:`_orm.Mapper` which is the target
  964. of this event.
  965. :param connection: the :class:`_engine.Connection` being used to
  966. emit INSERT statements for this instance. This
  967. provides a handle into the current transaction on the
  968. target database specific to this instance.
  969. :param target: the mapped instance being persisted. If
  970. the event is configured with ``raw=True``, this will
  971. instead be the :class:`.InstanceState` state-management
  972. object associated with the instance.
  973. :return: No return value is supported by this event.
  974. .. seealso::
  975. :ref:`session_persistence_events`
  976. """
  977. def after_insert(
  978. self, mapper: Mapper[_O], connection: Connection, target: _O
  979. ) -> None:
  980. """Receive an object instance after an INSERT statement
  981. is emitted corresponding to that instance.
  982. .. note:: this event **only** applies to the
  983. :ref:`session flush operation <session_flushing>`
  984. and does **not** apply to the ORM DML operations described at
  985. :ref:`orm_expression_update_delete`. To intercept ORM
  986. DML events, use :meth:`_orm.SessionEvents.do_orm_execute`.
  987. This event is used to modify in-Python-only
  988. state on the instance after an INSERT occurs, as well
  989. as to emit additional SQL statements on the given
  990. connection.
  991. The event is often called for a batch of objects of the
  992. same class after their INSERT statements have been
  993. emitted at once in a previous step. In the extremely
  994. rare case that this is not desirable, the
  995. :class:`_orm.Mapper` object can be configured with ``batch=False``,
  996. which will cause batches of instances to be broken up
  997. into individual (and more poorly performing)
  998. event->persist->event steps.
  999. .. warning::
  1000. Mapper-level flush events only allow **very limited operations**,
  1001. on attributes local to the row being operated upon only,
  1002. as well as allowing any SQL to be emitted on the given
  1003. :class:`_engine.Connection`. **Please read fully** the notes
  1004. at :ref:`session_persistence_mapper` for guidelines on using
  1005. these methods; generally, the :meth:`.SessionEvents.before_flush`
  1006. method should be preferred for general on-flush changes.
  1007. :param mapper: the :class:`_orm.Mapper` which is the target
  1008. of this event.
  1009. :param connection: the :class:`_engine.Connection` being used to
  1010. emit INSERT statements for this instance. This
  1011. provides a handle into the current transaction on the
  1012. target database specific to this instance.
  1013. :param target: the mapped instance being persisted. If
  1014. the event is configured with ``raw=True``, this will
  1015. instead be the :class:`.InstanceState` state-management
  1016. object associated with the instance.
  1017. :return: No return value is supported by this event.
  1018. .. seealso::
  1019. :ref:`session_persistence_events`
  1020. """
  1021. def before_update(
  1022. self, mapper: Mapper[_O], connection: Connection, target: _O
  1023. ) -> None:
  1024. """Receive an object instance before an UPDATE statement
  1025. is emitted corresponding to that instance.
  1026. .. note:: this event **only** applies to the
  1027. :ref:`session flush operation <session_flushing>`
  1028. and does **not** apply to the ORM DML operations described at
  1029. :ref:`orm_expression_update_delete`. To intercept ORM
  1030. DML events, use :meth:`_orm.SessionEvents.do_orm_execute`.
  1031. This event is used to modify local, non-object related
  1032. attributes on the instance before an UPDATE occurs, as well
  1033. as to emit additional SQL statements on the given
  1034. connection.
  1035. This method is called for all instances that are
  1036. marked as "dirty", *even those which have no net changes
  1037. to their column-based attributes*. An object is marked
  1038. as dirty when any of its column-based attributes have a
  1039. "set attribute" operation called or when any of its
  1040. collections are modified. If, at update time, no
  1041. column-based attributes have any net changes, no UPDATE
  1042. statement will be issued. This means that an instance
  1043. being sent to :meth:`~.MapperEvents.before_update` is
  1044. *not* a guarantee that an UPDATE statement will be
  1045. issued, although you can affect the outcome here by
  1046. modifying attributes so that a net change in value does
  1047. exist.
  1048. To detect if the column-based attributes on the object have net
  1049. changes, and will therefore generate an UPDATE statement, use
  1050. ``object_session(instance).is_modified(instance,
  1051. include_collections=False)``.
  1052. The event is often called for a batch of objects of the
  1053. same class before their UPDATE statements are emitted at
  1054. once in a later step. In the extremely rare case that
  1055. this is not desirable, the :class:`_orm.Mapper` can be
  1056. configured with ``batch=False``, which will cause
  1057. batches of instances to be broken up into individual
  1058. (and more poorly performing) event->persist->event
  1059. steps.
  1060. .. warning::
  1061. Mapper-level flush events only allow **very limited operations**,
  1062. on attributes local to the row being operated upon only,
  1063. as well as allowing any SQL to be emitted on the given
  1064. :class:`_engine.Connection`. **Please read fully** the notes
  1065. at :ref:`session_persistence_mapper` for guidelines on using
  1066. these methods; generally, the :meth:`.SessionEvents.before_flush`
  1067. method should be preferred for general on-flush changes.
  1068. :param mapper: the :class:`_orm.Mapper` which is the target
  1069. of this event.
  1070. :param connection: the :class:`_engine.Connection` being used to
  1071. emit UPDATE statements for this instance. This
  1072. provides a handle into the current transaction on the
  1073. target database specific to this instance.
  1074. :param target: the mapped instance being persisted. If
  1075. the event is configured with ``raw=True``, this will
  1076. instead be the :class:`.InstanceState` state-management
  1077. object associated with the instance.
  1078. :return: No return value is supported by this event.
  1079. .. seealso::
  1080. :ref:`session_persistence_events`
  1081. """
  1082. def after_update(
  1083. self, mapper: Mapper[_O], connection: Connection, target: _O
  1084. ) -> None:
  1085. """Receive an object instance after an UPDATE statement
  1086. is emitted corresponding to that instance.
  1087. .. note:: this event **only** applies to the
  1088. :ref:`session flush operation <session_flushing>`
  1089. and does **not** apply to the ORM DML operations described at
  1090. :ref:`orm_expression_update_delete`. To intercept ORM
  1091. DML events, use :meth:`_orm.SessionEvents.do_orm_execute`.
  1092. This event is used to modify in-Python-only
  1093. state on the instance after an UPDATE occurs, as well
  1094. as to emit additional SQL statements on the given
  1095. connection.
  1096. This method is called for all instances that are
  1097. marked as "dirty", *even those which have no net changes
  1098. to their column-based attributes*, and for which
  1099. no UPDATE statement has proceeded. An object is marked
  1100. as dirty when any of its column-based attributes have a
  1101. "set attribute" operation called or when any of its
  1102. collections are modified. If, at update time, no
  1103. column-based attributes have any net changes, no UPDATE
  1104. statement will be issued. This means that an instance
  1105. being sent to :meth:`~.MapperEvents.after_update` is
  1106. *not* a guarantee that an UPDATE statement has been
  1107. issued.
  1108. To detect if the column-based attributes on the object have net
  1109. changes, and therefore resulted in an UPDATE statement, use
  1110. ``object_session(instance).is_modified(instance,
  1111. include_collections=False)``.
  1112. The event is often called for a batch of objects of the
  1113. same class after their UPDATE statements have been emitted at
  1114. once in a previous step. In the extremely rare case that
  1115. this is not desirable, the :class:`_orm.Mapper` can be
  1116. configured with ``batch=False``, which will cause
  1117. batches of instances to be broken up into individual
  1118. (and more poorly performing) event->persist->event
  1119. steps.
  1120. .. warning::
  1121. Mapper-level flush events only allow **very limited operations**,
  1122. on attributes local to the row being operated upon only,
  1123. as well as allowing any SQL to be emitted on the given
  1124. :class:`_engine.Connection`. **Please read fully** the notes
  1125. at :ref:`session_persistence_mapper` for guidelines on using
  1126. these methods; generally, the :meth:`.SessionEvents.before_flush`
  1127. method should be preferred for general on-flush changes.
  1128. :param mapper: the :class:`_orm.Mapper` which is the target
  1129. of this event.
  1130. :param connection: the :class:`_engine.Connection` being used to
  1131. emit UPDATE statements for this instance. This
  1132. provides a handle into the current transaction on the
  1133. target database specific to this instance.
  1134. :param target: the mapped instance being persisted. If
  1135. the event is configured with ``raw=True``, this will
  1136. instead be the :class:`.InstanceState` state-management
  1137. object associated with the instance.
  1138. :return: No return value is supported by this event.
  1139. .. seealso::
  1140. :ref:`session_persistence_events`
  1141. """
  1142. def before_delete(
  1143. self, mapper: Mapper[_O], connection: Connection, target: _O
  1144. ) -> None:
  1145. """Receive an object instance before a DELETE statement
  1146. is emitted corresponding to that instance.
  1147. .. note:: this event **only** applies to the
  1148. :ref:`session flush operation <session_flushing>`
  1149. and does **not** apply to the ORM DML operations described at
  1150. :ref:`orm_expression_update_delete`. To intercept ORM
  1151. DML events, use :meth:`_orm.SessionEvents.do_orm_execute`.
  1152. This event is used to emit additional SQL statements on
  1153. the given connection as well as to perform application
  1154. specific bookkeeping related to a deletion event.
  1155. The event is often called for a batch of objects of the
  1156. same class before their DELETE statements are emitted at
  1157. once in a later step.
  1158. .. warning::
  1159. Mapper-level flush events only allow **very limited operations**,
  1160. on attributes local to the row being operated upon only,
  1161. as well as allowing any SQL to be emitted on the given
  1162. :class:`_engine.Connection`. **Please read fully** the notes
  1163. at :ref:`session_persistence_mapper` for guidelines on using
  1164. these methods; generally, the :meth:`.SessionEvents.before_flush`
  1165. method should be preferred for general on-flush changes.
  1166. :param mapper: the :class:`_orm.Mapper` which is the target
  1167. of this event.
  1168. :param connection: the :class:`_engine.Connection` being used to
  1169. emit DELETE statements for this instance. This
  1170. provides a handle into the current transaction on the
  1171. target database specific to this instance.
  1172. :param target: the mapped instance being deleted. If
  1173. the event is configured with ``raw=True``, this will
  1174. instead be the :class:`.InstanceState` state-management
  1175. object associated with the instance.
  1176. :return: No return value is supported by this event.
  1177. .. seealso::
  1178. :ref:`session_persistence_events`
  1179. """
  1180. def after_delete(
  1181. self, mapper: Mapper[_O], connection: Connection, target: _O
  1182. ) -> None:
  1183. """Receive an object instance after a DELETE statement
  1184. has been emitted corresponding to that instance.
  1185. .. note:: this event **only** applies to the
  1186. :ref:`session flush operation <session_flushing>`
  1187. and does **not** apply to the ORM DML operations described at
  1188. :ref:`orm_expression_update_delete`. To intercept ORM
  1189. DML events, use :meth:`_orm.SessionEvents.do_orm_execute`.
  1190. This event is used to emit additional SQL statements on
  1191. the given connection as well as to perform application
  1192. specific bookkeeping related to a deletion event.
  1193. The event is often called for a batch of objects of the
  1194. same class after their DELETE statements have been emitted at
  1195. once in a previous step.
  1196. .. warning::
  1197. Mapper-level flush events only allow **very limited operations**,
  1198. on attributes local to the row being operated upon only,
  1199. as well as allowing any SQL to be emitted on the given
  1200. :class:`_engine.Connection`. **Please read fully** the notes
  1201. at :ref:`session_persistence_mapper` for guidelines on using
  1202. these methods; generally, the :meth:`.SessionEvents.before_flush`
  1203. method should be preferred for general on-flush changes.
  1204. :param mapper: the :class:`_orm.Mapper` which is the target
  1205. of this event.
  1206. :param connection: the :class:`_engine.Connection` being used to
  1207. emit DELETE statements for this instance. This
  1208. provides a handle into the current transaction on the
  1209. target database specific to this instance.
  1210. :param target: the mapped instance being deleted. If
  1211. the event is configured with ``raw=True``, this will
  1212. instead be the :class:`.InstanceState` state-management
  1213. object associated with the instance.
  1214. :return: No return value is supported by this event.
  1215. .. seealso::
  1216. :ref:`session_persistence_events`
  1217. """
  1218. class _MapperEventsHold(_EventsHold[_ET]):
  1219. all_holds = weakref.WeakKeyDictionary()
  1220. def resolve(
  1221. self, class_: Union[Type[_T], _InternalEntityType[_T]]
  1222. ) -> Optional[Mapper[_T]]:
  1223. return _mapper_or_none(class_)
  1224. class HoldMapperEvents(_EventsHold.HoldEvents[_ET], MapperEvents): # type: ignore [misc] # noqa: E501
  1225. pass
  1226. dispatch = event.dispatcher(HoldMapperEvents)
  1227. _sessionevents_lifecycle_event_names: Set[str] = set()
  1228. class SessionEvents(event.Events[Session]):
  1229. """Define events specific to :class:`.Session` lifecycle.
  1230. e.g.::
  1231. from sqlalchemy import event
  1232. from sqlalchemy.orm import sessionmaker
  1233. def my_before_commit(session):
  1234. print("before commit!")
  1235. Session = sessionmaker()
  1236. event.listen(Session, "before_commit", my_before_commit)
  1237. The :func:`~.event.listen` function will accept
  1238. :class:`.Session` objects as well as the return result
  1239. of :class:`~.sessionmaker()` and :class:`~.scoped_session()`.
  1240. Additionally, it accepts the :class:`.Session` class which
  1241. will apply listeners to all :class:`.Session` instances
  1242. globally.
  1243. :param raw=False: When True, the "target" argument passed
  1244. to applicable event listener functions that work on individual
  1245. objects will be the instance's :class:`.InstanceState` management
  1246. object, rather than the mapped instance itself.
  1247. .. versionadded:: 1.3.14
  1248. :param restore_load_context=False: Applies to the
  1249. :meth:`.SessionEvents.loaded_as_persistent` event. Restores the loader
  1250. context of the object when the event hook is complete, so that ongoing
  1251. eager load operations continue to target the object appropriately. A
  1252. warning is emitted if the object is moved to a new loader context from
  1253. within this event if this flag is not set.
  1254. .. versionadded:: 1.3.14
  1255. """
  1256. _target_class_doc = "SomeSessionClassOrObject"
  1257. _dispatch_target = Session
  1258. def _lifecycle_event( # type: ignore [misc]
  1259. fn: Callable[[SessionEvents, Session, Any], None],
  1260. ) -> Callable[[SessionEvents, Session, Any], None]:
  1261. _sessionevents_lifecycle_event_names.add(fn.__name__)
  1262. return fn
  1263. @classmethod
  1264. def _accept_with( # type: ignore [return]
  1265. cls, target: Any, identifier: str
  1266. ) -> Union[Session, type]:
  1267. if isinstance(target, scoped_session):
  1268. target = target.session_factory
  1269. if not isinstance(target, sessionmaker) and (
  1270. not isinstance(target, type) or not issubclass(target, Session)
  1271. ):
  1272. raise exc.ArgumentError(
  1273. "Session event listen on a scoped_session "
  1274. "requires that its creation callable "
  1275. "is associated with the Session class."
  1276. )
  1277. if isinstance(target, sessionmaker):
  1278. return target.class_
  1279. elif isinstance(target, type):
  1280. if issubclass(target, scoped_session):
  1281. return Session
  1282. elif issubclass(target, Session):
  1283. return target
  1284. elif isinstance(target, Session):
  1285. return target
  1286. elif hasattr(target, "_no_async_engine_events"):
  1287. target._no_async_engine_events()
  1288. else:
  1289. # allows alternate SessionEvents-like-classes to be consulted
  1290. return event.Events._accept_with(target, identifier) # type: ignore [return-value] # noqa: E501
  1291. @classmethod
  1292. def _listen(
  1293. cls,
  1294. event_key: Any,
  1295. *,
  1296. raw: bool = False,
  1297. restore_load_context: bool = False,
  1298. **kw: Any,
  1299. ) -> None:
  1300. is_instance_event = (
  1301. event_key.identifier in _sessionevents_lifecycle_event_names
  1302. )
  1303. if is_instance_event:
  1304. if not raw or restore_load_context:
  1305. fn = event_key._listen_fn
  1306. def wrap(
  1307. session: Session,
  1308. state: InstanceState[_O],
  1309. *arg: Any,
  1310. **kw: Any,
  1311. ) -> Optional[Any]:
  1312. if not raw:
  1313. target = state.obj()
  1314. if target is None:
  1315. # existing behavior is that if the object is
  1316. # garbage collected, no event is emitted
  1317. return None
  1318. else:
  1319. target = state # type: ignore [assignment]
  1320. if restore_load_context:
  1321. runid = state.runid
  1322. try:
  1323. return fn(session, target, *arg, **kw)
  1324. finally:
  1325. if restore_load_context:
  1326. state.runid = runid
  1327. event_key = event_key.with_wrapper(wrap)
  1328. event_key.base_listen(**kw)
  1329. def do_orm_execute(self, orm_execute_state: ORMExecuteState) -> None:
  1330. """Intercept statement executions that occur on behalf of an
  1331. ORM :class:`.Session` object.
  1332. This event is invoked for all top-level SQL statements invoked from the
  1333. :meth:`_orm.Session.execute` method, as well as related methods such as
  1334. :meth:`_orm.Session.scalars` and :meth:`_orm.Session.scalar`. As of
  1335. SQLAlchemy 1.4, all ORM queries that run through the
  1336. :meth:`_orm.Session.execute` method as well as related methods
  1337. :meth:`_orm.Session.scalars`, :meth:`_orm.Session.scalar` etc.
  1338. will participate in this event.
  1339. This event hook does **not** apply to the queries that are
  1340. emitted internally within the ORM flush process, i.e. the
  1341. process described at :ref:`session_flushing`.
  1342. .. note:: The :meth:`_orm.SessionEvents.do_orm_execute` event hook
  1343. is triggered **for ORM statement executions only**, meaning those
  1344. invoked via the :meth:`_orm.Session.execute` and similar methods on
  1345. the :class:`_orm.Session` object. It does **not** trigger for
  1346. statements that are invoked by SQLAlchemy Core only, i.e. statements
  1347. invoked directly using :meth:`_engine.Connection.execute` or
  1348. otherwise originating from an :class:`_engine.Engine` object without
  1349. any :class:`_orm.Session` involved. To intercept **all** SQL
  1350. executions regardless of whether the Core or ORM APIs are in use,
  1351. see the event hooks at :class:`.ConnectionEvents`, such as
  1352. :meth:`.ConnectionEvents.before_execute` and
  1353. :meth:`.ConnectionEvents.before_cursor_execute`.
  1354. Also, this event hook does **not** apply to queries that are
  1355. emitted internally within the ORM flush process,
  1356. i.e. the process described at :ref:`session_flushing`; to
  1357. intercept steps within the flush process, see the event
  1358. hooks described at :ref:`session_persistence_events` as
  1359. well as :ref:`session_persistence_mapper`.
  1360. This event is a ``do_`` event, meaning it has the capability to replace
  1361. the operation that the :meth:`_orm.Session.execute` method normally
  1362. performs. The intended use for this includes sharding and
  1363. result-caching schemes which may seek to invoke the same statement
  1364. across multiple database connections, returning a result that is
  1365. merged from each of them, or which don't invoke the statement at all,
  1366. instead returning data from a cache.
  1367. The hook intends to replace the use of the
  1368. ``Query._execute_and_instances`` method that could be subclassed prior
  1369. to SQLAlchemy 1.4.
  1370. :param orm_execute_state: an instance of :class:`.ORMExecuteState`
  1371. which contains all information about the current execution, as well
  1372. as helper functions used to derive other commonly required
  1373. information. See that object for details.
  1374. .. seealso::
  1375. :ref:`session_execute_events` - top level documentation on how
  1376. to use :meth:`_orm.SessionEvents.do_orm_execute`
  1377. :class:`.ORMExecuteState` - the object passed to the
  1378. :meth:`_orm.SessionEvents.do_orm_execute` event which contains
  1379. all information about the statement to be invoked. It also
  1380. provides an interface to extend the current statement, options,
  1381. and parameters as well as an option that allows programmatic
  1382. invocation of the statement at any point.
  1383. :ref:`examples_session_orm_events` - includes examples of using
  1384. :meth:`_orm.SessionEvents.do_orm_execute`
  1385. :ref:`examples_caching` - an example of how to integrate
  1386. Dogpile caching with the ORM :class:`_orm.Session` making use
  1387. of the :meth:`_orm.SessionEvents.do_orm_execute` event hook.
  1388. :ref:`examples_sharding` - the Horizontal Sharding example /
  1389. extension relies upon the
  1390. :meth:`_orm.SessionEvents.do_orm_execute` event hook to invoke a
  1391. SQL statement on multiple backends and return a merged result.
  1392. .. versionadded:: 1.4
  1393. """
  1394. def after_transaction_create(
  1395. self, session: Session, transaction: SessionTransaction
  1396. ) -> None:
  1397. """Execute when a new :class:`.SessionTransaction` is created.
  1398. This event differs from :meth:`~.SessionEvents.after_begin`
  1399. in that it occurs for each :class:`.SessionTransaction`
  1400. overall, as opposed to when transactions are begun
  1401. on individual database connections. It is also invoked
  1402. for nested transactions and subtransactions, and is always
  1403. matched by a corresponding
  1404. :meth:`~.SessionEvents.after_transaction_end` event
  1405. (assuming normal operation of the :class:`.Session`).
  1406. :param session: the target :class:`.Session`.
  1407. :param transaction: the target :class:`.SessionTransaction`.
  1408. To detect if this is the outermost
  1409. :class:`.SessionTransaction`, as opposed to a "subtransaction" or a
  1410. SAVEPOINT, test that the :attr:`.SessionTransaction.parent` attribute
  1411. is ``None``::
  1412. @event.listens_for(session, "after_transaction_create")
  1413. def after_transaction_create(session, transaction):
  1414. if transaction.parent is None:
  1415. ... # work with top-level transaction
  1416. To detect if the :class:`.SessionTransaction` is a SAVEPOINT, use the
  1417. :attr:`.SessionTransaction.nested` attribute::
  1418. @event.listens_for(session, "after_transaction_create")
  1419. def after_transaction_create(session, transaction):
  1420. if transaction.nested:
  1421. ... # work with SAVEPOINT transaction
  1422. .. seealso::
  1423. :class:`.SessionTransaction`
  1424. :meth:`~.SessionEvents.after_transaction_end`
  1425. """
  1426. def after_transaction_end(
  1427. self, session: Session, transaction: SessionTransaction
  1428. ) -> None:
  1429. """Execute when the span of a :class:`.SessionTransaction` ends.
  1430. This event differs from :meth:`~.SessionEvents.after_commit`
  1431. in that it corresponds to all :class:`.SessionTransaction`
  1432. objects in use, including those for nested transactions
  1433. and subtransactions, and is always matched by a corresponding
  1434. :meth:`~.SessionEvents.after_transaction_create` event.
  1435. :param session: the target :class:`.Session`.
  1436. :param transaction: the target :class:`.SessionTransaction`.
  1437. To detect if this is the outermost
  1438. :class:`.SessionTransaction`, as opposed to a "subtransaction" or a
  1439. SAVEPOINT, test that the :attr:`.SessionTransaction.parent` attribute
  1440. is ``None``::
  1441. @event.listens_for(session, "after_transaction_create")
  1442. def after_transaction_end(session, transaction):
  1443. if transaction.parent is None:
  1444. ... # work with top-level transaction
  1445. To detect if the :class:`.SessionTransaction` is a SAVEPOINT, use the
  1446. :attr:`.SessionTransaction.nested` attribute::
  1447. @event.listens_for(session, "after_transaction_create")
  1448. def after_transaction_end(session, transaction):
  1449. if transaction.nested:
  1450. ... # work with SAVEPOINT transaction
  1451. .. seealso::
  1452. :class:`.SessionTransaction`
  1453. :meth:`~.SessionEvents.after_transaction_create`
  1454. """
  1455. def before_commit(self, session: Session) -> None:
  1456. """Execute before commit is called.
  1457. .. note::
  1458. The :meth:`~.SessionEvents.before_commit` hook is *not* per-flush,
  1459. that is, the :class:`.Session` can emit SQL to the database
  1460. many times within the scope of a transaction.
  1461. For interception of these events, use the
  1462. :meth:`~.SessionEvents.before_flush`,
  1463. :meth:`~.SessionEvents.after_flush`, or
  1464. :meth:`~.SessionEvents.after_flush_postexec`
  1465. events.
  1466. :param session: The target :class:`.Session`.
  1467. .. seealso::
  1468. :meth:`~.SessionEvents.after_commit`
  1469. :meth:`~.SessionEvents.after_begin`
  1470. :meth:`~.SessionEvents.after_transaction_create`
  1471. :meth:`~.SessionEvents.after_transaction_end`
  1472. """
  1473. def after_commit(self, session: Session) -> None:
  1474. """Execute after a commit has occurred.
  1475. .. note::
  1476. The :meth:`~.SessionEvents.after_commit` hook is *not* per-flush,
  1477. that is, the :class:`.Session` can emit SQL to the database
  1478. many times within the scope of a transaction.
  1479. For interception of these events, use the
  1480. :meth:`~.SessionEvents.before_flush`,
  1481. :meth:`~.SessionEvents.after_flush`, or
  1482. :meth:`~.SessionEvents.after_flush_postexec`
  1483. events.
  1484. .. note::
  1485. The :class:`.Session` is not in an active transaction
  1486. when the :meth:`~.SessionEvents.after_commit` event is invoked,
  1487. and therefore can not emit SQL. To emit SQL corresponding to
  1488. every transaction, use the :meth:`~.SessionEvents.before_commit`
  1489. event.
  1490. :param session: The target :class:`.Session`.
  1491. .. seealso::
  1492. :meth:`~.SessionEvents.before_commit`
  1493. :meth:`~.SessionEvents.after_begin`
  1494. :meth:`~.SessionEvents.after_transaction_create`
  1495. :meth:`~.SessionEvents.after_transaction_end`
  1496. """
  1497. def after_rollback(self, session: Session) -> None:
  1498. """Execute after a real DBAPI rollback has occurred.
  1499. Note that this event only fires when the *actual* rollback against
  1500. the database occurs - it does *not* fire each time the
  1501. :meth:`.Session.rollback` method is called, if the underlying
  1502. DBAPI transaction has already been rolled back. In many
  1503. cases, the :class:`.Session` will not be in
  1504. an "active" state during this event, as the current
  1505. transaction is not valid. To acquire a :class:`.Session`
  1506. which is active after the outermost rollback has proceeded,
  1507. use the :meth:`.SessionEvents.after_soft_rollback` event, checking the
  1508. :attr:`.Session.is_active` flag.
  1509. :param session: The target :class:`.Session`.
  1510. """
  1511. def after_soft_rollback(
  1512. self, session: Session, previous_transaction: SessionTransaction
  1513. ) -> None:
  1514. """Execute after any rollback has occurred, including "soft"
  1515. rollbacks that don't actually emit at the DBAPI level.
  1516. This corresponds to both nested and outer rollbacks, i.e.
  1517. the innermost rollback that calls the DBAPI's
  1518. rollback() method, as well as the enclosing rollback
  1519. calls that only pop themselves from the transaction stack.
  1520. The given :class:`.Session` can be used to invoke SQL and
  1521. :meth:`.Session.query` operations after an outermost rollback
  1522. by first checking the :attr:`.Session.is_active` flag::
  1523. @event.listens_for(Session, "after_soft_rollback")
  1524. def do_something(session, previous_transaction):
  1525. if session.is_active:
  1526. session.execute(text("select * from some_table"))
  1527. :param session: The target :class:`.Session`.
  1528. :param previous_transaction: The :class:`.SessionTransaction`
  1529. transactional marker object which was just closed. The current
  1530. :class:`.SessionTransaction` for the given :class:`.Session` is
  1531. available via the :attr:`.Session.transaction` attribute.
  1532. """
  1533. def before_flush(
  1534. self,
  1535. session: Session,
  1536. flush_context: UOWTransaction,
  1537. instances: Optional[Sequence[_O]],
  1538. ) -> None:
  1539. """Execute before flush process has started.
  1540. :param session: The target :class:`.Session`.
  1541. :param flush_context: Internal :class:`.UOWTransaction` object
  1542. which handles the details of the flush.
  1543. :param instances: Usually ``None``, this is the collection of
  1544. objects which can be passed to the :meth:`.Session.flush` method
  1545. (note this usage is deprecated).
  1546. .. seealso::
  1547. :meth:`~.SessionEvents.after_flush`
  1548. :meth:`~.SessionEvents.after_flush_postexec`
  1549. :ref:`session_persistence_events`
  1550. """
  1551. def after_flush(
  1552. self, session: Session, flush_context: UOWTransaction
  1553. ) -> None:
  1554. """Execute after flush has completed, but before commit has been
  1555. called.
  1556. Note that the session's state is still in pre-flush, i.e. 'new',
  1557. 'dirty', and 'deleted' lists still show pre-flush state as well
  1558. as the history settings on instance attributes.
  1559. .. warning:: This event runs after the :class:`.Session` has emitted
  1560. SQL to modify the database, but **before** it has altered its
  1561. internal state to reflect those changes, including that newly
  1562. inserted objects are placed into the identity map. ORM operations
  1563. emitted within this event such as loads of related items
  1564. may produce new identity map entries that will immediately
  1565. be replaced, sometimes causing confusing results. SQLAlchemy will
  1566. emit a warning for this condition as of version 1.3.9.
  1567. :param session: The target :class:`.Session`.
  1568. :param flush_context: Internal :class:`.UOWTransaction` object
  1569. which handles the details of the flush.
  1570. .. seealso::
  1571. :meth:`~.SessionEvents.before_flush`
  1572. :meth:`~.SessionEvents.after_flush_postexec`
  1573. :ref:`session_persistence_events`
  1574. """
  1575. def after_flush_postexec(
  1576. self, session: Session, flush_context: UOWTransaction
  1577. ) -> None:
  1578. """Execute after flush has completed, and after the post-exec
  1579. state occurs.
  1580. This will be when the 'new', 'dirty', and 'deleted' lists are in
  1581. their final state. An actual commit() may or may not have
  1582. occurred, depending on whether or not the flush started its own
  1583. transaction or participated in a larger transaction.
  1584. :param session: The target :class:`.Session`.
  1585. :param flush_context: Internal :class:`.UOWTransaction` object
  1586. which handles the details of the flush.
  1587. .. seealso::
  1588. :meth:`~.SessionEvents.before_flush`
  1589. :meth:`~.SessionEvents.after_flush`
  1590. :ref:`session_persistence_events`
  1591. """
  1592. def after_begin(
  1593. self,
  1594. session: Session,
  1595. transaction: SessionTransaction,
  1596. connection: Connection,
  1597. ) -> None:
  1598. """Execute after a transaction is begun on a connection.
  1599. .. note:: This event is called within the process of the
  1600. :class:`_orm.Session` modifying its own internal state.
  1601. To invoke SQL operations within this hook, use the
  1602. :class:`_engine.Connection` provided to the event;
  1603. do not run SQL operations using the :class:`_orm.Session`
  1604. directly.
  1605. :param session: The target :class:`.Session`.
  1606. :param transaction: The :class:`.SessionTransaction`.
  1607. :param connection: The :class:`_engine.Connection` object
  1608. which will be used for SQL statements.
  1609. .. seealso::
  1610. :meth:`~.SessionEvents.before_commit`
  1611. :meth:`~.SessionEvents.after_commit`
  1612. :meth:`~.SessionEvents.after_transaction_create`
  1613. :meth:`~.SessionEvents.after_transaction_end`
  1614. """
  1615. @_lifecycle_event
  1616. def before_attach(self, session: Session, instance: _O) -> None:
  1617. """Execute before an instance is attached to a session.
  1618. This is called before an add, delete or merge causes
  1619. the object to be part of the session.
  1620. .. seealso::
  1621. :meth:`~.SessionEvents.after_attach`
  1622. :ref:`session_lifecycle_events`
  1623. """
  1624. @_lifecycle_event
  1625. def after_attach(self, session: Session, instance: _O) -> None:
  1626. """Execute after an instance is attached to a session.
  1627. This is called after an add, delete or merge.
  1628. .. note::
  1629. As of 0.8, this event fires off *after* the item
  1630. has been fully associated with the session, which is
  1631. different than previous releases. For event
  1632. handlers that require the object not yet
  1633. be part of session state (such as handlers which
  1634. may autoflush while the target object is not
  1635. yet complete) consider the
  1636. new :meth:`.before_attach` event.
  1637. .. seealso::
  1638. :meth:`~.SessionEvents.before_attach`
  1639. :ref:`session_lifecycle_events`
  1640. """
  1641. @event._legacy_signature(
  1642. "0.9",
  1643. ["session", "query", "query_context", "result"],
  1644. lambda update_context: (
  1645. update_context.session,
  1646. update_context.query,
  1647. None,
  1648. update_context.result,
  1649. ),
  1650. )
  1651. def after_bulk_update(self, update_context: _O) -> None:
  1652. """Event for after the legacy :meth:`_orm.Query.update` method
  1653. has been called.
  1654. .. legacy:: The :meth:`_orm.SessionEvents.after_bulk_update` method
  1655. is a legacy event hook as of SQLAlchemy 2.0. The event
  1656. **does not participate** in :term:`2.0 style` invocations
  1657. using :func:`_dml.update` documented at
  1658. :ref:`orm_queryguide_update_delete_where`. For 2.0 style use,
  1659. the :meth:`_orm.SessionEvents.do_orm_execute` hook will intercept
  1660. these calls.
  1661. :param update_context: an "update context" object which contains
  1662. details about the update, including these attributes:
  1663. * ``session`` - the :class:`.Session` involved
  1664. * ``query`` -the :class:`_query.Query`
  1665. object that this update operation
  1666. was called upon.
  1667. * ``values`` The "values" dictionary that was passed to
  1668. :meth:`_query.Query.update`.
  1669. * ``result`` the :class:`_engine.CursorResult`
  1670. returned as a result of the
  1671. bulk UPDATE operation.
  1672. .. versionchanged:: 1.4 the update_context no longer has a
  1673. ``QueryContext`` object associated with it.
  1674. .. seealso::
  1675. :meth:`.QueryEvents.before_compile_update`
  1676. :meth:`.SessionEvents.after_bulk_delete`
  1677. """
  1678. @event._legacy_signature(
  1679. "0.9",
  1680. ["session", "query", "query_context", "result"],
  1681. lambda delete_context: (
  1682. delete_context.session,
  1683. delete_context.query,
  1684. None,
  1685. delete_context.result,
  1686. ),
  1687. )
  1688. def after_bulk_delete(self, delete_context: _O) -> None:
  1689. """Event for after the legacy :meth:`_orm.Query.delete` method
  1690. has been called.
  1691. .. legacy:: The :meth:`_orm.SessionEvents.after_bulk_delete` method
  1692. is a legacy event hook as of SQLAlchemy 2.0. The event
  1693. **does not participate** in :term:`2.0 style` invocations
  1694. using :func:`_dml.delete` documented at
  1695. :ref:`orm_queryguide_update_delete_where`. For 2.0 style use,
  1696. the :meth:`_orm.SessionEvents.do_orm_execute` hook will intercept
  1697. these calls.
  1698. :param delete_context: a "delete context" object which contains
  1699. details about the update, including these attributes:
  1700. * ``session`` - the :class:`.Session` involved
  1701. * ``query`` -the :class:`_query.Query`
  1702. object that this update operation
  1703. was called upon.
  1704. * ``result`` the :class:`_engine.CursorResult`
  1705. returned as a result of the
  1706. bulk DELETE operation.
  1707. .. versionchanged:: 1.4 the update_context no longer has a
  1708. ``QueryContext`` object associated with it.
  1709. .. seealso::
  1710. :meth:`.QueryEvents.before_compile_delete`
  1711. :meth:`.SessionEvents.after_bulk_update`
  1712. """
  1713. @_lifecycle_event
  1714. def transient_to_pending(self, session: Session, instance: _O) -> None:
  1715. """Intercept the "transient to pending" transition for a specific
  1716. object.
  1717. This event is a specialization of the
  1718. :meth:`.SessionEvents.after_attach` event which is only invoked
  1719. for this specific transition. It is invoked typically during the
  1720. :meth:`.Session.add` call.
  1721. :param session: target :class:`.Session`
  1722. :param instance: the ORM-mapped instance being operated upon.
  1723. .. seealso::
  1724. :ref:`session_lifecycle_events`
  1725. """
  1726. @_lifecycle_event
  1727. def pending_to_transient(self, session: Session, instance: _O) -> None:
  1728. """Intercept the "pending to transient" transition for a specific
  1729. object.
  1730. This less common transition occurs when an pending object that has
  1731. not been flushed is evicted from the session; this can occur
  1732. when the :meth:`.Session.rollback` method rolls back the transaction,
  1733. or when the :meth:`.Session.expunge` method is used.
  1734. :param session: target :class:`.Session`
  1735. :param instance: the ORM-mapped instance being operated upon.
  1736. .. seealso::
  1737. :ref:`session_lifecycle_events`
  1738. """
  1739. @_lifecycle_event
  1740. def persistent_to_transient(self, session: Session, instance: _O) -> None:
  1741. """Intercept the "persistent to transient" transition for a specific
  1742. object.
  1743. This less common transition occurs when an pending object that has
  1744. has been flushed is evicted from the session; this can occur
  1745. when the :meth:`.Session.rollback` method rolls back the transaction.
  1746. :param session: target :class:`.Session`
  1747. :param instance: the ORM-mapped instance being operated upon.
  1748. .. seealso::
  1749. :ref:`session_lifecycle_events`
  1750. """
  1751. @_lifecycle_event
  1752. def pending_to_persistent(self, session: Session, instance: _O) -> None:
  1753. """Intercept the "pending to persistent"" transition for a specific
  1754. object.
  1755. This event is invoked within the flush process, and is
  1756. similar to scanning the :attr:`.Session.new` collection within
  1757. the :meth:`.SessionEvents.after_flush` event. However, in this
  1758. case the object has already been moved to the persistent state
  1759. when the event is called.
  1760. :param session: target :class:`.Session`
  1761. :param instance: the ORM-mapped instance being operated upon.
  1762. .. seealso::
  1763. :ref:`session_lifecycle_events`
  1764. """
  1765. @_lifecycle_event
  1766. def detached_to_persistent(self, session: Session, instance: _O) -> None:
  1767. """Intercept the "detached to persistent" transition for a specific
  1768. object.
  1769. This event is a specialization of the
  1770. :meth:`.SessionEvents.after_attach` event which is only invoked
  1771. for this specific transition. It is invoked typically during the
  1772. :meth:`.Session.add` call, as well as during the
  1773. :meth:`.Session.delete` call if the object was not previously
  1774. associated with the
  1775. :class:`.Session` (note that an object marked as "deleted" remains
  1776. in the "persistent" state until the flush proceeds).
  1777. .. note::
  1778. If the object becomes persistent as part of a call to
  1779. :meth:`.Session.delete`, the object is **not** yet marked as
  1780. deleted when this event is called. To detect deleted objects,
  1781. check the ``deleted`` flag sent to the
  1782. :meth:`.SessionEvents.persistent_to_detached` to event after the
  1783. flush proceeds, or check the :attr:`.Session.deleted` collection
  1784. within the :meth:`.SessionEvents.before_flush` event if deleted
  1785. objects need to be intercepted before the flush.
  1786. :param session: target :class:`.Session`
  1787. :param instance: the ORM-mapped instance being operated upon.
  1788. .. seealso::
  1789. :ref:`session_lifecycle_events`
  1790. """
  1791. @_lifecycle_event
  1792. def loaded_as_persistent(self, session: Session, instance: _O) -> None:
  1793. """Intercept the "loaded as persistent" transition for a specific
  1794. object.
  1795. This event is invoked within the ORM loading process, and is invoked
  1796. very similarly to the :meth:`.InstanceEvents.load` event. However,
  1797. the event here is linkable to a :class:`.Session` class or instance,
  1798. rather than to a mapper or class hierarchy, and integrates
  1799. with the other session lifecycle events smoothly. The object
  1800. is guaranteed to be present in the session's identity map when
  1801. this event is called.
  1802. .. note:: This event is invoked within the loader process before
  1803. eager loaders may have been completed, and the object's state may
  1804. not be complete. Additionally, invoking row-level refresh
  1805. operations on the object will place the object into a new loader
  1806. context, interfering with the existing load context. See the note
  1807. on :meth:`.InstanceEvents.load` for background on making use of the
  1808. :paramref:`.SessionEvents.restore_load_context` parameter, which
  1809. works in the same manner as that of
  1810. :paramref:`.InstanceEvents.restore_load_context`, in order to
  1811. resolve this scenario.
  1812. :param session: target :class:`.Session`
  1813. :param instance: the ORM-mapped instance being operated upon.
  1814. .. seealso::
  1815. :ref:`session_lifecycle_events`
  1816. """
  1817. @_lifecycle_event
  1818. def persistent_to_deleted(self, session: Session, instance: _O) -> None:
  1819. """Intercept the "persistent to deleted" transition for a specific
  1820. object.
  1821. This event is invoked when a persistent object's identity
  1822. is deleted from the database within a flush, however the object
  1823. still remains associated with the :class:`.Session` until the
  1824. transaction completes.
  1825. If the transaction is rolled back, the object moves again
  1826. to the persistent state, and the
  1827. :meth:`.SessionEvents.deleted_to_persistent` event is called.
  1828. If the transaction is committed, the object becomes detached,
  1829. which will emit the :meth:`.SessionEvents.deleted_to_detached`
  1830. event.
  1831. Note that while the :meth:`.Session.delete` method is the primary
  1832. public interface to mark an object as deleted, many objects
  1833. get deleted due to cascade rules, which are not always determined
  1834. until flush time. Therefore, there's no way to catch
  1835. every object that will be deleted until the flush has proceeded.
  1836. the :meth:`.SessionEvents.persistent_to_deleted` event is therefore
  1837. invoked at the end of a flush.
  1838. .. seealso::
  1839. :ref:`session_lifecycle_events`
  1840. """
  1841. @_lifecycle_event
  1842. def deleted_to_persistent(self, session: Session, instance: _O) -> None:
  1843. """Intercept the "deleted to persistent" transition for a specific
  1844. object.
  1845. This transition occurs only when an object that's been deleted
  1846. successfully in a flush is restored due to a call to
  1847. :meth:`.Session.rollback`. The event is not called under
  1848. any other circumstances.
  1849. .. seealso::
  1850. :ref:`session_lifecycle_events`
  1851. """
  1852. @_lifecycle_event
  1853. def deleted_to_detached(self, session: Session, instance: _O) -> None:
  1854. """Intercept the "deleted to detached" transition for a specific
  1855. object.
  1856. This event is invoked when a deleted object is evicted
  1857. from the session. The typical case when this occurs is when
  1858. the transaction for a :class:`.Session` in which the object
  1859. was deleted is committed; the object moves from the deleted
  1860. state to the detached state.
  1861. It is also invoked for objects that were deleted in a flush
  1862. when the :meth:`.Session.expunge_all` or :meth:`.Session.close`
  1863. events are called, as well as if the object is individually
  1864. expunged from its deleted state via :meth:`.Session.expunge`.
  1865. .. seealso::
  1866. :ref:`session_lifecycle_events`
  1867. """
  1868. @_lifecycle_event
  1869. def persistent_to_detached(self, session: Session, instance: _O) -> None:
  1870. """Intercept the "persistent to detached" transition for a specific
  1871. object.
  1872. This event is invoked when a persistent object is evicted
  1873. from the session. There are many conditions that cause this
  1874. to happen, including:
  1875. * using a method such as :meth:`.Session.expunge`
  1876. or :meth:`.Session.close`
  1877. * Calling the :meth:`.Session.rollback` method, when the object
  1878. was part of an INSERT statement for that session's transaction
  1879. :param session: target :class:`.Session`
  1880. :param instance: the ORM-mapped instance being operated upon.
  1881. :param deleted: boolean. If True, indicates this object moved
  1882. to the detached state because it was marked as deleted and flushed.
  1883. .. seealso::
  1884. :ref:`session_lifecycle_events`
  1885. """
  1886. class AttributeEvents(event.Events[QueryableAttribute[Any]]):
  1887. r"""Define events for object attributes.
  1888. These are typically defined on the class-bound descriptor for the
  1889. target class.
  1890. For example, to register a listener that will receive the
  1891. :meth:`_orm.AttributeEvents.append` event::
  1892. from sqlalchemy import event
  1893. @event.listens_for(MyClass.collection, "append", propagate=True)
  1894. def my_append_listener(target, value, initiator):
  1895. print("received append event for target: %s" % target)
  1896. Listeners have the option to return a possibly modified version of the
  1897. value, when the :paramref:`.AttributeEvents.retval` flag is passed to
  1898. :func:`.event.listen` or :func:`.event.listens_for`, such as below,
  1899. illustrated using the :meth:`_orm.AttributeEvents.set` event::
  1900. def validate_phone(target, value, oldvalue, initiator):
  1901. "Strip non-numeric characters from a phone number"
  1902. return re.sub(r"\D", "", value)
  1903. # setup listener on UserContact.phone attribute, instructing
  1904. # it to use the return value
  1905. listen(UserContact.phone, "set", validate_phone, retval=True)
  1906. A validation function like the above can also raise an exception
  1907. such as :exc:`ValueError` to halt the operation.
  1908. The :paramref:`.AttributeEvents.propagate` flag is also important when
  1909. applying listeners to mapped classes that also have mapped subclasses,
  1910. as when using mapper inheritance patterns::
  1911. @event.listens_for(MySuperClass.attr, "set", propagate=True)
  1912. def receive_set(target, value, initiator):
  1913. print("value set: %s" % target)
  1914. The full list of modifiers available to the :func:`.event.listen`
  1915. and :func:`.event.listens_for` functions are below.
  1916. :param active_history=False: When True, indicates that the
  1917. "set" event would like to receive the "old" value being
  1918. replaced unconditionally, even if this requires firing off
  1919. database loads. Note that ``active_history`` can also be
  1920. set directly via :func:`.column_property` and
  1921. :func:`_orm.relationship`.
  1922. :param propagate=False: When True, the listener function will
  1923. be established not just for the class attribute given, but
  1924. for attributes of the same name on all current subclasses
  1925. of that class, as well as all future subclasses of that
  1926. class, using an additional listener that listens for
  1927. instrumentation events.
  1928. :param raw=False: When True, the "target" argument to the
  1929. event will be the :class:`.InstanceState` management
  1930. object, rather than the mapped instance itself.
  1931. :param retval=False: when True, the user-defined event
  1932. listening must return the "value" argument from the
  1933. function. This gives the listening function the opportunity
  1934. to change the value that is ultimately used for a "set"
  1935. or "append" event.
  1936. """
  1937. _target_class_doc = "SomeClass.some_attribute"
  1938. _dispatch_target = QueryableAttribute
  1939. @staticmethod
  1940. def _set_dispatch(
  1941. cls: Type[_HasEventsDispatch[Any]], dispatch_cls: Type[_Dispatch[Any]]
  1942. ) -> _Dispatch[Any]:
  1943. dispatch = event.Events._set_dispatch(cls, dispatch_cls)
  1944. dispatch_cls._active_history = False
  1945. return dispatch
  1946. @classmethod
  1947. def _accept_with(
  1948. cls,
  1949. target: Union[QueryableAttribute[Any], Type[QueryableAttribute[Any]]],
  1950. identifier: str,
  1951. ) -> Union[QueryableAttribute[Any], Type[QueryableAttribute[Any]]]:
  1952. # TODO: coverage
  1953. if isinstance(target, interfaces.MapperProperty):
  1954. return getattr(target.parent.class_, target.key)
  1955. else:
  1956. return target
  1957. @classmethod
  1958. def _listen( # type: ignore [override]
  1959. cls,
  1960. event_key: _EventKey[QueryableAttribute[Any]],
  1961. active_history: bool = False,
  1962. raw: bool = False,
  1963. retval: bool = False,
  1964. propagate: bool = False,
  1965. include_key: bool = False,
  1966. ) -> None:
  1967. target, fn = event_key.dispatch_target, event_key._listen_fn
  1968. if active_history:
  1969. target.dispatch._active_history = True
  1970. if not raw or not retval or not include_key:
  1971. def wrap(target: InstanceState[_O], *arg: Any, **kw: Any) -> Any:
  1972. if not raw:
  1973. target = target.obj() # type: ignore [assignment]
  1974. if not retval:
  1975. if arg:
  1976. value = arg[0]
  1977. else:
  1978. value = None
  1979. if include_key:
  1980. fn(target, *arg, **kw)
  1981. else:
  1982. fn(target, *arg)
  1983. return value
  1984. else:
  1985. if include_key:
  1986. return fn(target, *arg, **kw)
  1987. else:
  1988. return fn(target, *arg)
  1989. event_key = event_key.with_wrapper(wrap)
  1990. event_key.base_listen(propagate=propagate)
  1991. if propagate:
  1992. manager = instrumentation.manager_of_class(target.class_)
  1993. for mgr in manager.subclass_managers(True): # type: ignore [no-untyped-call] # noqa: E501
  1994. event_key.with_dispatch_target(mgr[target.key]).base_listen(
  1995. propagate=True
  1996. )
  1997. if active_history:
  1998. mgr[target.key].dispatch._active_history = True
  1999. def append(
  2000. self,
  2001. target: _O,
  2002. value: _T,
  2003. initiator: Event,
  2004. *,
  2005. key: EventConstants = NO_KEY,
  2006. ) -> Optional[_T]:
  2007. """Receive a collection append event.
  2008. The append event is invoked for each element as it is appended
  2009. to the collection. This occurs for single-item appends as well
  2010. as for a "bulk replace" operation.
  2011. :param target: the object instance receiving the event.
  2012. If the listener is registered with ``raw=True``, this will
  2013. be the :class:`.InstanceState` object.
  2014. :param value: the value being appended. If this listener
  2015. is registered with ``retval=True``, the listener
  2016. function must return this value, or a new value which
  2017. replaces it.
  2018. :param initiator: An instance of :class:`.attributes.Event`
  2019. representing the initiation of the event. May be modified
  2020. from its original value by backref handlers in order to control
  2021. chained event propagation, as well as be inspected for information
  2022. about the source of the event.
  2023. :param key: When the event is established using the
  2024. :paramref:`.AttributeEvents.include_key` parameter set to
  2025. True, this will be the key used in the operation, such as
  2026. ``collection[some_key_or_index] = value``.
  2027. The parameter is not passed
  2028. to the event at all if the the
  2029. :paramref:`.AttributeEvents.include_key`
  2030. was not used to set up the event; this is to allow backwards
  2031. compatibility with existing event handlers that don't include the
  2032. ``key`` parameter.
  2033. .. versionadded:: 2.0
  2034. :return: if the event was registered with ``retval=True``,
  2035. the given value, or a new effective value, should be returned.
  2036. .. seealso::
  2037. :class:`.AttributeEvents` - background on listener options such
  2038. as propagation to subclasses.
  2039. :meth:`.AttributeEvents.bulk_replace`
  2040. """
  2041. def append_wo_mutation(
  2042. self,
  2043. target: _O,
  2044. value: _T,
  2045. initiator: Event,
  2046. *,
  2047. key: EventConstants = NO_KEY,
  2048. ) -> None:
  2049. """Receive a collection append event where the collection was not
  2050. actually mutated.
  2051. This event differs from :meth:`_orm.AttributeEvents.append` in that
  2052. it is fired off for de-duplicating collections such as sets and
  2053. dictionaries, when the object already exists in the target collection.
  2054. The event does not have a return value and the identity of the
  2055. given object cannot be changed.
  2056. The event is used for cascading objects into a :class:`_orm.Session`
  2057. when the collection has already been mutated via a backref event.
  2058. :param target: the object instance receiving the event.
  2059. If the listener is registered with ``raw=True``, this will
  2060. be the :class:`.InstanceState` object.
  2061. :param value: the value that would be appended if the object did not
  2062. already exist in the collection.
  2063. :param initiator: An instance of :class:`.attributes.Event`
  2064. representing the initiation of the event. May be modified
  2065. from its original value by backref handlers in order to control
  2066. chained event propagation, as well as be inspected for information
  2067. about the source of the event.
  2068. :param key: When the event is established using the
  2069. :paramref:`.AttributeEvents.include_key` parameter set to
  2070. True, this will be the key used in the operation, such as
  2071. ``collection[some_key_or_index] = value``.
  2072. The parameter is not passed
  2073. to the event at all if the the
  2074. :paramref:`.AttributeEvents.include_key`
  2075. was not used to set up the event; this is to allow backwards
  2076. compatibility with existing event handlers that don't include the
  2077. ``key`` parameter.
  2078. .. versionadded:: 2.0
  2079. :return: No return value is defined for this event.
  2080. .. versionadded:: 1.4.15
  2081. """
  2082. def bulk_replace(
  2083. self,
  2084. target: _O,
  2085. values: Iterable[_T],
  2086. initiator: Event,
  2087. *,
  2088. keys: Optional[Iterable[EventConstants]] = None,
  2089. ) -> None:
  2090. """Receive a collection 'bulk replace' event.
  2091. This event is invoked for a sequence of values as they are incoming
  2092. to a bulk collection set operation, which can be
  2093. modified in place before the values are treated as ORM objects.
  2094. This is an "early hook" that runs before the bulk replace routine
  2095. attempts to reconcile which objects are already present in the
  2096. collection and which are being removed by the net replace operation.
  2097. It is typical that this method be combined with use of the
  2098. :meth:`.AttributeEvents.append` event. When using both of these
  2099. events, note that a bulk replace operation will invoke
  2100. the :meth:`.AttributeEvents.append` event for all new items,
  2101. even after :meth:`.AttributeEvents.bulk_replace` has been invoked
  2102. for the collection as a whole. In order to determine if an
  2103. :meth:`.AttributeEvents.append` event is part of a bulk replace,
  2104. use the symbol :attr:`~.attributes.OP_BULK_REPLACE` to test the
  2105. incoming initiator::
  2106. from sqlalchemy.orm.attributes import OP_BULK_REPLACE
  2107. @event.listens_for(SomeObject.collection, "bulk_replace")
  2108. def process_collection(target, values, initiator):
  2109. values[:] = [_make_value(value) for value in values]
  2110. @event.listens_for(SomeObject.collection, "append", retval=True)
  2111. def process_collection(target, value, initiator):
  2112. # make sure bulk_replace didn't already do it
  2113. if initiator is None or initiator.op is not OP_BULK_REPLACE:
  2114. return _make_value(value)
  2115. else:
  2116. return value
  2117. .. versionadded:: 1.2
  2118. :param target: the object instance receiving the event.
  2119. If the listener is registered with ``raw=True``, this will
  2120. be the :class:`.InstanceState` object.
  2121. :param value: a sequence (e.g. a list) of the values being set. The
  2122. handler can modify this list in place.
  2123. :param initiator: An instance of :class:`.attributes.Event`
  2124. representing the initiation of the event.
  2125. :param keys: When the event is established using the
  2126. :paramref:`.AttributeEvents.include_key` parameter set to
  2127. True, this will be the sequence of keys used in the operation,
  2128. typically only for a dictionary update. The parameter is not passed
  2129. to the event at all if the the
  2130. :paramref:`.AttributeEvents.include_key`
  2131. was not used to set up the event; this is to allow backwards
  2132. compatibility with existing event handlers that don't include the
  2133. ``key`` parameter.
  2134. .. versionadded:: 2.0
  2135. .. seealso::
  2136. :class:`.AttributeEvents` - background on listener options such
  2137. as propagation to subclasses.
  2138. """
  2139. def remove(
  2140. self,
  2141. target: _O,
  2142. value: _T,
  2143. initiator: Event,
  2144. *,
  2145. key: EventConstants = NO_KEY,
  2146. ) -> None:
  2147. """Receive a collection remove event.
  2148. :param target: the object instance receiving the event.
  2149. If the listener is registered with ``raw=True``, this will
  2150. be the :class:`.InstanceState` object.
  2151. :param value: the value being removed.
  2152. :param initiator: An instance of :class:`.attributes.Event`
  2153. representing the initiation of the event. May be modified
  2154. from its original value by backref handlers in order to control
  2155. chained event propagation.
  2156. :param key: When the event is established using the
  2157. :paramref:`.AttributeEvents.include_key` parameter set to
  2158. True, this will be the key used in the operation, such as
  2159. ``del collection[some_key_or_index]``. The parameter is not passed
  2160. to the event at all if the the
  2161. :paramref:`.AttributeEvents.include_key`
  2162. was not used to set up the event; this is to allow backwards
  2163. compatibility with existing event handlers that don't include the
  2164. ``key`` parameter.
  2165. .. versionadded:: 2.0
  2166. :return: No return value is defined for this event.
  2167. .. seealso::
  2168. :class:`.AttributeEvents` - background on listener options such
  2169. as propagation to subclasses.
  2170. """
  2171. def set(
  2172. self, target: _O, value: _T, oldvalue: _T, initiator: Event
  2173. ) -> None:
  2174. """Receive a scalar set event.
  2175. :param target: the object instance receiving the event.
  2176. If the listener is registered with ``raw=True``, this will
  2177. be the :class:`.InstanceState` object.
  2178. :param value: the value being set. If this listener
  2179. is registered with ``retval=True``, the listener
  2180. function must return this value, or a new value which
  2181. replaces it.
  2182. :param oldvalue: the previous value being replaced. This
  2183. may also be the symbol ``NEVER_SET`` or ``NO_VALUE``.
  2184. If the listener is registered with ``active_history=True``,
  2185. the previous value of the attribute will be loaded from
  2186. the database if the existing value is currently unloaded
  2187. or expired.
  2188. :param initiator: An instance of :class:`.attributes.Event`
  2189. representing the initiation of the event. May be modified
  2190. from its original value by backref handlers in order to control
  2191. chained event propagation.
  2192. :return: if the event was registered with ``retval=True``,
  2193. the given value, or a new effective value, should be returned.
  2194. .. seealso::
  2195. :class:`.AttributeEvents` - background on listener options such
  2196. as propagation to subclasses.
  2197. """
  2198. def init_scalar(
  2199. self, target: _O, value: _T, dict_: Dict[Any, Any]
  2200. ) -> None:
  2201. r"""Receive a scalar "init" event.
  2202. This event is invoked when an uninitialized, unpersisted scalar
  2203. attribute is accessed, e.g. read::
  2204. x = my_object.some_attribute
  2205. The ORM's default behavior when this occurs for an un-initialized
  2206. attribute is to return the value ``None``; note this differs from
  2207. Python's usual behavior of raising ``AttributeError``. The
  2208. event here can be used to customize what value is actually returned,
  2209. with the assumption that the event listener would be mirroring
  2210. a default generator that is configured on the Core
  2211. :class:`_schema.Column`
  2212. object as well.
  2213. Since a default generator on a :class:`_schema.Column`
  2214. might also produce
  2215. a changing value such as a timestamp, the
  2216. :meth:`.AttributeEvents.init_scalar`
  2217. event handler can also be used to **set** the newly returned value, so
  2218. that a Core-level default generation function effectively fires off
  2219. only once, but at the moment the attribute is accessed on the
  2220. non-persisted object. Normally, no change to the object's state
  2221. is made when an uninitialized attribute is accessed (much older
  2222. SQLAlchemy versions did in fact change the object's state).
  2223. If a default generator on a column returned a particular constant,
  2224. a handler might be used as follows::
  2225. SOME_CONSTANT = 3.1415926
  2226. class MyClass(Base):
  2227. # ...
  2228. some_attribute = Column(Numeric, default=SOME_CONSTANT)
  2229. @event.listens_for(
  2230. MyClass.some_attribute, "init_scalar", retval=True, propagate=True
  2231. )
  2232. def _init_some_attribute(target, dict_, value):
  2233. dict_["some_attribute"] = SOME_CONSTANT
  2234. return SOME_CONSTANT
  2235. Above, we initialize the attribute ``MyClass.some_attribute`` to the
  2236. value of ``SOME_CONSTANT``. The above code includes the following
  2237. features:
  2238. * By setting the value ``SOME_CONSTANT`` in the given ``dict_``,
  2239. we indicate that this value is to be persisted to the database.
  2240. This supersedes the use of ``SOME_CONSTANT`` in the default generator
  2241. for the :class:`_schema.Column`. The ``active_column_defaults.py``
  2242. example given at :ref:`examples_instrumentation` illustrates using
  2243. the same approach for a changing default, e.g. a timestamp
  2244. generator. In this particular example, it is not strictly
  2245. necessary to do this since ``SOME_CONSTANT`` would be part of the
  2246. INSERT statement in either case.
  2247. * By establishing the ``retval=True`` flag, the value we return
  2248. from the function will be returned by the attribute getter.
  2249. Without this flag, the event is assumed to be a passive observer
  2250. and the return value of our function is ignored.
  2251. * The ``propagate=True`` flag is significant if the mapped class
  2252. includes inheriting subclasses, which would also make use of this
  2253. event listener. Without this flag, an inheriting subclass will
  2254. not use our event handler.
  2255. In the above example, the attribute set event
  2256. :meth:`.AttributeEvents.set` as well as the related validation feature
  2257. provided by :obj:`_orm.validates` is **not** invoked when we apply our
  2258. value to the given ``dict_``. To have these events to invoke in
  2259. response to our newly generated value, apply the value to the given
  2260. object as a normal attribute set operation::
  2261. SOME_CONSTANT = 3.1415926
  2262. @event.listens_for(
  2263. MyClass.some_attribute, "init_scalar", retval=True, propagate=True
  2264. )
  2265. def _init_some_attribute(target, dict_, value):
  2266. # will also fire off attribute set events
  2267. target.some_attribute = SOME_CONSTANT
  2268. return SOME_CONSTANT
  2269. When multiple listeners are set up, the generation of the value
  2270. is "chained" from one listener to the next by passing the value
  2271. returned by the previous listener that specifies ``retval=True``
  2272. as the ``value`` argument of the next listener.
  2273. :param target: the object instance receiving the event.
  2274. If the listener is registered with ``raw=True``, this will
  2275. be the :class:`.InstanceState` object.
  2276. :param value: the value that is to be returned before this event
  2277. listener were invoked. This value begins as the value ``None``,
  2278. however will be the return value of the previous event handler
  2279. function if multiple listeners are present.
  2280. :param dict\_: the attribute dictionary of this mapped object.
  2281. This is normally the ``__dict__`` of the object, but in all cases
  2282. represents the destination that the attribute system uses to get
  2283. at the actual value of this attribute. Placing the value in this
  2284. dictionary has the effect that the value will be used in the
  2285. INSERT statement generated by the unit of work.
  2286. .. seealso::
  2287. :meth:`.AttributeEvents.init_collection` - collection version
  2288. of this event
  2289. :class:`.AttributeEvents` - background on listener options such
  2290. as propagation to subclasses.
  2291. :ref:`examples_instrumentation` - see the
  2292. ``active_column_defaults.py`` example.
  2293. """ # noqa: E501
  2294. def init_collection(
  2295. self,
  2296. target: _O,
  2297. collection: Type[Collection[Any]],
  2298. collection_adapter: CollectionAdapter,
  2299. ) -> None:
  2300. """Receive a 'collection init' event.
  2301. This event is triggered for a collection-based attribute, when
  2302. the initial "empty collection" is first generated for a blank
  2303. attribute, as well as for when the collection is replaced with
  2304. a new one, such as via a set event.
  2305. E.g., given that ``User.addresses`` is a relationship-based
  2306. collection, the event is triggered here::
  2307. u1 = User()
  2308. u1.addresses.append(a1) # <- new collection
  2309. and also during replace operations::
  2310. u1.addresses = [a2, a3] # <- new collection
  2311. :param target: the object instance receiving the event.
  2312. If the listener is registered with ``raw=True``, this will
  2313. be the :class:`.InstanceState` object.
  2314. :param collection: the new collection. This will always be generated
  2315. from what was specified as
  2316. :paramref:`_orm.relationship.collection_class`, and will always
  2317. be empty.
  2318. :param collection_adapter: the :class:`.CollectionAdapter` that will
  2319. mediate internal access to the collection.
  2320. .. seealso::
  2321. :class:`.AttributeEvents` - background on listener options such
  2322. as propagation to subclasses.
  2323. :meth:`.AttributeEvents.init_scalar` - "scalar" version of this
  2324. event.
  2325. """
  2326. def dispose_collection(
  2327. self,
  2328. target: _O,
  2329. collection: Collection[Any],
  2330. collection_adapter: CollectionAdapter,
  2331. ) -> None:
  2332. """Receive a 'collection dispose' event.
  2333. This event is triggered for a collection-based attribute when
  2334. a collection is replaced, that is::
  2335. u1.addresses.append(a1)
  2336. u1.addresses = [a2, a3] # <- old collection is disposed
  2337. The old collection received will contain its previous contents.
  2338. .. versionchanged:: 1.2 The collection passed to
  2339. :meth:`.AttributeEvents.dispose_collection` will now have its
  2340. contents before the dispose intact; previously, the collection
  2341. would be empty.
  2342. .. seealso::
  2343. :class:`.AttributeEvents` - background on listener options such
  2344. as propagation to subclasses.
  2345. """
  2346. def modified(self, target: _O, initiator: Event) -> None:
  2347. """Receive a 'modified' event.
  2348. This event is triggered when the :func:`.attributes.flag_modified`
  2349. function is used to trigger a modify event on an attribute without
  2350. any specific value being set.
  2351. .. versionadded:: 1.2
  2352. :param target: the object instance receiving the event.
  2353. If the listener is registered with ``raw=True``, this will
  2354. be the :class:`.InstanceState` object.
  2355. :param initiator: An instance of :class:`.attributes.Event`
  2356. representing the initiation of the event.
  2357. .. seealso::
  2358. :class:`.AttributeEvents` - background on listener options such
  2359. as propagation to subclasses.
  2360. """
  2361. class QueryEvents(event.Events[Query[Any]]):
  2362. """Represent events within the construction of a :class:`_query.Query`
  2363. object.
  2364. .. legacy:: The :class:`_orm.QueryEvents` event methods are legacy
  2365. as of SQLAlchemy 2.0, and only apply to direct use of the
  2366. :class:`_orm.Query` object. They are not used for :term:`2.0 style`
  2367. statements. For events to intercept and modify 2.0 style ORM use,
  2368. use the :meth:`_orm.SessionEvents.do_orm_execute` hook.
  2369. The :class:`_orm.QueryEvents` hooks are now superseded by the
  2370. :meth:`_orm.SessionEvents.do_orm_execute` event hook.
  2371. """
  2372. _target_class_doc = "SomeQuery"
  2373. _dispatch_target = Query
  2374. def before_compile(self, query: Query[Any]) -> None:
  2375. """Receive the :class:`_query.Query`
  2376. object before it is composed into a
  2377. core :class:`_expression.Select` object.
  2378. .. deprecated:: 1.4 The :meth:`_orm.QueryEvents.before_compile` event
  2379. is superseded by the much more capable
  2380. :meth:`_orm.SessionEvents.do_orm_execute` hook. In version 1.4,
  2381. the :meth:`_orm.QueryEvents.before_compile` event is **no longer
  2382. used** for ORM-level attribute loads, such as loads of deferred
  2383. or expired attributes as well as relationship loaders. See the
  2384. new examples in :ref:`examples_session_orm_events` which
  2385. illustrate new ways of intercepting and modifying ORM queries
  2386. for the most common purpose of adding arbitrary filter criteria.
  2387. This event is intended to allow changes to the query given::
  2388. @event.listens_for(Query, "before_compile", retval=True)
  2389. def no_deleted(query):
  2390. for desc in query.column_descriptions:
  2391. if desc["type"] is User:
  2392. entity = desc["entity"]
  2393. query = query.filter(entity.deleted == False)
  2394. return query
  2395. The event should normally be listened with the ``retval=True``
  2396. parameter set, so that the modified query may be returned.
  2397. The :meth:`.QueryEvents.before_compile` event by default
  2398. will disallow "baked" queries from caching a query, if the event
  2399. hook returns a new :class:`_query.Query` object.
  2400. This affects both direct
  2401. use of the baked query extension as well as its operation within
  2402. lazy loaders and eager loaders for relationships. In order to
  2403. re-establish the query being cached, apply the event adding the
  2404. ``bake_ok`` flag::
  2405. @event.listens_for(Query, "before_compile", retval=True, bake_ok=True)
  2406. def my_event(query):
  2407. for desc in query.column_descriptions:
  2408. if desc["type"] is User:
  2409. entity = desc["entity"]
  2410. query = query.filter(entity.deleted == False)
  2411. return query
  2412. When ``bake_ok`` is set to True, the event hook will only be invoked
  2413. once, and not called for subsequent invocations of a particular query
  2414. that is being cached.
  2415. .. versionadded:: 1.3.11 - added the "bake_ok" flag to the
  2416. :meth:`.QueryEvents.before_compile` event and disallowed caching via
  2417. the "baked" extension from occurring for event handlers that
  2418. return a new :class:`_query.Query` object if this flag is not set.
  2419. .. seealso::
  2420. :meth:`.QueryEvents.before_compile_update`
  2421. :meth:`.QueryEvents.before_compile_delete`
  2422. :ref:`baked_with_before_compile`
  2423. """ # noqa: E501
  2424. def before_compile_update(
  2425. self, query: Query[Any], update_context: BulkUpdate
  2426. ) -> None:
  2427. """Allow modifications to the :class:`_query.Query` object within
  2428. :meth:`_query.Query.update`.
  2429. .. deprecated:: 1.4 The :meth:`_orm.QueryEvents.before_compile_update`
  2430. event is superseded by the much more capable
  2431. :meth:`_orm.SessionEvents.do_orm_execute` hook.
  2432. Like the :meth:`.QueryEvents.before_compile` event, if the event
  2433. is to be used to alter the :class:`_query.Query` object, it should
  2434. be configured with ``retval=True``, and the modified
  2435. :class:`_query.Query` object returned, as in ::
  2436. @event.listens_for(Query, "before_compile_update", retval=True)
  2437. def no_deleted(query, update_context):
  2438. for desc in query.column_descriptions:
  2439. if desc["type"] is User:
  2440. entity = desc["entity"]
  2441. query = query.filter(entity.deleted == False)
  2442. update_context.values["timestamp"] = datetime.datetime.now(
  2443. datetime.UTC
  2444. )
  2445. return query
  2446. The ``.values`` dictionary of the "update context" object can also
  2447. be modified in place as illustrated above.
  2448. :param query: a :class:`_query.Query` instance; this is also
  2449. the ``.query`` attribute of the given "update context"
  2450. object.
  2451. :param update_context: an "update context" object which is
  2452. the same kind of object as described in
  2453. :paramref:`.QueryEvents.after_bulk_update.update_context`.
  2454. The object has a ``.values`` attribute in an UPDATE context which is
  2455. the dictionary of parameters passed to :meth:`_query.Query.update`.
  2456. This
  2457. dictionary can be modified to alter the VALUES clause of the
  2458. resulting UPDATE statement.
  2459. .. versionadded:: 1.2.17
  2460. .. seealso::
  2461. :meth:`.QueryEvents.before_compile`
  2462. :meth:`.QueryEvents.before_compile_delete`
  2463. """ # noqa: E501
  2464. def before_compile_delete(
  2465. self, query: Query[Any], delete_context: BulkDelete
  2466. ) -> None:
  2467. """Allow modifications to the :class:`_query.Query` object within
  2468. :meth:`_query.Query.delete`.
  2469. .. deprecated:: 1.4 The :meth:`_orm.QueryEvents.before_compile_delete`
  2470. event is superseded by the much more capable
  2471. :meth:`_orm.SessionEvents.do_orm_execute` hook.
  2472. Like the :meth:`.QueryEvents.before_compile` event, this event
  2473. should be configured with ``retval=True``, and the modified
  2474. :class:`_query.Query` object returned, as in ::
  2475. @event.listens_for(Query, "before_compile_delete", retval=True)
  2476. def no_deleted(query, delete_context):
  2477. for desc in query.column_descriptions:
  2478. if desc["type"] is User:
  2479. entity = desc["entity"]
  2480. query = query.filter(entity.deleted == False)
  2481. return query
  2482. :param query: a :class:`_query.Query` instance; this is also
  2483. the ``.query`` attribute of the given "delete context"
  2484. object.
  2485. :param delete_context: a "delete context" object which is
  2486. the same kind of object as described in
  2487. :paramref:`.QueryEvents.after_bulk_delete.delete_context`.
  2488. .. versionadded:: 1.2.17
  2489. .. seealso::
  2490. :meth:`.QueryEvents.before_compile`
  2491. :meth:`.QueryEvents.before_compile_update`
  2492. """
  2493. @classmethod
  2494. def _listen(
  2495. cls,
  2496. event_key: _EventKey[_ET],
  2497. retval: bool = False,
  2498. bake_ok: bool = False,
  2499. **kw: Any,
  2500. ) -> None:
  2501. fn = event_key._listen_fn
  2502. if not retval:
  2503. def wrap(*arg: Any, **kw: Any) -> Any:
  2504. if not retval:
  2505. query = arg[0]
  2506. fn(*arg, **kw)
  2507. return query
  2508. else:
  2509. return fn(*arg, **kw)
  2510. event_key = event_key.with_wrapper(wrap)
  2511. else:
  2512. # don't assume we can apply an attribute to the callable
  2513. def wrap(*arg: Any, **kw: Any) -> Any:
  2514. return fn(*arg, **kw)
  2515. event_key = event_key.with_wrapper(wrap)
  2516. wrap._bake_ok = bake_ok # type: ignore [attr-defined]
  2517. event_key.base_listen(**kw)