associationproxy.py 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027
  1. # ext/associationproxy.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. """Contain the ``AssociationProxy`` class.
  8. The ``AssociationProxy`` is a Python property object which provides
  9. transparent proxied access to the endpoint of an association object.
  10. See the example ``examples/association/proxied_association.py``.
  11. """
  12. from __future__ import annotations
  13. import operator
  14. import typing
  15. from typing import AbstractSet
  16. from typing import Any
  17. from typing import Callable
  18. from typing import cast
  19. from typing import Collection
  20. from typing import Dict
  21. from typing import Generic
  22. from typing import ItemsView
  23. from typing import Iterable
  24. from typing import Iterator
  25. from typing import KeysView
  26. from typing import List
  27. from typing import Mapping
  28. from typing import MutableMapping
  29. from typing import MutableSequence
  30. from typing import MutableSet
  31. from typing import NoReturn
  32. from typing import Optional
  33. from typing import overload
  34. from typing import Set
  35. from typing import Tuple
  36. from typing import Type
  37. from typing import TypeVar
  38. from typing import Union
  39. from typing import ValuesView
  40. from .. import ColumnElement
  41. from .. import exc
  42. from .. import inspect
  43. from .. import orm
  44. from .. import util
  45. from ..orm import collections
  46. from ..orm import InspectionAttrExtensionType
  47. from ..orm import interfaces
  48. from ..orm import ORMDescriptor
  49. from ..orm.base import SQLORMOperations
  50. from ..orm.interfaces import _AttributeOptions
  51. from ..orm.interfaces import _DCAttributeOptions
  52. from ..orm.interfaces import _DEFAULT_ATTRIBUTE_OPTIONS
  53. from ..sql import operators
  54. from ..sql import or_
  55. from ..sql.base import _NoArg
  56. from ..util.typing import Literal
  57. from ..util.typing import Protocol
  58. from ..util.typing import Self
  59. from ..util.typing import SupportsIndex
  60. from ..util.typing import SupportsKeysAndGetItem
  61. if typing.TYPE_CHECKING:
  62. from ..orm.interfaces import MapperProperty
  63. from ..orm.interfaces import PropComparator
  64. from ..orm.mapper import Mapper
  65. from ..sql._typing import _ColumnExpressionArgument
  66. from ..sql._typing import _InfoType
  67. _T = TypeVar("_T", bound=Any)
  68. _T_co = TypeVar("_T_co", bound=Any, covariant=True)
  69. _T_con = TypeVar("_T_con", bound=Any, contravariant=True)
  70. _S = TypeVar("_S", bound=Any)
  71. _KT = TypeVar("_KT", bound=Any)
  72. _VT = TypeVar("_VT", bound=Any)
  73. def association_proxy(
  74. target_collection: str,
  75. attr: str,
  76. *,
  77. creator: Optional[_CreatorProtocol] = None,
  78. getset_factory: Optional[_GetSetFactoryProtocol] = None,
  79. proxy_factory: Optional[_ProxyFactoryProtocol] = None,
  80. proxy_bulk_set: Optional[_ProxyBulkSetProtocol] = None,
  81. info: Optional[_InfoType] = None,
  82. cascade_scalar_deletes: bool = False,
  83. create_on_none_assignment: bool = False,
  84. init: Union[_NoArg, bool] = _NoArg.NO_ARG,
  85. repr: Union[_NoArg, bool] = _NoArg.NO_ARG, # noqa: A002
  86. default: Optional[Any] = _NoArg.NO_ARG,
  87. default_factory: Union[_NoArg, Callable[[], _T]] = _NoArg.NO_ARG,
  88. compare: Union[_NoArg, bool] = _NoArg.NO_ARG,
  89. kw_only: Union[_NoArg, bool] = _NoArg.NO_ARG,
  90. hash: Union[_NoArg, bool, None] = _NoArg.NO_ARG, # noqa: A002
  91. dataclass_metadata: Union[_NoArg, Mapping[Any, Any], None] = _NoArg.NO_ARG,
  92. ) -> AssociationProxy[Any]:
  93. r"""Return a Python property implementing a view of a target
  94. attribute which references an attribute on members of the
  95. target.
  96. The returned value is an instance of :class:`.AssociationProxy`.
  97. Implements a Python property representing a relationship as a collection
  98. of simpler values, or a scalar value. The proxied property will mimic
  99. the collection type of the target (list, dict or set), or, in the case of
  100. a one to one relationship, a simple scalar value.
  101. :param target_collection: Name of the attribute that is the immediate
  102. target. This attribute is typically mapped by
  103. :func:`~sqlalchemy.orm.relationship` to link to a target collection, but
  104. can also be a many-to-one or non-scalar relationship.
  105. :param attr: Attribute on the associated instance or instances that
  106. are available on instances of the target object.
  107. :param creator: optional.
  108. Defines custom behavior when new items are added to the proxied
  109. collection.
  110. By default, adding new items to the collection will trigger a
  111. construction of an instance of the target object, passing the given
  112. item as a positional argument to the target constructor. For cases
  113. where this isn't sufficient, :paramref:`.association_proxy.creator`
  114. can supply a callable that will construct the object in the
  115. appropriate way, given the item that was passed.
  116. For list- and set- oriented collections, a single argument is
  117. passed to the callable. For dictionary oriented collections, two
  118. arguments are passed, corresponding to the key and value.
  119. The :paramref:`.association_proxy.creator` callable is also invoked
  120. for scalar (i.e. many-to-one, one-to-one) relationships. If the
  121. current value of the target relationship attribute is ``None``, the
  122. callable is used to construct a new object. If an object value already
  123. exists, the given attribute value is populated onto that object.
  124. .. seealso::
  125. :ref:`associationproxy_creator`
  126. :param cascade_scalar_deletes: when True, indicates that setting
  127. the proxied value to ``None``, or deleting it via ``del``, should
  128. also remove the source object. Only applies to scalar attributes.
  129. Normally, removing the proxied target will not remove the proxy
  130. source, as this object may have other state that is still to be
  131. kept.
  132. .. versionadded:: 1.3
  133. .. seealso::
  134. :ref:`cascade_scalar_deletes` - complete usage example
  135. :param create_on_none_assignment: when True, indicates that setting
  136. the proxied value to ``None`` should **create** the source object
  137. if it does not exist, using the creator. Only applies to scalar
  138. attributes. This is mutually exclusive
  139. vs. the :paramref:`.assocation_proxy.cascade_scalar_deletes`.
  140. .. versionadded:: 2.0.18
  141. :param init: Specific to :ref:`orm_declarative_native_dataclasses`,
  142. specifies if the mapped attribute should be part of the ``__init__()``
  143. method as generated by the dataclass process.
  144. .. versionadded:: 2.0.0b4
  145. :param repr: Specific to :ref:`orm_declarative_native_dataclasses`,
  146. specifies if the attribute established by this :class:`.AssociationProxy`
  147. should be part of the ``__repr__()`` method as generated by the dataclass
  148. process.
  149. .. versionadded:: 2.0.0b4
  150. :param default_factory: Specific to
  151. :ref:`orm_declarative_native_dataclasses`, specifies a default-value
  152. generation function that will take place as part of the ``__init__()``
  153. method as generated by the dataclass process.
  154. .. versionadded:: 2.0.0b4
  155. :param compare: Specific to
  156. :ref:`orm_declarative_native_dataclasses`, indicates if this field
  157. should be included in comparison operations when generating the
  158. ``__eq__()`` and ``__ne__()`` methods for the mapped class.
  159. .. versionadded:: 2.0.0b4
  160. :param kw_only: Specific to :ref:`orm_declarative_native_dataclasses`,
  161. indicates if this field should be marked as keyword-only when generating
  162. the ``__init__()`` method as generated by the dataclass process.
  163. .. versionadded:: 2.0.0b4
  164. :param hash: Specific to
  165. :ref:`orm_declarative_native_dataclasses`, controls if this field
  166. is included when generating the ``__hash__()`` method for the mapped
  167. class.
  168. .. versionadded:: 2.0.36
  169. :param dataclass_metadata: Specific to
  170. :ref:`orm_declarative_native_dataclasses`, supplies metadata
  171. to be attached to the generated dataclass field.
  172. .. versionadded:: 2.0.42
  173. :param info: optional, will be assigned to
  174. :attr:`.AssociationProxy.info` if present.
  175. The following additional parameters involve injection of custom behaviors
  176. within the :class:`.AssociationProxy` object and are for advanced use
  177. only:
  178. :param getset_factory: Optional. Proxied attribute access is
  179. automatically handled by routines that get and set values based on
  180. the `attr` argument for this proxy.
  181. If you would like to customize this behavior, you may supply a
  182. `getset_factory` callable that produces a tuple of `getter` and
  183. `setter` functions. The factory is called with two arguments, the
  184. abstract type of the underlying collection and this proxy instance.
  185. :param proxy_factory: Optional. The type of collection to emulate is
  186. determined by sniffing the target collection. If your collection
  187. type can't be determined by duck typing or you'd like to use a
  188. different collection implementation, you may supply a factory
  189. function to produce those collections. Only applicable to
  190. non-scalar relationships.
  191. :param proxy_bulk_set: Optional, use with proxy_factory.
  192. """
  193. return AssociationProxy(
  194. target_collection,
  195. attr,
  196. creator=creator,
  197. getset_factory=getset_factory,
  198. proxy_factory=proxy_factory,
  199. proxy_bulk_set=proxy_bulk_set,
  200. info=info,
  201. cascade_scalar_deletes=cascade_scalar_deletes,
  202. create_on_none_assignment=create_on_none_assignment,
  203. attribute_options=_AttributeOptions(
  204. init,
  205. repr,
  206. default,
  207. default_factory,
  208. compare,
  209. kw_only,
  210. hash,
  211. dataclass_metadata,
  212. ),
  213. )
  214. class AssociationProxyExtensionType(InspectionAttrExtensionType):
  215. ASSOCIATION_PROXY = "ASSOCIATION_PROXY"
  216. """Symbol indicating an :class:`.InspectionAttr` that's
  217. of type :class:`.AssociationProxy`.
  218. Is assigned to the :attr:`.InspectionAttr.extension_type`
  219. attribute.
  220. """
  221. class _GetterProtocol(Protocol[_T_co]):
  222. def __call__(self, instance: Any) -> _T_co: ...
  223. # mypy 0.990 we are no longer allowed to make this Protocol[_T_con]
  224. class _SetterProtocol(Protocol): ...
  225. class _PlainSetterProtocol(_SetterProtocol, Protocol[_T_con]):
  226. def __call__(self, instance: Any, value: _T_con) -> None: ...
  227. class _DictSetterProtocol(_SetterProtocol, Protocol[_T_con]):
  228. def __call__(self, instance: Any, key: Any, value: _T_con) -> None: ...
  229. # mypy 0.990 we are no longer allowed to make this Protocol[_T_con]
  230. class _CreatorProtocol(Protocol): ...
  231. class _PlainCreatorProtocol(_CreatorProtocol, Protocol[_T_con]):
  232. def __call__(self, value: _T_con) -> Any: ...
  233. class _KeyCreatorProtocol(_CreatorProtocol, Protocol[_T_con]):
  234. def __call__(self, key: Any, value: Optional[_T_con]) -> Any: ...
  235. class _LazyCollectionProtocol(Protocol[_T]):
  236. def __call__(
  237. self,
  238. ) -> Union[
  239. MutableSet[_T], MutableMapping[Any, _T], MutableSequence[_T]
  240. ]: ...
  241. class _GetSetFactoryProtocol(Protocol):
  242. def __call__(
  243. self,
  244. collection_class: Optional[Type[Any]],
  245. assoc_instance: AssociationProxyInstance[Any],
  246. ) -> Tuple[_GetterProtocol[Any], _SetterProtocol]: ...
  247. class _ProxyFactoryProtocol(Protocol):
  248. def __call__(
  249. self,
  250. lazy_collection: _LazyCollectionProtocol[Any],
  251. creator: _CreatorProtocol,
  252. value_attr: str,
  253. parent: AssociationProxyInstance[Any],
  254. ) -> Any: ...
  255. class _ProxyBulkSetProtocol(Protocol):
  256. def __call__(
  257. self, proxy: _AssociationCollection[Any], collection: Iterable[Any]
  258. ) -> None: ...
  259. class _AssociationProxyProtocol(Protocol[_T]):
  260. """describes the interface of :class:`.AssociationProxy`
  261. without including descriptor methods in the interface."""
  262. creator: Optional[_CreatorProtocol]
  263. key: str
  264. target_collection: str
  265. value_attr: str
  266. cascade_scalar_deletes: bool
  267. create_on_none_assignment: bool
  268. getset_factory: Optional[_GetSetFactoryProtocol]
  269. proxy_factory: Optional[_ProxyFactoryProtocol]
  270. proxy_bulk_set: Optional[_ProxyBulkSetProtocol]
  271. @util.ro_memoized_property
  272. def info(self) -> _InfoType: ...
  273. def for_class(
  274. self, class_: Type[Any], obj: Optional[object] = None
  275. ) -> AssociationProxyInstance[_T]: ...
  276. def _default_getset(
  277. self, collection_class: Any
  278. ) -> Tuple[_GetterProtocol[Any], _SetterProtocol]: ...
  279. class AssociationProxy(
  280. interfaces.InspectionAttrInfo,
  281. ORMDescriptor[_T],
  282. _DCAttributeOptions,
  283. _AssociationProxyProtocol[_T],
  284. ):
  285. """A descriptor that presents a read/write view of an object attribute."""
  286. is_attribute = True
  287. extension_type = AssociationProxyExtensionType.ASSOCIATION_PROXY
  288. def __init__(
  289. self,
  290. target_collection: str,
  291. attr: str,
  292. *,
  293. creator: Optional[_CreatorProtocol] = None,
  294. getset_factory: Optional[_GetSetFactoryProtocol] = None,
  295. proxy_factory: Optional[_ProxyFactoryProtocol] = None,
  296. proxy_bulk_set: Optional[_ProxyBulkSetProtocol] = None,
  297. info: Optional[_InfoType] = None,
  298. cascade_scalar_deletes: bool = False,
  299. create_on_none_assignment: bool = False,
  300. attribute_options: Optional[_AttributeOptions] = None,
  301. ):
  302. """Construct a new :class:`.AssociationProxy`.
  303. The :class:`.AssociationProxy` object is typically constructed using
  304. the :func:`.association_proxy` constructor function. See the
  305. description of :func:`.association_proxy` for a description of all
  306. parameters.
  307. """
  308. self.target_collection = target_collection
  309. self.value_attr = attr
  310. self.creator = creator
  311. self.getset_factory = getset_factory
  312. self.proxy_factory = proxy_factory
  313. self.proxy_bulk_set = proxy_bulk_set
  314. if cascade_scalar_deletes and create_on_none_assignment:
  315. raise exc.ArgumentError(
  316. "The cascade_scalar_deletes and create_on_none_assignment "
  317. "parameters are mutually exclusive."
  318. )
  319. self.cascade_scalar_deletes = cascade_scalar_deletes
  320. self.create_on_none_assignment = create_on_none_assignment
  321. self.key = "_%s_%s_%s" % (
  322. type(self).__name__,
  323. target_collection,
  324. id(self),
  325. )
  326. if info:
  327. self.info = info # type: ignore
  328. if (
  329. attribute_options
  330. and attribute_options != _DEFAULT_ATTRIBUTE_OPTIONS
  331. ):
  332. self._has_dataclass_arguments = True
  333. self._attribute_options = attribute_options
  334. else:
  335. self._has_dataclass_arguments = False
  336. self._attribute_options = _DEFAULT_ATTRIBUTE_OPTIONS
  337. @overload
  338. def __get__(
  339. self, instance: Literal[None], owner: Literal[None]
  340. ) -> Self: ...
  341. @overload
  342. def __get__(
  343. self, instance: Literal[None], owner: Any
  344. ) -> AssociationProxyInstance[_T]: ...
  345. @overload
  346. def __get__(self, instance: object, owner: Any) -> _T: ...
  347. def __get__(
  348. self, instance: object, owner: Any
  349. ) -> Union[AssociationProxyInstance[_T], _T, AssociationProxy[_T]]:
  350. if owner is None:
  351. return self
  352. inst = self._as_instance(owner, instance)
  353. if inst:
  354. return inst.get(instance)
  355. assert instance is None
  356. return self
  357. def __set__(self, instance: object, values: _T) -> None:
  358. class_ = type(instance)
  359. self._as_instance(class_, instance).set(instance, values)
  360. def __delete__(self, instance: object) -> None:
  361. class_ = type(instance)
  362. self._as_instance(class_, instance).delete(instance)
  363. def for_class(
  364. self, class_: Type[Any], obj: Optional[object] = None
  365. ) -> AssociationProxyInstance[_T]:
  366. r"""Return the internal state local to a specific mapped class.
  367. E.g., given a class ``User``::
  368. class User(Base):
  369. # ...
  370. keywords = association_proxy("kws", "keyword")
  371. If we access this :class:`.AssociationProxy` from
  372. :attr:`_orm.Mapper.all_orm_descriptors`, and we want to view the
  373. target class for this proxy as mapped by ``User``::
  374. inspect(User).all_orm_descriptors["keywords"].for_class(User).target_class
  375. This returns an instance of :class:`.AssociationProxyInstance` that
  376. is specific to the ``User`` class. The :class:`.AssociationProxy`
  377. object remains agnostic of its parent class.
  378. :param class\_: the class that we are returning state for.
  379. :param obj: optional, an instance of the class that is required
  380. if the attribute refers to a polymorphic target, e.g. where we have
  381. to look at the type of the actual destination object to get the
  382. complete path.
  383. .. versionadded:: 1.3 - :class:`.AssociationProxy` no longer stores
  384. any state specific to a particular parent class; the state is now
  385. stored in per-class :class:`.AssociationProxyInstance` objects.
  386. """
  387. return self._as_instance(class_, obj)
  388. def _as_instance(
  389. self, class_: Any, obj: Any
  390. ) -> AssociationProxyInstance[_T]:
  391. try:
  392. inst = class_.__dict__[self.key + "_inst"]
  393. except KeyError:
  394. inst = None
  395. # avoid exception context
  396. if inst is None:
  397. owner = self._calc_owner(class_)
  398. if owner is not None:
  399. inst = AssociationProxyInstance.for_proxy(self, owner, obj)
  400. setattr(class_, self.key + "_inst", inst)
  401. else:
  402. inst = None
  403. if inst is not None and not inst._is_canonical:
  404. # the AssociationProxyInstance can't be generalized
  405. # since the proxied attribute is not on the targeted
  406. # class, only on subclasses of it, which might be
  407. # different. only return for the specific
  408. # object's current value
  409. return inst._non_canonical_get_for_object(obj) # type: ignore
  410. else:
  411. return inst # type: ignore # TODO
  412. def _calc_owner(self, target_cls: Any) -> Any:
  413. # we might be getting invoked for a subclass
  414. # that is not mapped yet, in some declarative situations.
  415. # save until we are mapped
  416. try:
  417. insp = inspect(target_cls)
  418. except exc.NoInspectionAvailable:
  419. # can't find a mapper, don't set owner. if we are a not-yet-mapped
  420. # subclass, we can also scan through __mro__ to find a mapped
  421. # class, but instead just wait for us to be called again against a
  422. # mapped class normally.
  423. return None
  424. else:
  425. return insp.mapper.class_manager.class_
  426. def _default_getset(
  427. self, collection_class: Any
  428. ) -> Tuple[_GetterProtocol[Any], _SetterProtocol]:
  429. attr = self.value_attr
  430. _getter = operator.attrgetter(attr)
  431. def getter(instance: Any) -> Optional[Any]:
  432. return _getter(instance) if instance is not None else None
  433. if collection_class is dict:
  434. def dict_setter(instance: Any, k: Any, value: Any) -> None:
  435. setattr(instance, attr, value)
  436. return getter, dict_setter
  437. else:
  438. def plain_setter(o: Any, v: Any) -> None:
  439. setattr(o, attr, v)
  440. return getter, plain_setter
  441. def __repr__(self) -> str:
  442. return "AssociationProxy(%r, %r)" % (
  443. self.target_collection,
  444. self.value_attr,
  445. )
  446. # the pep-673 Self type does not work in Mypy for a "hybrid"
  447. # style method that returns type or Self, so for one specific case
  448. # we still need to use the pre-pep-673 workaround.
  449. _Self = TypeVar("_Self", bound="AssociationProxyInstance[Any]")
  450. class AssociationProxyInstance(SQLORMOperations[_T]):
  451. """A per-class object that serves class- and object-specific results.
  452. This is used by :class:`.AssociationProxy` when it is invoked
  453. in terms of a specific class or instance of a class, i.e. when it is
  454. used as a regular Python descriptor.
  455. When referring to the :class:`.AssociationProxy` as a normal Python
  456. descriptor, the :class:`.AssociationProxyInstance` is the object that
  457. actually serves the information. Under normal circumstances, its presence
  458. is transparent::
  459. >>> User.keywords.scalar
  460. False
  461. In the special case that the :class:`.AssociationProxy` object is being
  462. accessed directly, in order to get an explicit handle to the
  463. :class:`.AssociationProxyInstance`, use the
  464. :meth:`.AssociationProxy.for_class` method::
  465. proxy_state = inspect(User).all_orm_descriptors["keywords"].for_class(User)
  466. # view if proxy object is scalar or not
  467. >>> proxy_state.scalar
  468. False
  469. .. versionadded:: 1.3
  470. """ # noqa
  471. collection_class: Optional[Type[Any]]
  472. parent: _AssociationProxyProtocol[_T]
  473. def __init__(
  474. self,
  475. parent: _AssociationProxyProtocol[_T],
  476. owning_class: Type[Any],
  477. target_class: Type[Any],
  478. value_attr: str,
  479. ):
  480. self.parent = parent
  481. self.key = parent.key
  482. self.owning_class = owning_class
  483. self.target_collection = parent.target_collection
  484. self.collection_class = None
  485. self.target_class = target_class
  486. self.value_attr = value_attr
  487. target_class: Type[Any]
  488. """The intermediary class handled by this
  489. :class:`.AssociationProxyInstance`.
  490. Intercepted append/set/assignment events will result
  491. in the generation of new instances of this class.
  492. """
  493. @classmethod
  494. def for_proxy(
  495. cls,
  496. parent: AssociationProxy[_T],
  497. owning_class: Type[Any],
  498. parent_instance: Any,
  499. ) -> AssociationProxyInstance[_T]:
  500. target_collection = parent.target_collection
  501. value_attr = parent.value_attr
  502. prop = cast(
  503. "orm.RelationshipProperty[_T]",
  504. orm.class_mapper(owning_class).get_property(target_collection),
  505. )
  506. # this was never asserted before but this should be made clear.
  507. if not isinstance(prop, orm.RelationshipProperty):
  508. raise NotImplementedError(
  509. "association proxy to a non-relationship "
  510. "intermediary is not supported"
  511. ) from None
  512. target_class = prop.mapper.class_
  513. try:
  514. target_assoc = cast(
  515. "AssociationProxyInstance[_T]",
  516. cls._cls_unwrap_target_assoc_proxy(target_class, value_attr),
  517. )
  518. except AttributeError:
  519. # the proxied attribute doesn't exist on the target class;
  520. # return an "ambiguous" instance that will work on a per-object
  521. # basis
  522. return AmbiguousAssociationProxyInstance(
  523. parent, owning_class, target_class, value_attr
  524. )
  525. except Exception as err:
  526. raise exc.InvalidRequestError(
  527. f"Association proxy received an unexpected error when "
  528. f"trying to retreive attribute "
  529. f'"{target_class.__name__}.{parent.value_attr}" from '
  530. f'class "{target_class.__name__}": {err}'
  531. ) from err
  532. else:
  533. return cls._construct_for_assoc(
  534. target_assoc, parent, owning_class, target_class, value_attr
  535. )
  536. @classmethod
  537. def _construct_for_assoc(
  538. cls,
  539. target_assoc: Optional[AssociationProxyInstance[_T]],
  540. parent: _AssociationProxyProtocol[_T],
  541. owning_class: Type[Any],
  542. target_class: Type[Any],
  543. value_attr: str,
  544. ) -> AssociationProxyInstance[_T]:
  545. if target_assoc is not None:
  546. return ObjectAssociationProxyInstance(
  547. parent, owning_class, target_class, value_attr
  548. )
  549. attr = getattr(target_class, value_attr)
  550. if not hasattr(attr, "_is_internal_proxy"):
  551. return AmbiguousAssociationProxyInstance(
  552. parent, owning_class, target_class, value_attr
  553. )
  554. is_object = attr._impl_uses_objects
  555. if is_object:
  556. return ObjectAssociationProxyInstance(
  557. parent, owning_class, target_class, value_attr
  558. )
  559. else:
  560. return ColumnAssociationProxyInstance(
  561. parent, owning_class, target_class, value_attr
  562. )
  563. def _get_property(self) -> MapperProperty[Any]:
  564. return orm.class_mapper(self.owning_class).get_property(
  565. self.target_collection
  566. )
  567. @property
  568. def _comparator(self) -> PropComparator[Any]:
  569. return getattr( # type: ignore
  570. self.owning_class, self.target_collection
  571. ).comparator
  572. def __clause_element__(self) -> NoReturn:
  573. raise NotImplementedError(
  574. "The association proxy can't be used as a plain column "
  575. "expression; it only works inside of a comparison expression"
  576. )
  577. @classmethod
  578. def _cls_unwrap_target_assoc_proxy(
  579. cls, target_class: Any, value_attr: str
  580. ) -> Optional[AssociationProxyInstance[_T]]:
  581. attr = getattr(target_class, value_attr)
  582. assert not isinstance(attr, AssociationProxy)
  583. if isinstance(attr, AssociationProxyInstance):
  584. return attr
  585. return None
  586. @util.memoized_property
  587. def _unwrap_target_assoc_proxy(
  588. self,
  589. ) -> Optional[AssociationProxyInstance[_T]]:
  590. return self._cls_unwrap_target_assoc_proxy(
  591. self.target_class, self.value_attr
  592. )
  593. @property
  594. def remote_attr(self) -> SQLORMOperations[_T]:
  595. """The 'remote' class attribute referenced by this
  596. :class:`.AssociationProxyInstance`.
  597. .. seealso::
  598. :attr:`.AssociationProxyInstance.attr`
  599. :attr:`.AssociationProxyInstance.local_attr`
  600. """
  601. return cast(
  602. "SQLORMOperations[_T]", getattr(self.target_class, self.value_attr)
  603. )
  604. @property
  605. def local_attr(self) -> SQLORMOperations[Any]:
  606. """The 'local' class attribute referenced by this
  607. :class:`.AssociationProxyInstance`.
  608. .. seealso::
  609. :attr:`.AssociationProxyInstance.attr`
  610. :attr:`.AssociationProxyInstance.remote_attr`
  611. """
  612. return cast(
  613. "SQLORMOperations[Any]",
  614. getattr(self.owning_class, self.target_collection),
  615. )
  616. @property
  617. def attr(self) -> Tuple[SQLORMOperations[Any], SQLORMOperations[_T]]:
  618. """Return a tuple of ``(local_attr, remote_attr)``.
  619. This attribute was originally intended to facilitate using the
  620. :meth:`_query.Query.join` method to join across the two relationships
  621. at once, however this makes use of a deprecated calling style.
  622. To use :meth:`_sql.select.join` or :meth:`_orm.Query.join` with
  623. an association proxy, the current method is to make use of the
  624. :attr:`.AssociationProxyInstance.local_attr` and
  625. :attr:`.AssociationProxyInstance.remote_attr` attributes separately::
  626. stmt = (
  627. select(Parent)
  628. .join(Parent.proxied.local_attr)
  629. .join(Parent.proxied.remote_attr)
  630. )
  631. A future release may seek to provide a more succinct join pattern
  632. for association proxy attributes.
  633. .. seealso::
  634. :attr:`.AssociationProxyInstance.local_attr`
  635. :attr:`.AssociationProxyInstance.remote_attr`
  636. """
  637. return (self.local_attr, self.remote_attr)
  638. @util.memoized_property
  639. def scalar(self) -> bool:
  640. """Return ``True`` if this :class:`.AssociationProxyInstance`
  641. proxies a scalar relationship on the local side."""
  642. scalar = not self._get_property().uselist
  643. if scalar:
  644. self._initialize_scalar_accessors()
  645. return scalar
  646. @util.memoized_property
  647. def _value_is_scalar(self) -> bool:
  648. return (
  649. not self._get_property()
  650. .mapper.get_property(self.value_attr)
  651. .uselist
  652. )
  653. @property
  654. def _target_is_object(self) -> bool:
  655. raise NotImplementedError()
  656. _scalar_get: _GetterProtocol[_T]
  657. _scalar_set: _PlainSetterProtocol[_T]
  658. def _initialize_scalar_accessors(self) -> None:
  659. if self.parent.getset_factory:
  660. get, set_ = self.parent.getset_factory(None, self)
  661. else:
  662. get, set_ = self.parent._default_getset(None)
  663. self._scalar_get, self._scalar_set = get, cast(
  664. "_PlainSetterProtocol[_T]", set_
  665. )
  666. def _default_getset(
  667. self, collection_class: Any
  668. ) -> Tuple[_GetterProtocol[Any], _SetterProtocol]:
  669. attr = self.value_attr
  670. _getter = operator.attrgetter(attr)
  671. def getter(instance: Any) -> Optional[_T]:
  672. return _getter(instance) if instance is not None else None
  673. if collection_class is dict:
  674. def dict_setter(instance: Any, k: Any, value: _T) -> None:
  675. setattr(instance, attr, value)
  676. return getter, dict_setter
  677. else:
  678. def plain_setter(o: Any, v: _T) -> None:
  679. setattr(o, attr, v)
  680. return getter, plain_setter
  681. @util.ro_non_memoized_property
  682. def info(self) -> _InfoType:
  683. return self.parent.info
  684. @overload
  685. def get(self: _Self, obj: Literal[None]) -> _Self: ...
  686. @overload
  687. def get(self, obj: Any) -> _T: ...
  688. def get(
  689. self, obj: Any
  690. ) -> Union[Optional[_T], AssociationProxyInstance[_T]]:
  691. if obj is None:
  692. return self
  693. proxy: _T
  694. if self.scalar:
  695. target = getattr(obj, self.target_collection)
  696. return self._scalar_get(target)
  697. else:
  698. try:
  699. # If the owning instance is reborn (orm session resurrect,
  700. # etc.), refresh the proxy cache.
  701. creator_id, self_id, proxy = cast(
  702. "Tuple[int, int, _T]", getattr(obj, self.key)
  703. )
  704. except AttributeError:
  705. pass
  706. else:
  707. if id(obj) == creator_id and id(self) == self_id:
  708. assert self.collection_class is not None
  709. return proxy
  710. self.collection_class, proxy = self._new(
  711. _lazy_collection(obj, self.target_collection)
  712. )
  713. setattr(obj, self.key, (id(obj), id(self), proxy))
  714. return proxy
  715. def set(self, obj: Any, values: _T) -> None:
  716. if self.scalar:
  717. creator = cast(
  718. "_PlainCreatorProtocol[_T]",
  719. (
  720. self.parent.creator
  721. if self.parent.creator
  722. else self.target_class
  723. ),
  724. )
  725. target = getattr(obj, self.target_collection)
  726. if target is None:
  727. if (
  728. values is None
  729. and not self.parent.create_on_none_assignment
  730. ):
  731. return
  732. setattr(obj, self.target_collection, creator(values))
  733. else:
  734. self._scalar_set(target, values)
  735. if values is None and self.parent.cascade_scalar_deletes:
  736. setattr(obj, self.target_collection, None)
  737. else:
  738. proxy = self.get(obj)
  739. assert self.collection_class is not None
  740. if proxy is not values:
  741. proxy._bulk_replace(self, values)
  742. def delete(self, obj: Any) -> None:
  743. if self.owning_class is None:
  744. self._calc_owner(obj, None)
  745. if self.scalar:
  746. target = getattr(obj, self.target_collection)
  747. if target is not None:
  748. delattr(target, self.value_attr)
  749. delattr(obj, self.target_collection)
  750. def _new(
  751. self, lazy_collection: _LazyCollectionProtocol[_T]
  752. ) -> Tuple[Type[Any], _T]:
  753. creator = (
  754. self.parent.creator
  755. if self.parent.creator is not None
  756. else cast("_CreatorProtocol", self.target_class)
  757. )
  758. collection_class = util.duck_type_collection(lazy_collection())
  759. if collection_class is None:
  760. raise exc.InvalidRequestError(
  761. f"lazy collection factory did not return a "
  762. f"valid collection type, got {collection_class}"
  763. )
  764. if self.parent.proxy_factory:
  765. return (
  766. collection_class,
  767. self.parent.proxy_factory(
  768. lazy_collection, creator, self.value_attr, self
  769. ),
  770. )
  771. if self.parent.getset_factory:
  772. getter, setter = self.parent.getset_factory(collection_class, self)
  773. else:
  774. getter, setter = self.parent._default_getset(collection_class)
  775. if collection_class is list:
  776. return (
  777. collection_class,
  778. cast(
  779. _T,
  780. _AssociationList(
  781. lazy_collection, creator, getter, setter, self
  782. ),
  783. ),
  784. )
  785. elif collection_class is dict:
  786. return (
  787. collection_class,
  788. cast(
  789. _T,
  790. _AssociationDict(
  791. lazy_collection, creator, getter, setter, self
  792. ),
  793. ),
  794. )
  795. elif collection_class is set:
  796. return (
  797. collection_class,
  798. cast(
  799. _T,
  800. _AssociationSet(
  801. lazy_collection, creator, getter, setter, self
  802. ),
  803. ),
  804. )
  805. else:
  806. raise exc.ArgumentError(
  807. "could not guess which interface to use for "
  808. 'collection_class "%s" backing "%s"; specify a '
  809. "proxy_factory and proxy_bulk_set manually"
  810. % (self.collection_class, self.target_collection)
  811. )
  812. def _set(
  813. self, proxy: _AssociationCollection[Any], values: Iterable[Any]
  814. ) -> None:
  815. if self.parent.proxy_bulk_set:
  816. self.parent.proxy_bulk_set(proxy, values)
  817. elif self.collection_class is list:
  818. cast("_AssociationList[Any]", proxy).extend(values)
  819. elif self.collection_class is dict:
  820. cast("_AssociationDict[Any, Any]", proxy).update(values)
  821. elif self.collection_class is set:
  822. cast("_AssociationSet[Any]", proxy).update(values)
  823. else:
  824. raise exc.ArgumentError(
  825. "no proxy_bulk_set supplied for custom "
  826. "collection_class implementation"
  827. )
  828. def _inflate(self, proxy: _AssociationCollection[Any]) -> None:
  829. creator = (
  830. self.parent.creator
  831. and self.parent.creator
  832. or cast(_CreatorProtocol, self.target_class)
  833. )
  834. if self.parent.getset_factory:
  835. getter, setter = self.parent.getset_factory(
  836. self.collection_class, self
  837. )
  838. else:
  839. getter, setter = self.parent._default_getset(self.collection_class)
  840. proxy.creator = creator
  841. proxy.getter = getter
  842. proxy.setter = setter
  843. def _criterion_exists(
  844. self,
  845. criterion: Optional[_ColumnExpressionArgument[bool]] = None,
  846. **kwargs: Any,
  847. ) -> ColumnElement[bool]:
  848. is_has = kwargs.pop("is_has", None)
  849. target_assoc = self._unwrap_target_assoc_proxy
  850. if target_assoc is not None:
  851. inner = target_assoc._criterion_exists(
  852. criterion=criterion, **kwargs
  853. )
  854. return self._comparator._criterion_exists(inner)
  855. if self._target_is_object:
  856. attr = getattr(self.target_class, self.value_attr)
  857. value_expr = attr.comparator._criterion_exists(criterion, **kwargs)
  858. else:
  859. if kwargs:
  860. raise exc.ArgumentError(
  861. "Can't apply keyword arguments to column-targeted "
  862. "association proxy; use =="
  863. )
  864. elif is_has and criterion is not None:
  865. raise exc.ArgumentError(
  866. "Non-empty has() not allowed for "
  867. "column-targeted association proxy; use =="
  868. )
  869. value_expr = criterion
  870. return self._comparator._criterion_exists(value_expr)
  871. def any(
  872. self,
  873. criterion: Optional[_ColumnExpressionArgument[bool]] = None,
  874. **kwargs: Any,
  875. ) -> ColumnElement[bool]:
  876. """Produce a proxied 'any' expression using EXISTS.
  877. This expression will be a composed product
  878. using the :meth:`.Relationship.Comparator.any`
  879. and/or :meth:`.Relationship.Comparator.has`
  880. operators of the underlying proxied attributes.
  881. """
  882. if self._unwrap_target_assoc_proxy is None and (
  883. self.scalar
  884. and (not self._target_is_object or self._value_is_scalar)
  885. ):
  886. raise exc.InvalidRequestError(
  887. "'any()' not implemented for scalar attributes. Use has()."
  888. )
  889. return self._criterion_exists(
  890. criterion=criterion, is_has=False, **kwargs
  891. )
  892. def has(
  893. self,
  894. criterion: Optional[_ColumnExpressionArgument[bool]] = None,
  895. **kwargs: Any,
  896. ) -> ColumnElement[bool]:
  897. """Produce a proxied 'has' expression using EXISTS.
  898. This expression will be a composed product
  899. using the :meth:`.Relationship.Comparator.any`
  900. and/or :meth:`.Relationship.Comparator.has`
  901. operators of the underlying proxied attributes.
  902. """
  903. if self._unwrap_target_assoc_proxy is None and (
  904. not self.scalar
  905. or (self._target_is_object and not self._value_is_scalar)
  906. ):
  907. raise exc.InvalidRequestError(
  908. "'has()' not implemented for collections. Use any()."
  909. )
  910. return self._criterion_exists(
  911. criterion=criterion, is_has=True, **kwargs
  912. )
  913. def __repr__(self) -> str:
  914. return "%s(%r)" % (self.__class__.__name__, self.parent)
  915. class AmbiguousAssociationProxyInstance(AssociationProxyInstance[_T]):
  916. """an :class:`.AssociationProxyInstance` where we cannot determine
  917. the type of target object.
  918. """
  919. _is_canonical = False
  920. def _ambiguous(self) -> NoReturn:
  921. raise AttributeError(
  922. "Association proxy %s.%s refers to an attribute '%s' that is not "
  923. "directly mapped on class %s; therefore this operation cannot "
  924. "proceed since we don't know what type of object is referred "
  925. "towards"
  926. % (
  927. self.owning_class.__name__,
  928. self.target_collection,
  929. self.value_attr,
  930. self.target_class,
  931. )
  932. )
  933. def get(self, obj: Any) -> Any:
  934. if obj is None:
  935. return self
  936. else:
  937. return super().get(obj)
  938. def __eq__(self, obj: object) -> NoReturn:
  939. self._ambiguous()
  940. def __ne__(self, obj: object) -> NoReturn:
  941. self._ambiguous()
  942. def any(
  943. self,
  944. criterion: Optional[_ColumnExpressionArgument[bool]] = None,
  945. **kwargs: Any,
  946. ) -> NoReturn:
  947. self._ambiguous()
  948. def has(
  949. self,
  950. criterion: Optional[_ColumnExpressionArgument[bool]] = None,
  951. **kwargs: Any,
  952. ) -> NoReturn:
  953. self._ambiguous()
  954. @util.memoized_property
  955. def _lookup_cache(self) -> Dict[Type[Any], AssociationProxyInstance[_T]]:
  956. # mapping of <subclass>->AssociationProxyInstance.
  957. # e.g. proxy is A-> A.b -> B -> B.b_attr, but B.b_attr doesn't exist;
  958. # only B1(B) and B2(B) have "b_attr", keys in here would be B1, B2
  959. return {}
  960. def _non_canonical_get_for_object(
  961. self, parent_instance: Any
  962. ) -> AssociationProxyInstance[_T]:
  963. if parent_instance is not None:
  964. actual_obj = getattr(parent_instance, self.target_collection)
  965. if actual_obj is not None:
  966. try:
  967. insp = inspect(actual_obj)
  968. except exc.NoInspectionAvailable:
  969. pass
  970. else:
  971. mapper = insp.mapper
  972. instance_class = mapper.class_
  973. if instance_class not in self._lookup_cache:
  974. self._populate_cache(instance_class, mapper)
  975. try:
  976. return self._lookup_cache[instance_class]
  977. except KeyError:
  978. pass
  979. # no object or ambiguous object given, so return "self", which
  980. # is a proxy with generally only instance-level functionality
  981. return self
  982. def _populate_cache(
  983. self, instance_class: Any, mapper: Mapper[Any]
  984. ) -> None:
  985. prop = orm.class_mapper(self.owning_class).get_property(
  986. self.target_collection
  987. )
  988. if mapper.isa(prop.mapper):
  989. target_class = instance_class
  990. try:
  991. target_assoc = self._cls_unwrap_target_assoc_proxy(
  992. target_class, self.value_attr
  993. )
  994. except AttributeError:
  995. pass
  996. else:
  997. self._lookup_cache[instance_class] = self._construct_for_assoc(
  998. cast("AssociationProxyInstance[_T]", target_assoc),
  999. self.parent,
  1000. self.owning_class,
  1001. target_class,
  1002. self.value_attr,
  1003. )
  1004. class ObjectAssociationProxyInstance(AssociationProxyInstance[_T]):
  1005. """an :class:`.AssociationProxyInstance` that has an object as a target."""
  1006. _target_is_object: bool = True
  1007. _is_canonical = True
  1008. def contains(self, other: Any, **kw: Any) -> ColumnElement[bool]:
  1009. """Produce a proxied 'contains' expression using EXISTS.
  1010. This expression will be a composed product
  1011. using the :meth:`.Relationship.Comparator.any`,
  1012. :meth:`.Relationship.Comparator.has`,
  1013. and/or :meth:`.Relationship.Comparator.contains`
  1014. operators of the underlying proxied attributes.
  1015. """
  1016. target_assoc = self._unwrap_target_assoc_proxy
  1017. if target_assoc is not None:
  1018. return self._comparator._criterion_exists(
  1019. target_assoc.contains(other)
  1020. if not target_assoc.scalar
  1021. else target_assoc == other
  1022. )
  1023. elif (
  1024. self._target_is_object
  1025. and self.scalar
  1026. and not self._value_is_scalar
  1027. ):
  1028. return self._comparator.has(
  1029. getattr(self.target_class, self.value_attr).contains(other)
  1030. )
  1031. elif self._target_is_object and self.scalar and self._value_is_scalar:
  1032. raise exc.InvalidRequestError(
  1033. "contains() doesn't apply to a scalar object endpoint; use =="
  1034. )
  1035. else:
  1036. return self._comparator._criterion_exists(
  1037. **{self.value_attr: other}
  1038. )
  1039. def __eq__(self, obj: Any) -> ColumnElement[bool]: # type: ignore[override] # noqa: E501
  1040. # note the has() here will fail for collections; eq_()
  1041. # is only allowed with a scalar.
  1042. if obj is None:
  1043. return or_(
  1044. self._comparator.has(**{self.value_attr: obj}),
  1045. self._comparator == None,
  1046. )
  1047. else:
  1048. return self._comparator.has(**{self.value_attr: obj})
  1049. def __ne__(self, obj: Any) -> ColumnElement[bool]: # type: ignore[override] # noqa: E501
  1050. # note the has() here will fail for collections; eq_()
  1051. # is only allowed with a scalar.
  1052. return self._comparator.has(
  1053. getattr(self.target_class, self.value_attr) != obj
  1054. )
  1055. class ColumnAssociationProxyInstance(AssociationProxyInstance[_T]):
  1056. """an :class:`.AssociationProxyInstance` that has a database column as a
  1057. target.
  1058. """
  1059. _target_is_object: bool = False
  1060. _is_canonical = True
  1061. def __eq__(self, other: Any) -> ColumnElement[bool]: # type: ignore[override] # noqa: E501
  1062. # special case "is None" to check for no related row as well
  1063. expr = self._criterion_exists(
  1064. self.remote_attr.operate(operators.eq, other)
  1065. )
  1066. if other is None:
  1067. return or_(expr, self._comparator == None)
  1068. else:
  1069. return expr
  1070. def operate(
  1071. self, op: operators.OperatorType, *other: Any, **kwargs: Any
  1072. ) -> ColumnElement[Any]:
  1073. return self._criterion_exists(
  1074. self.remote_attr.operate(op, *other, **kwargs)
  1075. )
  1076. class _lazy_collection(_LazyCollectionProtocol[_T]):
  1077. def __init__(self, obj: Any, target: str):
  1078. self.parent = obj
  1079. self.target = target
  1080. def __call__(
  1081. self,
  1082. ) -> Union[MutableSet[_T], MutableMapping[Any, _T], MutableSequence[_T]]:
  1083. return getattr(self.parent, self.target) # type: ignore[no-any-return]
  1084. def __getstate__(self) -> Any:
  1085. return {"obj": self.parent, "target": self.target}
  1086. def __setstate__(self, state: Any) -> None:
  1087. self.parent = state["obj"]
  1088. self.target = state["target"]
  1089. _IT = TypeVar("_IT", bound="Any")
  1090. """instance type - this is the type of object inside a collection.
  1091. this is not the same as the _T of AssociationProxy and
  1092. AssociationProxyInstance itself, which will often refer to the
  1093. collection[_IT] type.
  1094. """
  1095. class _AssociationCollection(Generic[_IT]):
  1096. getter: _GetterProtocol[_IT]
  1097. """A function. Given an associated object, return the 'value'."""
  1098. creator: _CreatorProtocol
  1099. """
  1100. A function that creates new target entities. Given one parameter:
  1101. value. This assertion is assumed::
  1102. obj = creator(somevalue)
  1103. assert getter(obj) == somevalue
  1104. """
  1105. parent: AssociationProxyInstance[_IT]
  1106. setter: _SetterProtocol
  1107. """A function. Given an associated object and a value, store that
  1108. value on the object.
  1109. """
  1110. lazy_collection: _LazyCollectionProtocol[_IT]
  1111. """A callable returning a list-based collection of entities (usually an
  1112. object attribute managed by a SQLAlchemy relationship())"""
  1113. def __init__(
  1114. self,
  1115. lazy_collection: _LazyCollectionProtocol[_IT],
  1116. creator: _CreatorProtocol,
  1117. getter: _GetterProtocol[_IT],
  1118. setter: _SetterProtocol,
  1119. parent: AssociationProxyInstance[_IT],
  1120. ):
  1121. """Constructs an _AssociationCollection.
  1122. This will always be a subclass of either _AssociationList,
  1123. _AssociationSet, or _AssociationDict.
  1124. """
  1125. self.lazy_collection = lazy_collection
  1126. self.creator = creator
  1127. self.getter = getter
  1128. self.setter = setter
  1129. self.parent = parent
  1130. if typing.TYPE_CHECKING:
  1131. col: Collection[_IT]
  1132. else:
  1133. col = property(lambda self: self.lazy_collection())
  1134. def __len__(self) -> int:
  1135. return len(self.col)
  1136. def __bool__(self) -> bool:
  1137. return bool(self.col)
  1138. def __getstate__(self) -> Any:
  1139. return {"parent": self.parent, "lazy_collection": self.lazy_collection}
  1140. def __setstate__(self, state: Any) -> None:
  1141. self.parent = state["parent"]
  1142. self.lazy_collection = state["lazy_collection"]
  1143. self.parent._inflate(self)
  1144. def clear(self) -> None:
  1145. raise NotImplementedError()
  1146. class _AssociationSingleItem(_AssociationCollection[_T]):
  1147. setter: _PlainSetterProtocol[_T]
  1148. creator: _PlainCreatorProtocol[_T]
  1149. def _create(self, value: _T) -> Any:
  1150. return self.creator(value)
  1151. def _get(self, object_: Any) -> _T:
  1152. return self.getter(object_)
  1153. def _bulk_replace(
  1154. self, assoc_proxy: AssociationProxyInstance[Any], values: Iterable[_IT]
  1155. ) -> None:
  1156. self.clear()
  1157. assoc_proxy._set(self, values)
  1158. class _AssociationList(_AssociationSingleItem[_T], MutableSequence[_T]):
  1159. """Generic, converting, list-to-list proxy."""
  1160. col: MutableSequence[_T]
  1161. def _set(self, object_: Any, value: _T) -> None:
  1162. self.setter(object_, value)
  1163. @overload
  1164. def __getitem__(self, index: int) -> _T: ...
  1165. @overload
  1166. def __getitem__(self, index: slice) -> MutableSequence[_T]: ...
  1167. def __getitem__(
  1168. self, index: Union[int, slice]
  1169. ) -> Union[_T, MutableSequence[_T]]:
  1170. if not isinstance(index, slice):
  1171. return self._get(self.col[index])
  1172. else:
  1173. return [self._get(member) for member in self.col[index]]
  1174. @overload
  1175. def __setitem__(self, index: int, value: _T) -> None: ...
  1176. @overload
  1177. def __setitem__(self, index: slice, value: Iterable[_T]) -> None: ...
  1178. def __setitem__(
  1179. self, index: Union[int, slice], value: Union[_T, Iterable[_T]]
  1180. ) -> None:
  1181. if not isinstance(index, slice):
  1182. self._set(self.col[index], cast("_T", value))
  1183. else:
  1184. if index.stop is None:
  1185. stop = len(self)
  1186. elif index.stop < 0:
  1187. stop = len(self) + index.stop
  1188. else:
  1189. stop = index.stop
  1190. step = index.step or 1
  1191. start = index.start or 0
  1192. rng = list(range(index.start or 0, stop, step))
  1193. sized_value = list(value)
  1194. if step == 1:
  1195. for i in rng:
  1196. del self[start]
  1197. i = start
  1198. for item in sized_value:
  1199. self.insert(i, item)
  1200. i += 1
  1201. else:
  1202. if len(sized_value) != len(rng):
  1203. raise ValueError(
  1204. "attempt to assign sequence of size %s to "
  1205. "extended slice of size %s"
  1206. % (len(sized_value), len(rng))
  1207. )
  1208. for i, item in zip(rng, value):
  1209. self._set(self.col[i], item)
  1210. @overload
  1211. def __delitem__(self, index: int) -> None: ...
  1212. @overload
  1213. def __delitem__(self, index: slice) -> None: ...
  1214. def __delitem__(self, index: Union[slice, int]) -> None:
  1215. del self.col[index]
  1216. def __contains__(self, value: object) -> bool:
  1217. for member in self.col:
  1218. # testlib.pragma exempt:__eq__
  1219. if self._get(member) == value:
  1220. return True
  1221. return False
  1222. def __iter__(self) -> Iterator[_T]:
  1223. """Iterate over proxied values.
  1224. For the actual domain objects, iterate over .col instead or
  1225. just use the underlying collection directly from its property
  1226. on the parent.
  1227. """
  1228. for member in self.col:
  1229. yield self._get(member)
  1230. return
  1231. def append(self, value: _T) -> None:
  1232. col = self.col
  1233. item = self._create(value)
  1234. col.append(item)
  1235. def count(self, value: Any) -> int:
  1236. count = 0
  1237. for v in self:
  1238. if v == value:
  1239. count += 1
  1240. return count
  1241. def extend(self, values: Iterable[_T]) -> None:
  1242. for v in values:
  1243. self.append(v)
  1244. def insert(self, index: int, value: _T) -> None:
  1245. self.col[index:index] = [self._create(value)]
  1246. def pop(self, index: int = -1) -> _T:
  1247. return self.getter(self.col.pop(index))
  1248. def remove(self, value: _T) -> None:
  1249. for i, val in enumerate(self):
  1250. if val == value:
  1251. del self.col[i]
  1252. return
  1253. raise ValueError("value not in list")
  1254. def reverse(self) -> NoReturn:
  1255. """Not supported, use reversed(mylist)"""
  1256. raise NotImplementedError()
  1257. def sort(self) -> NoReturn:
  1258. """Not supported, use sorted(mylist)"""
  1259. raise NotImplementedError()
  1260. def clear(self) -> None:
  1261. del self.col[0 : len(self.col)]
  1262. def __eq__(self, other: object) -> bool:
  1263. return list(self) == other
  1264. def __ne__(self, other: object) -> bool:
  1265. return list(self) != other
  1266. def __lt__(self, other: List[_T]) -> bool:
  1267. return list(self) < other
  1268. def __le__(self, other: List[_T]) -> bool:
  1269. return list(self) <= other
  1270. def __gt__(self, other: List[_T]) -> bool:
  1271. return list(self) > other
  1272. def __ge__(self, other: List[_T]) -> bool:
  1273. return list(self) >= other
  1274. def __add__(self, other: List[_T]) -> List[_T]:
  1275. try:
  1276. other = list(other)
  1277. except TypeError:
  1278. return NotImplemented
  1279. return list(self) + other
  1280. def __radd__(self, other: List[_T]) -> List[_T]:
  1281. try:
  1282. other = list(other)
  1283. except TypeError:
  1284. return NotImplemented
  1285. return other + list(self)
  1286. def __mul__(self, n: SupportsIndex) -> List[_T]:
  1287. if not isinstance(n, int):
  1288. return NotImplemented
  1289. return list(self) * n
  1290. def __rmul__(self, n: SupportsIndex) -> List[_T]:
  1291. if not isinstance(n, int):
  1292. return NotImplemented
  1293. return n * list(self)
  1294. def __iadd__(self, iterable: Iterable[_T]) -> Self:
  1295. self.extend(iterable)
  1296. return self
  1297. def __imul__(self, n: SupportsIndex) -> Self:
  1298. # unlike a regular list *=, proxied __imul__ will generate unique
  1299. # backing objects for each copy. *= on proxied lists is a bit of
  1300. # a stretch anyhow, and this interpretation of the __imul__ contract
  1301. # is more plausibly useful than copying the backing objects.
  1302. if not isinstance(n, int):
  1303. raise NotImplementedError()
  1304. if n == 0:
  1305. self.clear()
  1306. elif n > 1:
  1307. self.extend(list(self) * (n - 1))
  1308. return self
  1309. if typing.TYPE_CHECKING:
  1310. # TODO: no idea how to do this without separate "stub"
  1311. def index(
  1312. self, value: Any, start: int = ..., stop: int = ...
  1313. ) -> int: ...
  1314. else:
  1315. def index(self, value: Any, *arg) -> int:
  1316. ls = list(self)
  1317. return ls.index(value, *arg)
  1318. def copy(self) -> List[_T]:
  1319. return list(self)
  1320. def __repr__(self) -> str:
  1321. return repr(list(self))
  1322. def __hash__(self) -> NoReturn:
  1323. raise TypeError("%s objects are unhashable" % type(self).__name__)
  1324. if not typing.TYPE_CHECKING:
  1325. for func_name, func in list(locals().items()):
  1326. if (
  1327. callable(func)
  1328. and func.__name__ == func_name
  1329. and not func.__doc__
  1330. and hasattr(list, func_name)
  1331. ):
  1332. func.__doc__ = getattr(list, func_name).__doc__
  1333. del func_name, func
  1334. class _AssociationDict(_AssociationCollection[_VT], MutableMapping[_KT, _VT]):
  1335. """Generic, converting, dict-to-dict proxy."""
  1336. setter: _DictSetterProtocol[_VT]
  1337. creator: _KeyCreatorProtocol[_VT]
  1338. col: MutableMapping[_KT, Optional[_VT]]
  1339. def _create(self, key: _KT, value: Optional[_VT]) -> Any:
  1340. return self.creator(key, value)
  1341. def _get(self, object_: Any) -> _VT:
  1342. return self.getter(object_)
  1343. def _set(self, object_: Any, key: _KT, value: _VT) -> None:
  1344. return self.setter(object_, key, value)
  1345. def __getitem__(self, key: _KT) -> _VT:
  1346. return self._get(self.col[key])
  1347. def __setitem__(self, key: _KT, value: _VT) -> None:
  1348. if key in self.col:
  1349. self._set(self.col[key], key, value)
  1350. else:
  1351. self.col[key] = self._create(key, value)
  1352. def __delitem__(self, key: _KT) -> None:
  1353. del self.col[key]
  1354. def __contains__(self, key: object) -> bool:
  1355. return key in self.col
  1356. def __iter__(self) -> Iterator[_KT]:
  1357. return iter(self.col.keys())
  1358. def clear(self) -> None:
  1359. self.col.clear()
  1360. def __eq__(self, other: object) -> bool:
  1361. return dict(self) == other
  1362. def __ne__(self, other: object) -> bool:
  1363. return dict(self) != other
  1364. def __repr__(self) -> str:
  1365. return repr(dict(self))
  1366. @overload
  1367. def get(self, __key: _KT) -> Optional[_VT]: ...
  1368. @overload
  1369. def get(self, __key: _KT, default: Union[_VT, _T]) -> Union[_VT, _T]: ...
  1370. def get(
  1371. self, key: _KT, default: Optional[Union[_VT, _T]] = None
  1372. ) -> Union[_VT, _T, None]:
  1373. try:
  1374. return self[key]
  1375. except KeyError:
  1376. return default
  1377. def setdefault(self, key: _KT, default: Optional[_VT] = None) -> _VT:
  1378. # TODO: again, no idea how to create an actual MutableMapping.
  1379. # default must allow None, return type can't include None,
  1380. # the stub explicitly allows for default of None with a cryptic message
  1381. # "This overload should be allowed only if the value type is
  1382. # compatible with None.".
  1383. if key not in self.col:
  1384. self.col[key] = self._create(key, default)
  1385. return default # type: ignore
  1386. else:
  1387. return self[key]
  1388. def keys(self) -> KeysView[_KT]:
  1389. return self.col.keys()
  1390. def items(self) -> ItemsView[_KT, _VT]:
  1391. return ItemsView(self)
  1392. def values(self) -> ValuesView[_VT]:
  1393. return ValuesView(self)
  1394. @overload
  1395. def pop(self, __key: _KT) -> _VT: ...
  1396. @overload
  1397. def pop(
  1398. self, __key: _KT, default: Union[_VT, _T] = ...
  1399. ) -> Union[_VT, _T]: ...
  1400. def pop(self, __key: _KT, *arg: Any, **kw: Any) -> Union[_VT, _T]:
  1401. member = self.col.pop(__key, *arg, **kw)
  1402. return self._get(member)
  1403. def popitem(self) -> Tuple[_KT, _VT]:
  1404. item = self.col.popitem()
  1405. return (item[0], self._get(item[1]))
  1406. @overload
  1407. def update(
  1408. self, __m: SupportsKeysAndGetItem[_KT, _VT], **kwargs: _VT
  1409. ) -> None: ...
  1410. @overload
  1411. def update(
  1412. self, __m: Iterable[tuple[_KT, _VT]], **kwargs: _VT
  1413. ) -> None: ...
  1414. @overload
  1415. def update(self, **kwargs: _VT) -> None: ...
  1416. def update(self, *a: Any, **kw: Any) -> None:
  1417. up: Dict[_KT, _VT] = {}
  1418. up.update(*a, **kw)
  1419. for key, value in up.items():
  1420. self[key] = value
  1421. def _bulk_replace(
  1422. self,
  1423. assoc_proxy: AssociationProxyInstance[Any],
  1424. values: Mapping[_KT, _VT],
  1425. ) -> None:
  1426. existing = set(self)
  1427. constants = existing.intersection(values or ())
  1428. additions = set(values or ()).difference(constants)
  1429. removals = existing.difference(constants)
  1430. for key, member in values.items() or ():
  1431. if key in additions:
  1432. self[key] = member
  1433. elif key in constants:
  1434. self[key] = member
  1435. for key in removals:
  1436. del self[key]
  1437. def copy(self) -> Dict[_KT, _VT]:
  1438. return dict(self.items())
  1439. def __hash__(self) -> NoReturn:
  1440. raise TypeError("%s objects are unhashable" % type(self).__name__)
  1441. if not typing.TYPE_CHECKING:
  1442. for func_name, func in list(locals().items()):
  1443. if (
  1444. callable(func)
  1445. and func.__name__ == func_name
  1446. and not func.__doc__
  1447. and hasattr(dict, func_name)
  1448. ):
  1449. func.__doc__ = getattr(dict, func_name).__doc__
  1450. del func_name, func
  1451. class _AssociationSet(_AssociationSingleItem[_T], MutableSet[_T]):
  1452. """Generic, converting, set-to-set proxy."""
  1453. col: MutableSet[_T]
  1454. def __len__(self) -> int:
  1455. return len(self.col)
  1456. def __bool__(self) -> bool:
  1457. if self.col:
  1458. return True
  1459. else:
  1460. return False
  1461. def __contains__(self, __o: object) -> bool:
  1462. for member in self.col:
  1463. if self._get(member) == __o:
  1464. return True
  1465. return False
  1466. def __iter__(self) -> Iterator[_T]:
  1467. """Iterate over proxied values.
  1468. For the actual domain objects, iterate over .col instead or just use
  1469. the underlying collection directly from its property on the parent.
  1470. """
  1471. for member in self.col:
  1472. yield self._get(member)
  1473. return
  1474. def add(self, __element: _T) -> None:
  1475. if __element not in self:
  1476. self.col.add(self._create(__element))
  1477. # for discard and remove, choosing a more expensive check strategy rather
  1478. # than call self.creator()
  1479. def discard(self, __element: _T) -> None:
  1480. for member in self.col:
  1481. if self._get(member) == __element:
  1482. self.col.discard(member)
  1483. break
  1484. def remove(self, __element: _T) -> None:
  1485. for member in self.col:
  1486. if self._get(member) == __element:
  1487. self.col.discard(member)
  1488. return
  1489. raise KeyError(__element)
  1490. def pop(self) -> _T:
  1491. if not self.col:
  1492. raise KeyError("pop from an empty set")
  1493. member = self.col.pop()
  1494. return self._get(member)
  1495. def update(self, *s: Iterable[_T]) -> None:
  1496. for iterable in s:
  1497. for value in iterable:
  1498. self.add(value)
  1499. def _bulk_replace(self, assoc_proxy: Any, values: Iterable[_T]) -> None:
  1500. existing = set(self)
  1501. constants = existing.intersection(values or ())
  1502. additions = set(values or ()).difference(constants)
  1503. removals = existing.difference(constants)
  1504. appender = self.add
  1505. remover = self.remove
  1506. for member in values or ():
  1507. if member in additions:
  1508. appender(member)
  1509. elif member in constants:
  1510. appender(member)
  1511. for member in removals:
  1512. remover(member)
  1513. def __ior__( # type: ignore
  1514. self, other: AbstractSet[_S]
  1515. ) -> MutableSet[Union[_T, _S]]:
  1516. if not collections._set_binops_check_strict(self, other):
  1517. raise NotImplementedError()
  1518. for value in other:
  1519. self.add(value)
  1520. return self
  1521. def _set(self) -> Set[_T]:
  1522. return set(iter(self))
  1523. def union(self, *s: Iterable[_S]) -> MutableSet[Union[_T, _S]]:
  1524. return set(self).union(*s)
  1525. def __or__(self, __s: AbstractSet[_S]) -> MutableSet[Union[_T, _S]]:
  1526. return self.union(__s)
  1527. def difference(self, *s: Iterable[Any]) -> MutableSet[_T]:
  1528. return set(self).difference(*s)
  1529. def __sub__(self, s: AbstractSet[Any]) -> MutableSet[_T]:
  1530. return self.difference(s)
  1531. def difference_update(self, *s: Iterable[Any]) -> None:
  1532. for other in s:
  1533. for value in other:
  1534. self.discard(value)
  1535. def __isub__(self, s: AbstractSet[Any]) -> Self:
  1536. if not collections._set_binops_check_strict(self, s):
  1537. raise NotImplementedError()
  1538. for value in s:
  1539. self.discard(value)
  1540. return self
  1541. def intersection(self, *s: Iterable[Any]) -> MutableSet[_T]:
  1542. return set(self).intersection(*s)
  1543. def __and__(self, s: AbstractSet[Any]) -> MutableSet[_T]:
  1544. return self.intersection(s)
  1545. def intersection_update(self, *s: Iterable[Any]) -> None:
  1546. for other in s:
  1547. want, have = self.intersection(other), set(self)
  1548. remove, add = have - want, want - have
  1549. for value in remove:
  1550. self.remove(value)
  1551. for value in add:
  1552. self.add(value)
  1553. def __iand__(self, s: AbstractSet[Any]) -> Self:
  1554. if not collections._set_binops_check_strict(self, s):
  1555. raise NotImplementedError()
  1556. want = self.intersection(s)
  1557. have: Set[_T] = set(self)
  1558. remove, add = have - want, want - have
  1559. for value in remove:
  1560. self.remove(value)
  1561. for value in add:
  1562. self.add(value)
  1563. return self
  1564. def symmetric_difference(self, __s: Iterable[_T]) -> MutableSet[_T]:
  1565. return set(self).symmetric_difference(__s)
  1566. def __xor__(self, s: AbstractSet[_S]) -> MutableSet[Union[_T, _S]]:
  1567. return self.symmetric_difference(s)
  1568. def symmetric_difference_update(self, other: Iterable[Any]) -> None:
  1569. want, have = self.symmetric_difference(other), set(self)
  1570. remove, add = have - want, want - have
  1571. for value in remove:
  1572. self.remove(value)
  1573. for value in add:
  1574. self.add(value)
  1575. def __ixor__(self, other: AbstractSet[_S]) -> MutableSet[Union[_T, _S]]: # type: ignore # noqa: E501
  1576. if not collections._set_binops_check_strict(self, other):
  1577. raise NotImplementedError()
  1578. self.symmetric_difference_update(other)
  1579. return self
  1580. def issubset(self, __s: Iterable[Any]) -> bool:
  1581. return set(self).issubset(__s)
  1582. def issuperset(self, __s: Iterable[Any]) -> bool:
  1583. return set(self).issuperset(__s)
  1584. def clear(self) -> None:
  1585. self.col.clear()
  1586. def copy(self) -> AbstractSet[_T]:
  1587. return set(self)
  1588. def __eq__(self, other: object) -> bool:
  1589. return set(self) == other
  1590. def __ne__(self, other: object) -> bool:
  1591. return set(self) != other
  1592. def __lt__(self, other: AbstractSet[Any]) -> bool:
  1593. return set(self) < other
  1594. def __le__(self, other: AbstractSet[Any]) -> bool:
  1595. return set(self) <= other
  1596. def __gt__(self, other: AbstractSet[Any]) -> bool:
  1597. return set(self) > other
  1598. def __ge__(self, other: AbstractSet[Any]) -> bool:
  1599. return set(self) >= other
  1600. def __repr__(self) -> str:
  1601. return repr(set(self))
  1602. def __hash__(self) -> NoReturn:
  1603. raise TypeError("%s objects are unhashable" % type(self).__name__)
  1604. if not typing.TYPE_CHECKING:
  1605. for func_name, func in list(locals().items()):
  1606. if (
  1607. callable(func)
  1608. and func.__name__ == func_name
  1609. and not func.__doc__
  1610. and hasattr(set, func_name)
  1611. ):
  1612. func.__doc__ = getattr(set, func_name).__doc__
  1613. del func_name, func