associationproxy.py 64 KB

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