decl_base.py 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  1. # orm/decl_base.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. """Internal implementation for declarative."""
  8. from __future__ import annotations
  9. import collections
  10. import dataclasses
  11. import re
  12. from typing import Any
  13. from typing import Callable
  14. from typing import cast
  15. from typing import Dict
  16. from typing import Iterable
  17. from typing import List
  18. from typing import Mapping
  19. from typing import NamedTuple
  20. from typing import NoReturn
  21. from typing import Optional
  22. from typing import Sequence
  23. from typing import Tuple
  24. from typing import Type
  25. from typing import TYPE_CHECKING
  26. from typing import TypeVar
  27. from typing import Union
  28. import weakref
  29. from . import attributes
  30. from . import clsregistry
  31. from . import exc as orm_exc
  32. from . import instrumentation
  33. from . import mapperlib
  34. from ._typing import _O
  35. from ._typing import attr_is_internal_proxy
  36. from .attributes import InstrumentedAttribute
  37. from .attributes import QueryableAttribute
  38. from .base import _is_mapped_class
  39. from .base import InspectionAttr
  40. from .descriptor_props import CompositeProperty
  41. from .descriptor_props import SynonymProperty
  42. from .interfaces import _AttributeOptions
  43. from .interfaces import _DCAttributeOptions
  44. from .interfaces import _IntrospectsAnnotations
  45. from .interfaces import _MappedAttribute
  46. from .interfaces import _MapsColumns
  47. from .interfaces import MapperProperty
  48. from .mapper import Mapper
  49. from .properties import ColumnProperty
  50. from .properties import MappedColumn
  51. from .util import _extract_mapped_subtype
  52. from .util import _is_mapped_annotation
  53. from .util import class_mapper
  54. from .util import de_stringify_annotation
  55. from .. import event
  56. from .. import exc
  57. from .. import util
  58. from ..sql import expression
  59. from ..sql.base import _NoArg
  60. from ..sql.schema import Column
  61. from ..sql.schema import Table
  62. from ..util import topological
  63. from ..util.typing import _AnnotationScanType
  64. from ..util.typing import is_fwd_ref
  65. from ..util.typing import is_literal
  66. from ..util.typing import Protocol
  67. from ..util.typing import TypedDict
  68. from ..util.typing import typing_get_args
  69. if TYPE_CHECKING:
  70. from ._typing import _ClassDict
  71. from ._typing import _RegistryType
  72. from .base import Mapped
  73. from .decl_api import declared_attr
  74. from .instrumentation import ClassManager
  75. from ..sql.elements import NamedColumn
  76. from ..sql.schema import MetaData
  77. from ..sql.selectable import FromClause
  78. _T = TypeVar("_T", bound=Any)
  79. _MapperKwArgs = Mapping[str, Any]
  80. _TableArgsType = Union[Tuple[Any, ...], Dict[str, Any]]
  81. class MappedClassProtocol(Protocol[_O]):
  82. """A protocol representing a SQLAlchemy mapped class.
  83. The protocol is generic on the type of class, use
  84. ``MappedClassProtocol[Any]`` to allow any mapped class.
  85. """
  86. __name__: str
  87. __mapper__: Mapper[_O]
  88. __table__: FromClause
  89. def __call__(self, **kw: Any) -> _O:
  90. ...
  91. class _DeclMappedClassProtocol(MappedClassProtocol[_O], Protocol):
  92. "Internal more detailed version of ``MappedClassProtocol``."
  93. metadata: MetaData
  94. __tablename__: str
  95. __mapper_args__: _MapperKwArgs
  96. __table_args__: Optional[_TableArgsType]
  97. _sa_apply_dc_transforms: Optional[_DataclassArguments]
  98. def __declare_first__(self) -> None:
  99. ...
  100. def __declare_last__(self) -> None:
  101. ...
  102. class _DataclassArguments(TypedDict):
  103. init: Union[_NoArg, bool]
  104. repr: Union[_NoArg, bool]
  105. eq: Union[_NoArg, bool]
  106. order: Union[_NoArg, bool]
  107. unsafe_hash: Union[_NoArg, bool]
  108. match_args: Union[_NoArg, bool]
  109. kw_only: Union[_NoArg, bool]
  110. dataclass_callable: Union[_NoArg, Callable[..., Type[Any]]]
  111. def _declared_mapping_info(
  112. cls: Type[Any],
  113. ) -> Optional[Union[_DeferredMapperConfig, Mapper[Any]]]:
  114. # deferred mapping
  115. if _DeferredMapperConfig.has_cls(cls):
  116. return _DeferredMapperConfig.config_for_cls(cls)
  117. # regular mapping
  118. elif _is_mapped_class(cls):
  119. return class_mapper(cls, configure=False)
  120. else:
  121. return None
  122. def _is_supercls_for_inherits(cls: Type[Any]) -> bool:
  123. """return True if this class will be used as a superclass to set in
  124. 'inherits'.
  125. This includes deferred mapper configs that aren't mapped yet, however does
  126. not include classes with _sa_decl_prepare_nocascade (e.g.
  127. ``AbstractConcreteBase``); these concrete-only classes are not set up as
  128. "inherits" until after mappers are configured using
  129. mapper._set_concrete_base()
  130. """
  131. if _DeferredMapperConfig.has_cls(cls):
  132. return not _get_immediate_cls_attr(
  133. cls, "_sa_decl_prepare_nocascade", strict=True
  134. )
  135. # regular mapping
  136. elif _is_mapped_class(cls):
  137. return True
  138. else:
  139. return False
  140. def _resolve_for_abstract_or_classical(cls: Type[Any]) -> Optional[Type[Any]]:
  141. if cls is object:
  142. return None
  143. sup: Optional[Type[Any]]
  144. if cls.__dict__.get("__abstract__", False):
  145. for base_ in cls.__bases__:
  146. sup = _resolve_for_abstract_or_classical(base_)
  147. if sup is not None:
  148. return sup
  149. else:
  150. return None
  151. else:
  152. clsmanager = _dive_for_cls_manager(cls)
  153. if clsmanager:
  154. return clsmanager.class_
  155. else:
  156. return cls
  157. def _get_immediate_cls_attr(
  158. cls: Type[Any], attrname: str, strict: bool = False
  159. ) -> Optional[Any]:
  160. """return an attribute of the class that is either present directly
  161. on the class, e.g. not on a superclass, or is from a superclass but
  162. this superclass is a non-mapped mixin, that is, not a descendant of
  163. the declarative base and is also not classically mapped.
  164. This is used to detect attributes that indicate something about
  165. a mapped class independently from any mapped classes that it may
  166. inherit from.
  167. """
  168. # the rules are different for this name than others,
  169. # make sure we've moved it out. transitional
  170. assert attrname != "__abstract__"
  171. if not issubclass(cls, object):
  172. return None
  173. if attrname in cls.__dict__:
  174. return getattr(cls, attrname)
  175. for base in cls.__mro__[1:]:
  176. _is_classical_inherits = _dive_for_cls_manager(base) is not None
  177. if attrname in base.__dict__ and (
  178. base is cls
  179. or (
  180. (base in cls.__bases__ if strict else True)
  181. and not _is_classical_inherits
  182. )
  183. ):
  184. return getattr(base, attrname)
  185. else:
  186. return None
  187. def _dive_for_cls_manager(cls: Type[_O]) -> Optional[ClassManager[_O]]:
  188. # because the class manager registration is pluggable,
  189. # we need to do the search for every class in the hierarchy,
  190. # rather than just a simple "cls._sa_class_manager"
  191. for base in cls.__mro__:
  192. manager: Optional[ClassManager[_O]] = attributes.opt_manager_of_class(
  193. base
  194. )
  195. if manager:
  196. return manager
  197. return None
  198. def _as_declarative(
  199. registry: _RegistryType, cls: Type[Any], dict_: _ClassDict
  200. ) -> Optional[_MapperConfig]:
  201. # declarative scans the class for attributes. no table or mapper
  202. # args passed separately.
  203. return _MapperConfig.setup_mapping(registry, cls, dict_, None, {})
  204. def _mapper(
  205. registry: _RegistryType,
  206. cls: Type[_O],
  207. table: Optional[FromClause],
  208. mapper_kw: _MapperKwArgs,
  209. ) -> Mapper[_O]:
  210. _ImperativeMapperConfig(registry, cls, table, mapper_kw)
  211. return cast("MappedClassProtocol[_O]", cls).__mapper__
  212. @util.preload_module("sqlalchemy.orm.decl_api")
  213. def _is_declarative_props(obj: Any) -> bool:
  214. _declared_attr_common = util.preloaded.orm_decl_api._declared_attr_common
  215. return isinstance(obj, (_declared_attr_common, util.classproperty))
  216. def _check_declared_props_nocascade(
  217. obj: Any, name: str, cls: Type[_O]
  218. ) -> bool:
  219. if _is_declarative_props(obj):
  220. if getattr(obj, "_cascading", False):
  221. util.warn(
  222. "@declared_attr.cascading is not supported on the %s "
  223. "attribute on class %s. This attribute invokes for "
  224. "subclasses in any case." % (name, cls)
  225. )
  226. return True
  227. else:
  228. return False
  229. class _MapperConfig:
  230. __slots__ = (
  231. "cls",
  232. "classname",
  233. "properties",
  234. "declared_attr_reg",
  235. "__weakref__",
  236. )
  237. cls: Type[Any]
  238. classname: str
  239. properties: util.OrderedDict[
  240. str,
  241. Union[
  242. Sequence[NamedColumn[Any]], NamedColumn[Any], MapperProperty[Any]
  243. ],
  244. ]
  245. declared_attr_reg: Dict[declared_attr[Any], Any]
  246. @classmethod
  247. def setup_mapping(
  248. cls,
  249. registry: _RegistryType,
  250. cls_: Type[_O],
  251. dict_: _ClassDict,
  252. table: Optional[FromClause],
  253. mapper_kw: _MapperKwArgs,
  254. ) -> Optional[_MapperConfig]:
  255. manager = attributes.opt_manager_of_class(cls)
  256. if manager and manager.class_ is cls_:
  257. raise exc.InvalidRequestError(
  258. f"Class {cls!r} already has been instrumented declaratively"
  259. )
  260. if cls_.__dict__.get("__abstract__", False):
  261. return None
  262. defer_map = _get_immediate_cls_attr(
  263. cls_, "_sa_decl_prepare_nocascade", strict=True
  264. ) or hasattr(cls_, "_sa_decl_prepare")
  265. if defer_map:
  266. return _DeferredMapperConfig(
  267. registry, cls_, dict_, table, mapper_kw
  268. )
  269. else:
  270. return _ClassScanMapperConfig(
  271. registry, cls_, dict_, table, mapper_kw
  272. )
  273. def __init__(
  274. self,
  275. registry: _RegistryType,
  276. cls_: Type[Any],
  277. mapper_kw: _MapperKwArgs,
  278. ):
  279. self.cls = util.assert_arg_type(cls_, type, "cls_")
  280. self.classname = cls_.__name__
  281. self.properties = util.OrderedDict()
  282. self.declared_attr_reg = {}
  283. if not mapper_kw.get("non_primary", False):
  284. instrumentation.register_class(
  285. self.cls,
  286. finalize=False,
  287. registry=registry,
  288. declarative_scan=self,
  289. init_method=registry.constructor,
  290. )
  291. else:
  292. manager = attributes.opt_manager_of_class(self.cls)
  293. if not manager or not manager.is_mapped:
  294. raise exc.InvalidRequestError(
  295. "Class %s has no primary mapper configured. Configure "
  296. "a primary mapper first before setting up a non primary "
  297. "Mapper." % self.cls
  298. )
  299. def set_cls_attribute(self, attrname: str, value: _T) -> _T:
  300. manager = instrumentation.manager_of_class(self.cls)
  301. manager.install_member(attrname, value)
  302. return value
  303. def map(self, mapper_kw: _MapperKwArgs = ...) -> Mapper[Any]:
  304. raise NotImplementedError()
  305. def _early_mapping(self, mapper_kw: _MapperKwArgs) -> None:
  306. self.map(mapper_kw)
  307. class _ImperativeMapperConfig(_MapperConfig):
  308. __slots__ = ("local_table", "inherits")
  309. def __init__(
  310. self,
  311. registry: _RegistryType,
  312. cls_: Type[_O],
  313. table: Optional[FromClause],
  314. mapper_kw: _MapperKwArgs,
  315. ):
  316. super().__init__(registry, cls_, mapper_kw)
  317. self.local_table = self.set_cls_attribute("__table__", table)
  318. with mapperlib._CONFIGURE_MUTEX:
  319. if not mapper_kw.get("non_primary", False):
  320. clsregistry.add_class(
  321. self.classname, self.cls, registry._class_registry
  322. )
  323. self._setup_inheritance(mapper_kw)
  324. self._early_mapping(mapper_kw)
  325. def map(self, mapper_kw: _MapperKwArgs = util.EMPTY_DICT) -> Mapper[Any]:
  326. mapper_cls = Mapper
  327. return self.set_cls_attribute(
  328. "__mapper__",
  329. mapper_cls(self.cls, self.local_table, **mapper_kw),
  330. )
  331. def _setup_inheritance(self, mapper_kw: _MapperKwArgs) -> None:
  332. cls = self.cls
  333. inherits = mapper_kw.get("inherits", None)
  334. if inherits is None:
  335. # since we search for classical mappings now, search for
  336. # multiple mapped bases as well and raise an error.
  337. inherits_search = []
  338. for base_ in cls.__bases__:
  339. c = _resolve_for_abstract_or_classical(base_)
  340. if c is None:
  341. continue
  342. if _is_supercls_for_inherits(c) and c not in inherits_search:
  343. inherits_search.append(c)
  344. if inherits_search:
  345. if len(inherits_search) > 1:
  346. raise exc.InvalidRequestError(
  347. "Class %s has multiple mapped bases: %r"
  348. % (cls, inherits_search)
  349. )
  350. inherits = inherits_search[0]
  351. elif isinstance(inherits, Mapper):
  352. inherits = inherits.class_
  353. self.inherits = inherits
  354. class _CollectedAnnotation(NamedTuple):
  355. raw_annotation: _AnnotationScanType
  356. mapped_container: Optional[Type[Mapped[Any]]]
  357. extracted_mapped_annotation: Union[Type[Any], str]
  358. is_dataclass: bool
  359. attr_value: Any
  360. originating_module: str
  361. originating_class: Type[Any]
  362. class _ClassScanMapperConfig(_MapperConfig):
  363. __slots__ = (
  364. "registry",
  365. "clsdict_view",
  366. "collected_attributes",
  367. "collected_annotations",
  368. "local_table",
  369. "persist_selectable",
  370. "declared_columns",
  371. "column_ordering",
  372. "column_copies",
  373. "table_args",
  374. "tablename",
  375. "mapper_args",
  376. "mapper_args_fn",
  377. "inherits",
  378. "single",
  379. "allow_dataclass_fields",
  380. "dataclass_setup_arguments",
  381. "is_dataclass_prior_to_mapping",
  382. "allow_unmapped_annotations",
  383. )
  384. is_deferred = False
  385. registry: _RegistryType
  386. clsdict_view: _ClassDict
  387. collected_annotations: Dict[str, _CollectedAnnotation]
  388. collected_attributes: Dict[str, Any]
  389. local_table: Optional[FromClause]
  390. persist_selectable: Optional[FromClause]
  391. declared_columns: util.OrderedSet[Column[Any]]
  392. column_ordering: Dict[Column[Any], int]
  393. column_copies: Dict[
  394. Union[MappedColumn[Any], Column[Any]],
  395. Union[MappedColumn[Any], Column[Any]],
  396. ]
  397. tablename: Optional[str]
  398. mapper_args: Mapping[str, Any]
  399. table_args: Optional[_TableArgsType]
  400. mapper_args_fn: Optional[Callable[[], Dict[str, Any]]]
  401. inherits: Optional[Type[Any]]
  402. single: bool
  403. is_dataclass_prior_to_mapping: bool
  404. allow_unmapped_annotations: bool
  405. dataclass_setup_arguments: Optional[_DataclassArguments]
  406. """if the class has SQLAlchemy native dataclass parameters, where
  407. we will turn the class into a dataclass within the declarative mapping
  408. process.
  409. """
  410. allow_dataclass_fields: bool
  411. """if true, look for dataclass-processed Field objects on the target
  412. class as well as superclasses and extract ORM mapping directives from
  413. the "metadata" attribute of each Field.
  414. if False, dataclass fields can still be used, however they won't be
  415. mapped.
  416. """
  417. def __init__(
  418. self,
  419. registry: _RegistryType,
  420. cls_: Type[_O],
  421. dict_: _ClassDict,
  422. table: Optional[FromClause],
  423. mapper_kw: _MapperKwArgs,
  424. ):
  425. # grab class dict before the instrumentation manager has been added.
  426. # reduces cycles
  427. self.clsdict_view = (
  428. util.immutabledict(dict_) if dict_ else util.EMPTY_DICT
  429. )
  430. super().__init__(registry, cls_, mapper_kw)
  431. self.registry = registry
  432. self.persist_selectable = None
  433. self.collected_attributes = {}
  434. self.collected_annotations = {}
  435. self.declared_columns = util.OrderedSet()
  436. self.column_ordering = {}
  437. self.column_copies = {}
  438. self.single = False
  439. self.dataclass_setup_arguments = dca = getattr(
  440. self.cls, "_sa_apply_dc_transforms", None
  441. )
  442. self.allow_unmapped_annotations = getattr(
  443. self.cls, "__allow_unmapped__", False
  444. ) or bool(self.dataclass_setup_arguments)
  445. self.is_dataclass_prior_to_mapping = cld = dataclasses.is_dataclass(
  446. cls_
  447. )
  448. sdk = _get_immediate_cls_attr(cls_, "__sa_dataclass_metadata_key__")
  449. # we don't want to consume Field objects from a not-already-dataclass.
  450. # the Field objects won't have their "name" or "type" populated,
  451. # and while it seems like we could just set these on Field as we
  452. # read them, Field is documented as "user read only" and we need to
  453. # stay far away from any off-label use of dataclasses APIs.
  454. if (not cld or dca) and sdk:
  455. raise exc.InvalidRequestError(
  456. "SQLAlchemy mapped dataclasses can't consume mapping "
  457. "information from dataclass.Field() objects if the immediate "
  458. "class is not already a dataclass."
  459. )
  460. # if already a dataclass, and __sa_dataclass_metadata_key__ present,
  461. # then also look inside of dataclass.Field() objects yielded by
  462. # dataclasses.get_fields(cls) when scanning for attributes
  463. self.allow_dataclass_fields = bool(sdk and cld)
  464. self._setup_declared_events()
  465. self._scan_attributes()
  466. self._setup_dataclasses_transforms()
  467. with mapperlib._CONFIGURE_MUTEX:
  468. clsregistry.add_class(
  469. self.classname, self.cls, registry._class_registry
  470. )
  471. self._setup_inheriting_mapper(mapper_kw)
  472. self._extract_mappable_attributes()
  473. self._extract_declared_columns()
  474. self._setup_table(table)
  475. self._setup_inheriting_columns(mapper_kw)
  476. self._early_mapping(mapper_kw)
  477. def _setup_declared_events(self) -> None:
  478. if _get_immediate_cls_attr(self.cls, "__declare_last__"):
  479. @event.listens_for(Mapper, "after_configured")
  480. def after_configured() -> None:
  481. cast(
  482. "_DeclMappedClassProtocol[Any]", self.cls
  483. ).__declare_last__()
  484. if _get_immediate_cls_attr(self.cls, "__declare_first__"):
  485. @event.listens_for(Mapper, "before_configured")
  486. def before_configured() -> None:
  487. cast(
  488. "_DeclMappedClassProtocol[Any]", self.cls
  489. ).__declare_first__()
  490. def _cls_attr_override_checker(
  491. self, cls: Type[_O]
  492. ) -> Callable[[str, Any], bool]:
  493. """Produce a function that checks if a class has overridden an
  494. attribute, taking SQLAlchemy-enabled dataclass fields into account.
  495. """
  496. if self.allow_dataclass_fields:
  497. sa_dataclass_metadata_key = _get_immediate_cls_attr(
  498. cls, "__sa_dataclass_metadata_key__"
  499. )
  500. else:
  501. sa_dataclass_metadata_key = None
  502. if not sa_dataclass_metadata_key:
  503. def attribute_is_overridden(key: str, obj: Any) -> bool:
  504. return getattr(cls, key, obj) is not obj
  505. else:
  506. all_datacls_fields = {
  507. f.name: f.metadata[sa_dataclass_metadata_key]
  508. for f in util.dataclass_fields(cls)
  509. if sa_dataclass_metadata_key in f.metadata
  510. }
  511. local_datacls_fields = {
  512. f.name: f.metadata[sa_dataclass_metadata_key]
  513. for f in util.local_dataclass_fields(cls)
  514. if sa_dataclass_metadata_key in f.metadata
  515. }
  516. absent = object()
  517. def attribute_is_overridden(key: str, obj: Any) -> bool:
  518. if _is_declarative_props(obj):
  519. obj = obj.fget
  520. # this function likely has some failure modes still if
  521. # someone is doing a deep mixing of the same attribute
  522. # name as plain Python attribute vs. dataclass field.
  523. ret = local_datacls_fields.get(key, absent)
  524. if _is_declarative_props(ret):
  525. ret = ret.fget
  526. if ret is obj:
  527. return False
  528. elif ret is not absent:
  529. return True
  530. all_field = all_datacls_fields.get(key, absent)
  531. ret = getattr(cls, key, obj)
  532. if ret is obj:
  533. return False
  534. # for dataclasses, this could be the
  535. # 'default' of the field. so filter more specifically
  536. # for an already-mapped InstrumentedAttribute
  537. if ret is not absent and isinstance(
  538. ret, InstrumentedAttribute
  539. ):
  540. return True
  541. if all_field is obj:
  542. return False
  543. elif all_field is not absent:
  544. return True
  545. # can't find another attribute
  546. return False
  547. return attribute_is_overridden
  548. _include_dunders = {
  549. "__table__",
  550. "__mapper_args__",
  551. "__tablename__",
  552. "__table_args__",
  553. }
  554. _match_exclude_dunders = re.compile(r"^(?:_sa_|__)")
  555. def _cls_attr_resolver(
  556. self, cls: Type[Any]
  557. ) -> Callable[[], Iterable[Tuple[str, Any, Any, bool]]]:
  558. """produce a function to iterate the "attributes" of a class
  559. which we want to consider for mapping, adjusting for SQLAlchemy fields
  560. embedded in dataclass fields.
  561. """
  562. cls_annotations = util.get_annotations(cls)
  563. cls_vars = vars(cls)
  564. _include_dunders = self._include_dunders
  565. _match_exclude_dunders = self._match_exclude_dunders
  566. names = [
  567. n
  568. for n in util.merge_lists_w_ordering(
  569. list(cls_vars), list(cls_annotations)
  570. )
  571. if not _match_exclude_dunders.match(n) or n in _include_dunders
  572. ]
  573. if self.allow_dataclass_fields:
  574. sa_dataclass_metadata_key: Optional[str] = _get_immediate_cls_attr(
  575. cls, "__sa_dataclass_metadata_key__"
  576. )
  577. else:
  578. sa_dataclass_metadata_key = None
  579. if not sa_dataclass_metadata_key:
  580. def local_attributes_for_class() -> (
  581. Iterable[Tuple[str, Any, Any, bool]]
  582. ):
  583. return (
  584. (
  585. name,
  586. cls_vars.get(name),
  587. cls_annotations.get(name),
  588. False,
  589. )
  590. for name in names
  591. )
  592. else:
  593. dataclass_fields = {
  594. field.name: field for field in util.local_dataclass_fields(cls)
  595. }
  596. fixed_sa_dataclass_metadata_key = sa_dataclass_metadata_key
  597. def local_attributes_for_class() -> (
  598. Iterable[Tuple[str, Any, Any, bool]]
  599. ):
  600. for name in names:
  601. field = dataclass_fields.get(name, None)
  602. if field and sa_dataclass_metadata_key in field.metadata:
  603. yield field.name, _as_dc_declaredattr(
  604. field.metadata, fixed_sa_dataclass_metadata_key
  605. ), cls_annotations.get(field.name), True
  606. else:
  607. yield name, cls_vars.get(name), cls_annotations.get(
  608. name
  609. ), False
  610. return local_attributes_for_class
  611. def _scan_attributes(self) -> None:
  612. cls = self.cls
  613. cls_as_Decl = cast("_DeclMappedClassProtocol[Any]", cls)
  614. clsdict_view = self.clsdict_view
  615. collected_attributes = self.collected_attributes
  616. column_copies = self.column_copies
  617. _include_dunders = self._include_dunders
  618. mapper_args_fn = None
  619. table_args = inherited_table_args = None
  620. tablename = None
  621. fixed_table = "__table__" in clsdict_view
  622. attribute_is_overridden = self._cls_attr_override_checker(self.cls)
  623. bases = []
  624. for base in cls.__mro__:
  625. # collect bases and make sure standalone columns are copied
  626. # to be the column they will ultimately be on the class,
  627. # so that declared_attr functions use the right columns.
  628. # need to do this all the way up the hierarchy first
  629. # (see #8190)
  630. class_mapped = base is not cls and _is_supercls_for_inherits(base)
  631. local_attributes_for_class = self._cls_attr_resolver(base)
  632. if not class_mapped and base is not cls:
  633. locally_collected_columns = self._produce_column_copies(
  634. local_attributes_for_class,
  635. attribute_is_overridden,
  636. fixed_table,
  637. base,
  638. )
  639. else:
  640. locally_collected_columns = {}
  641. bases.append(
  642. (
  643. base,
  644. class_mapped,
  645. local_attributes_for_class,
  646. locally_collected_columns,
  647. )
  648. )
  649. for (
  650. base,
  651. class_mapped,
  652. local_attributes_for_class,
  653. locally_collected_columns,
  654. ) in bases:
  655. # this transfer can also take place as we scan each name
  656. # for finer-grained control of how collected_attributes is
  657. # populated, as this is what impacts column ordering.
  658. # however it's simpler to get it out of the way here.
  659. collected_attributes.update(locally_collected_columns)
  660. for (
  661. name,
  662. obj,
  663. annotation,
  664. is_dataclass_field,
  665. ) in local_attributes_for_class():
  666. if name in _include_dunders:
  667. if name == "__mapper_args__":
  668. check_decl = _check_declared_props_nocascade(
  669. obj, name, cls
  670. )
  671. if not mapper_args_fn and (
  672. not class_mapped or check_decl
  673. ):
  674. # don't even invoke __mapper_args__ until
  675. # after we've determined everything about the
  676. # mapped table.
  677. # make a copy of it so a class-level dictionary
  678. # is not overwritten when we update column-based
  679. # arguments.
  680. def _mapper_args_fn() -> Dict[str, Any]:
  681. return dict(cls_as_Decl.__mapper_args__)
  682. mapper_args_fn = _mapper_args_fn
  683. elif name == "__tablename__":
  684. check_decl = _check_declared_props_nocascade(
  685. obj, name, cls
  686. )
  687. if not tablename and (not class_mapped or check_decl):
  688. tablename = cls_as_Decl.__tablename__
  689. elif name == "__table_args__":
  690. check_decl = _check_declared_props_nocascade(
  691. obj, name, cls
  692. )
  693. if not table_args and (not class_mapped or check_decl):
  694. table_args = cls_as_Decl.__table_args__
  695. if not isinstance(
  696. table_args, (tuple, dict, type(None))
  697. ):
  698. raise exc.ArgumentError(
  699. "__table_args__ value must be a tuple, "
  700. "dict, or None"
  701. )
  702. if base is not cls:
  703. inherited_table_args = True
  704. else:
  705. # skip all other dunder names, which at the moment
  706. # should only be __table__
  707. continue
  708. elif class_mapped:
  709. if _is_declarative_props(obj) and not obj._quiet:
  710. util.warn(
  711. "Regular (i.e. not __special__) "
  712. "attribute '%s.%s' uses @declared_attr, "
  713. "but owning class %s is mapped - "
  714. "not applying to subclass %s."
  715. % (base.__name__, name, base, cls)
  716. )
  717. continue
  718. elif base is not cls:
  719. # we're a mixin, abstract base, or something that is
  720. # acting like that for now.
  721. if isinstance(obj, (Column, MappedColumn)):
  722. # already copied columns to the mapped class.
  723. continue
  724. elif isinstance(obj, MapperProperty):
  725. raise exc.InvalidRequestError(
  726. "Mapper properties (i.e. deferred,"
  727. "column_property(), relationship(), etc.) must "
  728. "be declared as @declared_attr callables "
  729. "on declarative mixin classes. For dataclass "
  730. "field() objects, use a lambda:"
  731. )
  732. elif _is_declarative_props(obj):
  733. # tried to get overloads to tell this to
  734. # pylance, no luck
  735. assert obj is not None
  736. if obj._cascading:
  737. if name in clsdict_view:
  738. # unfortunately, while we can use the user-
  739. # defined attribute here to allow a clean
  740. # override, if there's another
  741. # subclass below then it still tries to use
  742. # this. not sure if there is enough
  743. # information here to add this as a feature
  744. # later on.
  745. util.warn(
  746. "Attribute '%s' on class %s cannot be "
  747. "processed due to "
  748. "@declared_attr.cascading; "
  749. "skipping" % (name, cls)
  750. )
  751. collected_attributes[name] = column_copies[
  752. obj
  753. ] = ret = obj.__get__(obj, cls)
  754. setattr(cls, name, ret)
  755. else:
  756. if is_dataclass_field:
  757. # access attribute using normal class access
  758. # first, to see if it's been mapped on a
  759. # superclass. note if the dataclasses.field()
  760. # has "default", this value can be anything.
  761. ret = getattr(cls, name, None)
  762. # so, if it's anything that's not ORM
  763. # mapped, assume we should invoke the
  764. # declared_attr
  765. if not isinstance(ret, InspectionAttr):
  766. ret = obj.fget()
  767. else:
  768. # access attribute using normal class access.
  769. # if the declared attr already took place
  770. # on a superclass that is mapped, then
  771. # this is no longer a declared_attr, it will
  772. # be the InstrumentedAttribute
  773. ret = getattr(cls, name)
  774. # correct for proxies created from hybrid_property
  775. # or similar. note there is no known case that
  776. # produces nested proxies, so we are only
  777. # looking one level deep right now.
  778. if (
  779. isinstance(ret, InspectionAttr)
  780. and attr_is_internal_proxy(ret)
  781. and not isinstance(
  782. ret.original_property, MapperProperty
  783. )
  784. ):
  785. ret = ret.descriptor
  786. collected_attributes[name] = column_copies[
  787. obj
  788. ] = ret
  789. if (
  790. isinstance(ret, (Column, MapperProperty))
  791. and ret.doc is None
  792. ):
  793. ret.doc = obj.__doc__
  794. self._collect_annotation(
  795. name,
  796. obj._collect_return_annotation(),
  797. base,
  798. True,
  799. obj,
  800. )
  801. elif _is_mapped_annotation(annotation, cls, base):
  802. # Mapped annotation without any object.
  803. # product_column_copies should have handled this.
  804. # if future support for other MapperProperty,
  805. # then test if this name is already handled and
  806. # otherwise proceed to generate.
  807. if not fixed_table:
  808. assert (
  809. name in collected_attributes
  810. or attribute_is_overridden(name, None)
  811. )
  812. continue
  813. else:
  814. # here, the attribute is some other kind of
  815. # property that we assume is not part of the
  816. # declarative mapping. however, check for some
  817. # more common mistakes
  818. self._warn_for_decl_attributes(base, name, obj)
  819. elif is_dataclass_field and (
  820. name not in clsdict_view or clsdict_view[name] is not obj
  821. ):
  822. # here, we are definitely looking at the target class
  823. # and not a superclass. this is currently a
  824. # dataclass-only path. if the name is only
  825. # a dataclass field and isn't in local cls.__dict__,
  826. # put the object there.
  827. # assert that the dataclass-enabled resolver agrees
  828. # with what we are seeing
  829. assert not attribute_is_overridden(name, obj)
  830. if _is_declarative_props(obj):
  831. obj = obj.fget()
  832. collected_attributes[name] = obj
  833. self._collect_annotation(
  834. name, annotation, base, False, obj
  835. )
  836. else:
  837. collected_annotation = self._collect_annotation(
  838. name, annotation, base, None, obj
  839. )
  840. is_mapped = (
  841. collected_annotation is not None
  842. and collected_annotation.mapped_container is not None
  843. )
  844. generated_obj = (
  845. collected_annotation.attr_value
  846. if collected_annotation is not None
  847. else obj
  848. )
  849. if obj is None and not fixed_table and is_mapped:
  850. collected_attributes[name] = (
  851. generated_obj
  852. if generated_obj is not None
  853. else MappedColumn()
  854. )
  855. elif name in clsdict_view:
  856. collected_attributes[name] = obj
  857. # else if the name is not in the cls.__dict__,
  858. # don't collect it as an attribute.
  859. # we will see the annotation only, which is meaningful
  860. # both for mapping and dataclasses setup
  861. if inherited_table_args and not tablename:
  862. table_args = None
  863. self.table_args = table_args
  864. self.tablename = tablename
  865. self.mapper_args_fn = mapper_args_fn
  866. def _setup_dataclasses_transforms(self) -> None:
  867. dataclass_setup_arguments = self.dataclass_setup_arguments
  868. if not dataclass_setup_arguments:
  869. return
  870. # can't use is_dataclass since it uses hasattr
  871. if "__dataclass_fields__" in self.cls.__dict__:
  872. raise exc.InvalidRequestError(
  873. f"Class {self.cls} is already a dataclass; ensure that "
  874. "base classes / decorator styles of establishing dataclasses "
  875. "are not being mixed. "
  876. "This can happen if a class that inherits from "
  877. "'MappedAsDataclass', even indirectly, is been mapped with "
  878. "'@registry.mapped_as_dataclass'"
  879. )
  880. warn_for_non_dc_attrs = collections.defaultdict(list)
  881. def _allow_dataclass_field(
  882. key: str, originating_class: Type[Any]
  883. ) -> bool:
  884. if (
  885. originating_class is not self.cls
  886. and "__dataclass_fields__" not in originating_class.__dict__
  887. ):
  888. warn_for_non_dc_attrs[originating_class].append(key)
  889. return True
  890. manager = instrumentation.manager_of_class(self.cls)
  891. assert manager is not None
  892. field_list = [
  893. _AttributeOptions._get_arguments_for_make_dataclass(
  894. key,
  895. anno,
  896. mapped_container,
  897. self.collected_attributes.get(key, _NoArg.NO_ARG),
  898. )
  899. for key, anno, mapped_container in (
  900. (
  901. key,
  902. mapped_anno if mapped_anno else raw_anno,
  903. mapped_container,
  904. )
  905. for key, (
  906. raw_anno,
  907. mapped_container,
  908. mapped_anno,
  909. is_dc,
  910. attr_value,
  911. originating_module,
  912. originating_class,
  913. ) in self.collected_annotations.items()
  914. if _allow_dataclass_field(key, originating_class)
  915. and (
  916. key not in self.collected_attributes
  917. # issue #9226; check for attributes that we've collected
  918. # which are already instrumented, which we would assume
  919. # mean we are in an ORM inheritance mapping and this
  920. # attribute is already mapped on the superclass. Under
  921. # no circumstance should any QueryableAttribute be sent to
  922. # the dataclass() function; anything that's mapped should
  923. # be Field and that's it
  924. or not isinstance(
  925. self.collected_attributes[key], QueryableAttribute
  926. )
  927. )
  928. )
  929. ]
  930. if warn_for_non_dc_attrs:
  931. for (
  932. originating_class,
  933. non_dc_attrs,
  934. ) in warn_for_non_dc_attrs.items():
  935. util.warn_deprecated(
  936. f"When transforming {self.cls} to a dataclass, "
  937. f"attribute(s) "
  938. f"{', '.join(repr(key) for key in non_dc_attrs)} "
  939. f"originates from superclass "
  940. f"{originating_class}, which is not a dataclass. This "
  941. f"usage is deprecated and will raise an error in "
  942. f"SQLAlchemy 2.1. When declaring SQLAlchemy Declarative "
  943. f"Dataclasses, ensure that all mixin classes and other "
  944. f"superclasses which include attributes are also a "
  945. f"subclass of MappedAsDataclass.",
  946. "2.0",
  947. code="dcmx",
  948. )
  949. annotations = {}
  950. defaults = {}
  951. for item in field_list:
  952. if len(item) == 2:
  953. name, tp = item
  954. elif len(item) == 3:
  955. name, tp, spec = item
  956. defaults[name] = spec
  957. else:
  958. assert False
  959. annotations[name] = tp
  960. for k, v in defaults.items():
  961. setattr(self.cls, k, v)
  962. self._apply_dataclasses_to_any_class(
  963. dataclass_setup_arguments, self.cls, annotations
  964. )
  965. @classmethod
  966. def _update_annotations_for_non_mapped_class(
  967. cls, klass: Type[_O]
  968. ) -> Mapping[str, _AnnotationScanType]:
  969. cls_annotations = util.get_annotations(klass)
  970. new_anno = {}
  971. for name, annotation in cls_annotations.items():
  972. if _is_mapped_annotation(annotation, klass, klass):
  973. extracted = _extract_mapped_subtype(
  974. annotation,
  975. klass,
  976. klass.__module__,
  977. name,
  978. type(None),
  979. required=False,
  980. is_dataclass_field=False,
  981. expect_mapped=False,
  982. )
  983. if extracted:
  984. inner, _ = extracted
  985. new_anno[name] = inner
  986. else:
  987. new_anno[name] = annotation
  988. return new_anno
  989. @classmethod
  990. def _apply_dataclasses_to_any_class(
  991. cls,
  992. dataclass_setup_arguments: _DataclassArguments,
  993. klass: Type[_O],
  994. use_annotations: Mapping[str, _AnnotationScanType],
  995. ) -> None:
  996. cls._assert_dc_arguments(dataclass_setup_arguments)
  997. dataclass_callable = dataclass_setup_arguments["dataclass_callable"]
  998. if dataclass_callable is _NoArg.NO_ARG:
  999. dataclass_callable = dataclasses.dataclass
  1000. restored: Optional[Any]
  1001. if use_annotations:
  1002. # apply constructed annotations that should look "normal" to a
  1003. # dataclasses callable, based on the fields present. This
  1004. # means remove the Mapped[] container and ensure all Field
  1005. # entries have an annotation
  1006. restored = getattr(klass, "__annotations__", None)
  1007. klass.__annotations__ = cast("Dict[str, Any]", use_annotations)
  1008. else:
  1009. restored = None
  1010. try:
  1011. dataclass_callable(
  1012. klass,
  1013. **{
  1014. k: v
  1015. for k, v in dataclass_setup_arguments.items()
  1016. if v is not _NoArg.NO_ARG and k != "dataclass_callable"
  1017. },
  1018. )
  1019. except (TypeError, ValueError) as ex:
  1020. raise exc.InvalidRequestError(
  1021. f"Python dataclasses error encountered when creating "
  1022. f"dataclass for {klass.__name__!r}: "
  1023. f"{ex!r}. Please refer to Python dataclasses "
  1024. "documentation for additional information.",
  1025. code="dcte",
  1026. ) from ex
  1027. finally:
  1028. # restore original annotations outside of the dataclasses
  1029. # process; for mixins and __abstract__ superclasses, SQLAlchemy
  1030. # Declarative will need to see the Mapped[] container inside the
  1031. # annotations in order to map subclasses
  1032. if use_annotations:
  1033. if restored is None:
  1034. del klass.__annotations__
  1035. else:
  1036. klass.__annotations__ = restored
  1037. @classmethod
  1038. def _assert_dc_arguments(cls, arguments: _DataclassArguments) -> None:
  1039. allowed = {
  1040. "init",
  1041. "repr",
  1042. "order",
  1043. "eq",
  1044. "unsafe_hash",
  1045. "kw_only",
  1046. "match_args",
  1047. "dataclass_callable",
  1048. }
  1049. disallowed_args = set(arguments).difference(allowed)
  1050. if disallowed_args:
  1051. msg = ", ".join(f"{arg!r}" for arg in sorted(disallowed_args))
  1052. raise exc.ArgumentError(
  1053. f"Dataclass argument(s) {msg} are not accepted"
  1054. )
  1055. def _collect_annotation(
  1056. self,
  1057. name: str,
  1058. raw_annotation: _AnnotationScanType,
  1059. originating_class: Type[Any],
  1060. expect_mapped: Optional[bool],
  1061. attr_value: Any,
  1062. ) -> Optional[_CollectedAnnotation]:
  1063. if name in self.collected_annotations:
  1064. return self.collected_annotations[name]
  1065. if raw_annotation is None:
  1066. return None
  1067. is_dataclass = self.is_dataclass_prior_to_mapping
  1068. allow_unmapped = self.allow_unmapped_annotations
  1069. if expect_mapped is None:
  1070. is_dataclass_field = isinstance(attr_value, dataclasses.Field)
  1071. expect_mapped = (
  1072. not is_dataclass_field
  1073. and not allow_unmapped
  1074. and (
  1075. attr_value is None
  1076. or isinstance(attr_value, _MappedAttribute)
  1077. )
  1078. )
  1079. else:
  1080. is_dataclass_field = False
  1081. is_dataclass_field = False
  1082. extracted = _extract_mapped_subtype(
  1083. raw_annotation,
  1084. self.cls,
  1085. originating_class.__module__,
  1086. name,
  1087. type(attr_value),
  1088. required=False,
  1089. is_dataclass_field=is_dataclass_field,
  1090. expect_mapped=expect_mapped
  1091. and not is_dataclass, # self.allow_dataclass_fields,
  1092. )
  1093. if extracted is None:
  1094. # ClassVar can come out here
  1095. return None
  1096. extracted_mapped_annotation, mapped_container = extracted
  1097. if attr_value is None and not is_literal(extracted_mapped_annotation):
  1098. for elem in typing_get_args(extracted_mapped_annotation):
  1099. if isinstance(elem, str) or is_fwd_ref(
  1100. elem, check_generic=True
  1101. ):
  1102. elem = de_stringify_annotation(
  1103. self.cls,
  1104. elem,
  1105. originating_class.__module__,
  1106. include_generic=True,
  1107. )
  1108. # look in Annotated[...] for an ORM construct,
  1109. # such as Annotated[int, mapped_column(primary_key=True)]
  1110. if isinstance(elem, _IntrospectsAnnotations):
  1111. attr_value = elem.found_in_pep593_annotated()
  1112. self.collected_annotations[name] = ca = _CollectedAnnotation(
  1113. raw_annotation,
  1114. mapped_container,
  1115. extracted_mapped_annotation,
  1116. is_dataclass,
  1117. attr_value,
  1118. originating_class.__module__,
  1119. originating_class,
  1120. )
  1121. return ca
  1122. def _warn_for_decl_attributes(
  1123. self, cls: Type[Any], key: str, c: Any
  1124. ) -> None:
  1125. if isinstance(c, expression.ColumnElement):
  1126. util.warn(
  1127. f"Attribute '{key}' on class {cls} appears to "
  1128. "be a non-schema SQLAlchemy expression "
  1129. "object; this won't be part of the declarative mapping. "
  1130. "To map arbitrary expressions, use ``column_property()`` "
  1131. "or a similar function such as ``deferred()``, "
  1132. "``query_expression()`` etc. "
  1133. )
  1134. def _produce_column_copies(
  1135. self,
  1136. attributes_for_class: Callable[
  1137. [], Iterable[Tuple[str, Any, Any, bool]]
  1138. ],
  1139. attribute_is_overridden: Callable[[str, Any], bool],
  1140. fixed_table: bool,
  1141. originating_class: Type[Any],
  1142. ) -> Dict[str, Union[Column[Any], MappedColumn[Any]]]:
  1143. cls = self.cls
  1144. dict_ = self.clsdict_view
  1145. locally_collected_attributes = {}
  1146. column_copies = self.column_copies
  1147. # copy mixin columns to the mapped class
  1148. for name, obj, annotation, is_dataclass in attributes_for_class():
  1149. if (
  1150. not fixed_table
  1151. and obj is None
  1152. and _is_mapped_annotation(annotation, cls, originating_class)
  1153. ):
  1154. # obj is None means this is the annotation only path
  1155. if attribute_is_overridden(name, obj):
  1156. # perform same "overridden" check as we do for
  1157. # Column/MappedColumn, this is how a mixin col is not
  1158. # applied to an inherited subclass that does not have
  1159. # the mixin. the anno-only path added here for
  1160. # #9564
  1161. continue
  1162. collected_annotation = self._collect_annotation(
  1163. name, annotation, originating_class, True, obj
  1164. )
  1165. obj = (
  1166. collected_annotation.attr_value
  1167. if collected_annotation is not None
  1168. else obj
  1169. )
  1170. if obj is None:
  1171. obj = MappedColumn()
  1172. locally_collected_attributes[name] = obj
  1173. setattr(cls, name, obj)
  1174. elif isinstance(obj, (Column, MappedColumn)):
  1175. if attribute_is_overridden(name, obj):
  1176. # if column has been overridden
  1177. # (like by the InstrumentedAttribute of the
  1178. # superclass), skip. don't collect the annotation
  1179. # either (issue #8718)
  1180. continue
  1181. collected_annotation = self._collect_annotation(
  1182. name, annotation, originating_class, True, obj
  1183. )
  1184. obj = (
  1185. collected_annotation.attr_value
  1186. if collected_annotation is not None
  1187. else obj
  1188. )
  1189. if name not in dict_ and not (
  1190. "__table__" in dict_
  1191. and (getattr(obj, "name", None) or name)
  1192. in dict_["__table__"].c
  1193. ):
  1194. if obj.foreign_keys:
  1195. for fk in obj.foreign_keys:
  1196. if (
  1197. fk._table_column is not None
  1198. and fk._table_column.table is None
  1199. ):
  1200. raise exc.InvalidRequestError(
  1201. "Columns with foreign keys to "
  1202. "non-table-bound "
  1203. "columns must be declared as "
  1204. "@declared_attr callables "
  1205. "on declarative mixin classes. "
  1206. "For dataclass "
  1207. "field() objects, use a lambda:."
  1208. )
  1209. column_copies[obj] = copy_ = obj._copy()
  1210. locally_collected_attributes[name] = copy_
  1211. setattr(cls, name, copy_)
  1212. return locally_collected_attributes
  1213. def _extract_mappable_attributes(self) -> None:
  1214. cls = self.cls
  1215. collected_attributes = self.collected_attributes
  1216. our_stuff = self.properties
  1217. _include_dunders = self._include_dunders
  1218. late_mapped = _get_immediate_cls_attr(
  1219. cls, "_sa_decl_prepare_nocascade", strict=True
  1220. )
  1221. allow_unmapped_annotations = self.allow_unmapped_annotations
  1222. expect_annotations_wo_mapped = (
  1223. allow_unmapped_annotations or self.is_dataclass_prior_to_mapping
  1224. )
  1225. look_for_dataclass_things = bool(self.dataclass_setup_arguments)
  1226. for k in list(collected_attributes):
  1227. if k in _include_dunders:
  1228. continue
  1229. value = collected_attributes[k]
  1230. if _is_declarative_props(value):
  1231. # @declared_attr in collected_attributes only occurs here for a
  1232. # @declared_attr that's directly on the mapped class;
  1233. # for a mixin, these have already been evaluated
  1234. if value._cascading:
  1235. util.warn(
  1236. "Use of @declared_attr.cascading only applies to "
  1237. "Declarative 'mixin' and 'abstract' classes. "
  1238. "Currently, this flag is ignored on mapped class "
  1239. "%s" % self.cls
  1240. )
  1241. value = getattr(cls, k)
  1242. elif (
  1243. isinstance(value, QueryableAttribute)
  1244. and value.class_ is not cls
  1245. and value.key != k
  1246. ):
  1247. # detect a QueryableAttribute that's already mapped being
  1248. # assigned elsewhere in userland, turn into a synonym()
  1249. value = SynonymProperty(value.key)
  1250. setattr(cls, k, value)
  1251. if (
  1252. isinstance(value, tuple)
  1253. and len(value) == 1
  1254. and isinstance(value[0], (Column, _MappedAttribute))
  1255. ):
  1256. util.warn(
  1257. "Ignoring declarative-like tuple value of attribute "
  1258. "'%s': possibly a copy-and-paste error with a comma "
  1259. "accidentally placed at the end of the line?" % k
  1260. )
  1261. continue
  1262. elif look_for_dataclass_things and isinstance(
  1263. value, dataclasses.Field
  1264. ):
  1265. # we collected a dataclass Field; dataclasses would have
  1266. # set up the correct state on the class
  1267. continue
  1268. elif not isinstance(value, (Column, _DCAttributeOptions)):
  1269. # using @declared_attr for some object that
  1270. # isn't Column/MapperProperty/_DCAttributeOptions; remove
  1271. # from the clsdict_view
  1272. # and place the evaluated value onto the class.
  1273. collected_attributes.pop(k)
  1274. self._warn_for_decl_attributes(cls, k, value)
  1275. if not late_mapped:
  1276. setattr(cls, k, value)
  1277. continue
  1278. # we expect to see the name 'metadata' in some valid cases;
  1279. # however at this point we see it's assigned to something trying
  1280. # to be mapped, so raise for that.
  1281. # TODO: should "registry" here be also? might be too late
  1282. # to change that now (2.0 betas)
  1283. elif k in ("metadata",):
  1284. raise exc.InvalidRequestError(
  1285. f"Attribute name '{k}' is reserved when using the "
  1286. "Declarative API."
  1287. )
  1288. elif isinstance(value, Column):
  1289. _undefer_column_name(
  1290. k, self.column_copies.get(value, value) # type: ignore
  1291. )
  1292. else:
  1293. if isinstance(value, _IntrospectsAnnotations):
  1294. (
  1295. annotation,
  1296. mapped_container,
  1297. extracted_mapped_annotation,
  1298. is_dataclass,
  1299. attr_value,
  1300. originating_module,
  1301. originating_class,
  1302. ) = self.collected_annotations.get(
  1303. k, (None, None, None, False, None, None, None)
  1304. )
  1305. # issue #8692 - don't do any annotation interpretation if
  1306. # an annotation were present and a container such as
  1307. # Mapped[] etc. were not used. If annotation is None,
  1308. # do declarative_scan so that the property can raise
  1309. # for required
  1310. if (
  1311. mapped_container is not None
  1312. or annotation is None
  1313. # issue #10516: need to do declarative_scan even with
  1314. # a non-Mapped annotation if we are doing
  1315. # __allow_unmapped__, for things like col.name
  1316. # assignment
  1317. or allow_unmapped_annotations
  1318. ):
  1319. try:
  1320. value.declarative_scan(
  1321. self,
  1322. self.registry,
  1323. cls,
  1324. originating_module,
  1325. k,
  1326. mapped_container,
  1327. annotation,
  1328. extracted_mapped_annotation,
  1329. is_dataclass,
  1330. )
  1331. except NameError as ne:
  1332. raise exc.ArgumentError(
  1333. f"Could not resolve all types within mapped "
  1334. f'annotation: "{annotation}". Ensure all '
  1335. f"types are written correctly and are "
  1336. f"imported within the module in use."
  1337. ) from ne
  1338. else:
  1339. # assert that we were expecting annotations
  1340. # without Mapped[] were going to be passed.
  1341. # otherwise an error should have been raised
  1342. # by util._extract_mapped_subtype before we got here.
  1343. assert expect_annotations_wo_mapped
  1344. if isinstance(value, _DCAttributeOptions):
  1345. if (
  1346. value._has_dataclass_arguments
  1347. and not look_for_dataclass_things
  1348. ):
  1349. if isinstance(value, MapperProperty):
  1350. argnames = [
  1351. "init",
  1352. "default_factory",
  1353. "repr",
  1354. "default",
  1355. ]
  1356. else:
  1357. argnames = ["init", "default_factory", "repr"]
  1358. args = {
  1359. a
  1360. for a in argnames
  1361. if getattr(
  1362. value._attribute_options, f"dataclasses_{a}"
  1363. )
  1364. is not _NoArg.NO_ARG
  1365. }
  1366. raise exc.ArgumentError(
  1367. f"Attribute '{k}' on class {cls} includes "
  1368. f"dataclasses argument(s): "
  1369. f"{', '.join(sorted(repr(a) for a in args))} but "
  1370. f"class does not specify "
  1371. "SQLAlchemy native dataclass configuration."
  1372. )
  1373. if not isinstance(value, (MapperProperty, _MapsColumns)):
  1374. # filter for _DCAttributeOptions objects that aren't
  1375. # MapperProperty / mapped_column(). Currently this
  1376. # includes AssociationProxy. pop it from the things
  1377. # we're going to map and set it up as a descriptor
  1378. # on the class.
  1379. collected_attributes.pop(k)
  1380. # Assoc Prox (or other descriptor object that may
  1381. # use _DCAttributeOptions) is usually here, except if
  1382. # 1. we're a
  1383. # dataclass, dataclasses would have removed the
  1384. # attr here or 2. assoc proxy is coming from a
  1385. # superclass, we want it to be direct here so it
  1386. # tracks state or 3. assoc prox comes from
  1387. # declared_attr, uncommon case
  1388. setattr(cls, k, value)
  1389. continue
  1390. our_stuff[k] = value
  1391. def _extract_declared_columns(self) -> None:
  1392. our_stuff = self.properties
  1393. # extract columns from the class dict
  1394. declared_columns = self.declared_columns
  1395. column_ordering = self.column_ordering
  1396. name_to_prop_key = collections.defaultdict(set)
  1397. for key, c in list(our_stuff.items()):
  1398. if isinstance(c, _MapsColumns):
  1399. mp_to_assign = c.mapper_property_to_assign
  1400. if mp_to_assign:
  1401. our_stuff[key] = mp_to_assign
  1402. else:
  1403. # if no mapper property to assign, this currently means
  1404. # this is a MappedColumn that will produce a Column for us
  1405. del our_stuff[key]
  1406. for col, sort_order in c.columns_to_assign:
  1407. if not isinstance(c, CompositeProperty):
  1408. name_to_prop_key[col.name].add(key)
  1409. declared_columns.add(col)
  1410. # we would assert this, however we want the below
  1411. # warning to take effect instead. See #9630
  1412. # assert col not in column_ordering
  1413. column_ordering[col] = sort_order
  1414. # if this is a MappedColumn and the attribute key we
  1415. # have is not what the column has for its key, map the
  1416. # Column explicitly under the attribute key name.
  1417. # otherwise, Mapper will map it under the column key.
  1418. if mp_to_assign is None and key != col.key:
  1419. our_stuff[key] = col
  1420. elif isinstance(c, Column):
  1421. # undefer previously occurred here, and now occurs earlier.
  1422. # ensure every column we get here has been named
  1423. assert c.name is not None
  1424. name_to_prop_key[c.name].add(key)
  1425. declared_columns.add(c)
  1426. # if the column is the same name as the key,
  1427. # remove it from the explicit properties dict.
  1428. # the normal rules for assigning column-based properties
  1429. # will take over, including precedence of columns
  1430. # in multi-column ColumnProperties.
  1431. if key == c.key:
  1432. del our_stuff[key]
  1433. for name, keys in name_to_prop_key.items():
  1434. if len(keys) > 1:
  1435. util.warn(
  1436. "On class %r, Column object %r named "
  1437. "directly multiple times, "
  1438. "only one will be used: %s. "
  1439. "Consider using orm.synonym instead"
  1440. % (self.classname, name, (", ".join(sorted(keys))))
  1441. )
  1442. def _setup_table(self, table: Optional[FromClause] = None) -> None:
  1443. cls = self.cls
  1444. cls_as_Decl = cast("MappedClassProtocol[Any]", cls)
  1445. tablename = self.tablename
  1446. table_args = self.table_args
  1447. clsdict_view = self.clsdict_view
  1448. declared_columns = self.declared_columns
  1449. column_ordering = self.column_ordering
  1450. manager = attributes.manager_of_class(cls)
  1451. if "__table__" not in clsdict_view and table is None:
  1452. if hasattr(cls, "__table_cls__"):
  1453. table_cls = cast(
  1454. Type[Table],
  1455. util.unbound_method_to_callable(cls.__table_cls__), # type: ignore # noqa: E501
  1456. )
  1457. else:
  1458. table_cls = Table
  1459. if tablename is not None:
  1460. args: Tuple[Any, ...] = ()
  1461. table_kw: Dict[str, Any] = {}
  1462. if table_args:
  1463. if isinstance(table_args, dict):
  1464. table_kw = table_args
  1465. elif isinstance(table_args, tuple):
  1466. if isinstance(table_args[-1], dict):
  1467. args, table_kw = table_args[0:-1], table_args[-1]
  1468. else:
  1469. args = table_args
  1470. autoload_with = clsdict_view.get("__autoload_with__")
  1471. if autoload_with:
  1472. table_kw["autoload_with"] = autoload_with
  1473. autoload = clsdict_view.get("__autoload__")
  1474. if autoload:
  1475. table_kw["autoload"] = True
  1476. sorted_columns = sorted(
  1477. declared_columns,
  1478. key=lambda c: column_ordering.get(c, 0),
  1479. )
  1480. table = self.set_cls_attribute(
  1481. "__table__",
  1482. table_cls(
  1483. tablename,
  1484. self._metadata_for_cls(manager),
  1485. *sorted_columns,
  1486. *args,
  1487. **table_kw,
  1488. ),
  1489. )
  1490. else:
  1491. if table is None:
  1492. table = cls_as_Decl.__table__
  1493. if declared_columns:
  1494. for c in declared_columns:
  1495. if not table.c.contains_column(c):
  1496. raise exc.ArgumentError(
  1497. "Can't add additional column %r when "
  1498. "specifying __table__" % c.key
  1499. )
  1500. self.local_table = table
  1501. def _metadata_for_cls(self, manager: ClassManager[Any]) -> MetaData:
  1502. meta: Optional[MetaData] = getattr(self.cls, "metadata", None)
  1503. if meta is not None:
  1504. return meta
  1505. else:
  1506. return manager.registry.metadata
  1507. def _setup_inheriting_mapper(self, mapper_kw: _MapperKwArgs) -> None:
  1508. cls = self.cls
  1509. inherits = mapper_kw.get("inherits", None)
  1510. if inherits is None:
  1511. # since we search for classical mappings now, search for
  1512. # multiple mapped bases as well and raise an error.
  1513. inherits_search = []
  1514. for base_ in cls.__bases__:
  1515. c = _resolve_for_abstract_or_classical(base_)
  1516. if c is None:
  1517. continue
  1518. if _is_supercls_for_inherits(c) and c not in inherits_search:
  1519. inherits_search.append(c)
  1520. if inherits_search:
  1521. if len(inherits_search) > 1:
  1522. raise exc.InvalidRequestError(
  1523. "Class %s has multiple mapped bases: %r"
  1524. % (cls, inherits_search)
  1525. )
  1526. inherits = inherits_search[0]
  1527. elif isinstance(inherits, Mapper):
  1528. inherits = inherits.class_
  1529. self.inherits = inherits
  1530. clsdict_view = self.clsdict_view
  1531. if "__table__" not in clsdict_view and self.tablename is None:
  1532. self.single = True
  1533. def _setup_inheriting_columns(self, mapper_kw: _MapperKwArgs) -> None:
  1534. table = self.local_table
  1535. cls = self.cls
  1536. table_args = self.table_args
  1537. declared_columns = self.declared_columns
  1538. if (
  1539. table is None
  1540. and self.inherits is None
  1541. and not _get_immediate_cls_attr(cls, "__no_table__")
  1542. ):
  1543. raise exc.InvalidRequestError(
  1544. "Class %r does not have a __table__ or __tablename__ "
  1545. "specified and does not inherit from an existing "
  1546. "table-mapped class." % cls
  1547. )
  1548. elif self.inherits:
  1549. inherited_mapper_or_config = _declared_mapping_info(self.inherits)
  1550. assert inherited_mapper_or_config is not None
  1551. inherited_table = inherited_mapper_or_config.local_table
  1552. inherited_persist_selectable = (
  1553. inherited_mapper_or_config.persist_selectable
  1554. )
  1555. if table is None:
  1556. # single table inheritance.
  1557. # ensure no table args
  1558. if table_args:
  1559. raise exc.ArgumentError(
  1560. "Can't place __table_args__ on an inherited class "
  1561. "with no table."
  1562. )
  1563. # add any columns declared here to the inherited table.
  1564. if declared_columns and not isinstance(inherited_table, Table):
  1565. raise exc.ArgumentError(
  1566. f"Can't declare columns on single-table-inherited "
  1567. f"subclass {self.cls}; superclass {self.inherits} "
  1568. "is not mapped to a Table"
  1569. )
  1570. for col in declared_columns:
  1571. assert inherited_table is not None
  1572. if col.name in inherited_table.c:
  1573. if inherited_table.c[col.name] is col:
  1574. continue
  1575. raise exc.ArgumentError(
  1576. f"Column '{col}' on class {cls.__name__} "
  1577. f"conflicts with existing column "
  1578. f"'{inherited_table.c[col.name]}'. If using "
  1579. f"Declarative, consider using the "
  1580. "use_existing_column parameter of mapped_column() "
  1581. "to resolve conflicts."
  1582. )
  1583. if col.primary_key:
  1584. raise exc.ArgumentError(
  1585. "Can't place primary key columns on an inherited "
  1586. "class with no table."
  1587. )
  1588. if TYPE_CHECKING:
  1589. assert isinstance(inherited_table, Table)
  1590. inherited_table.append_column(col)
  1591. if (
  1592. inherited_persist_selectable is not None
  1593. and inherited_persist_selectable is not inherited_table
  1594. ):
  1595. inherited_persist_selectable._refresh_for_new_column(
  1596. col
  1597. )
  1598. def _prepare_mapper_arguments(self, mapper_kw: _MapperKwArgs) -> None:
  1599. properties = self.properties
  1600. if self.mapper_args_fn:
  1601. mapper_args = self.mapper_args_fn()
  1602. else:
  1603. mapper_args = {}
  1604. if mapper_kw:
  1605. mapper_args.update(mapper_kw)
  1606. if "properties" in mapper_args:
  1607. properties = dict(properties)
  1608. properties.update(mapper_args["properties"])
  1609. # make sure that column copies are used rather
  1610. # than the original columns from any mixins
  1611. for k in ("version_id_col", "polymorphic_on"):
  1612. if k in mapper_args:
  1613. v = mapper_args[k]
  1614. mapper_args[k] = self.column_copies.get(v, v)
  1615. if "primary_key" in mapper_args:
  1616. mapper_args["primary_key"] = [
  1617. self.column_copies.get(v, v)
  1618. for v in util.to_list(mapper_args["primary_key"])
  1619. ]
  1620. if "inherits" in mapper_args:
  1621. inherits_arg = mapper_args["inherits"]
  1622. if isinstance(inherits_arg, Mapper):
  1623. inherits_arg = inherits_arg.class_
  1624. if inherits_arg is not self.inherits:
  1625. raise exc.InvalidRequestError(
  1626. "mapper inherits argument given for non-inheriting "
  1627. "class %s" % (mapper_args["inherits"])
  1628. )
  1629. if self.inherits:
  1630. mapper_args["inherits"] = self.inherits
  1631. if self.inherits and not mapper_args.get("concrete", False):
  1632. # note the superclass is expected to have a Mapper assigned and
  1633. # not be a deferred config, as this is called within map()
  1634. inherited_mapper = class_mapper(self.inherits, False)
  1635. inherited_table = inherited_mapper.local_table
  1636. # single or joined inheritance
  1637. # exclude any cols on the inherited table which are
  1638. # not mapped on the parent class, to avoid
  1639. # mapping columns specific to sibling/nephew classes
  1640. if "exclude_properties" not in mapper_args:
  1641. mapper_args["exclude_properties"] = exclude_properties = {
  1642. c.key
  1643. for c in inherited_table.c
  1644. if c not in inherited_mapper._columntoproperty
  1645. }.union(inherited_mapper.exclude_properties or ())
  1646. exclude_properties.difference_update(
  1647. [c.key for c in self.declared_columns]
  1648. )
  1649. # look through columns in the current mapper that
  1650. # are keyed to a propname different than the colname
  1651. # (if names were the same, we'd have popped it out above,
  1652. # in which case the mapper makes this combination).
  1653. # See if the superclass has a similar column property.
  1654. # If so, join them together.
  1655. for k, col in list(properties.items()):
  1656. if not isinstance(col, expression.ColumnElement):
  1657. continue
  1658. if k in inherited_mapper._props:
  1659. p = inherited_mapper._props[k]
  1660. if isinstance(p, ColumnProperty):
  1661. # note here we place the subclass column
  1662. # first. See [ticket:1892] for background.
  1663. properties[k] = [col] + p.columns
  1664. result_mapper_args = mapper_args.copy()
  1665. result_mapper_args["properties"] = properties
  1666. self.mapper_args = result_mapper_args
  1667. def map(self, mapper_kw: _MapperKwArgs = util.EMPTY_DICT) -> Mapper[Any]:
  1668. self._prepare_mapper_arguments(mapper_kw)
  1669. if hasattr(self.cls, "__mapper_cls__"):
  1670. mapper_cls = cast(
  1671. "Type[Mapper[Any]]",
  1672. util.unbound_method_to_callable(
  1673. self.cls.__mapper_cls__ # type: ignore
  1674. ),
  1675. )
  1676. else:
  1677. mapper_cls = Mapper
  1678. return self.set_cls_attribute(
  1679. "__mapper__",
  1680. mapper_cls(self.cls, self.local_table, **self.mapper_args),
  1681. )
  1682. @util.preload_module("sqlalchemy.orm.decl_api")
  1683. def _as_dc_declaredattr(
  1684. field_metadata: Mapping[str, Any], sa_dataclass_metadata_key: str
  1685. ) -> Any:
  1686. # wrap lambdas inside dataclass fields inside an ad-hoc declared_attr.
  1687. # we can't write it because field.metadata is immutable :( so we have
  1688. # to go through extra trouble to compare these
  1689. decl_api = util.preloaded.orm_decl_api
  1690. obj = field_metadata[sa_dataclass_metadata_key]
  1691. if callable(obj) and not isinstance(obj, decl_api.declared_attr):
  1692. return decl_api.declared_attr(obj)
  1693. else:
  1694. return obj
  1695. class _DeferredMapperConfig(_ClassScanMapperConfig):
  1696. _cls: weakref.ref[Type[Any]]
  1697. is_deferred = True
  1698. _configs: util.OrderedDict[
  1699. weakref.ref[Type[Any]], _DeferredMapperConfig
  1700. ] = util.OrderedDict()
  1701. def _early_mapping(self, mapper_kw: _MapperKwArgs) -> None:
  1702. pass
  1703. # mypy disallows plain property override of variable
  1704. @property # type: ignore
  1705. def cls(self) -> Type[Any]:
  1706. return self._cls() # type: ignore
  1707. @cls.setter
  1708. def cls(self, class_: Type[Any]) -> None:
  1709. self._cls = weakref.ref(class_, self._remove_config_cls)
  1710. self._configs[self._cls] = self
  1711. @classmethod
  1712. def _remove_config_cls(cls, ref: weakref.ref[Type[Any]]) -> None:
  1713. cls._configs.pop(ref, None)
  1714. @classmethod
  1715. def has_cls(cls, class_: Type[Any]) -> bool:
  1716. # 2.6 fails on weakref if class_ is an old style class
  1717. return isinstance(class_, type) and weakref.ref(class_) in cls._configs
  1718. @classmethod
  1719. def raise_unmapped_for_cls(cls, class_: Type[Any]) -> NoReturn:
  1720. if hasattr(class_, "_sa_raise_deferred_config"):
  1721. class_._sa_raise_deferred_config()
  1722. raise orm_exc.UnmappedClassError(
  1723. class_,
  1724. msg=(
  1725. f"Class {orm_exc._safe_cls_name(class_)} has a deferred "
  1726. "mapping on it. It is not yet usable as a mapped class."
  1727. ),
  1728. )
  1729. @classmethod
  1730. def config_for_cls(cls, class_: Type[Any]) -> _DeferredMapperConfig:
  1731. return cls._configs[weakref.ref(class_)]
  1732. @classmethod
  1733. def classes_for_base(
  1734. cls, base_cls: Type[Any], sort: bool = True
  1735. ) -> List[_DeferredMapperConfig]:
  1736. classes_for_base = [
  1737. m
  1738. for m, cls_ in [(m, m.cls) for m in cls._configs.values()]
  1739. if cls_ is not None and issubclass(cls_, base_cls)
  1740. ]
  1741. if not sort:
  1742. return classes_for_base
  1743. all_m_by_cls = {m.cls: m for m in classes_for_base}
  1744. tuples: List[Tuple[_DeferredMapperConfig, _DeferredMapperConfig]] = []
  1745. for m_cls in all_m_by_cls:
  1746. tuples.extend(
  1747. (all_m_by_cls[base_cls], all_m_by_cls[m_cls])
  1748. for base_cls in m_cls.__bases__
  1749. if base_cls in all_m_by_cls
  1750. )
  1751. return list(topological.sort(tuples, classes_for_base))
  1752. def map(self, mapper_kw: _MapperKwArgs = util.EMPTY_DICT) -> Mapper[Any]:
  1753. self._configs.pop(self._cls, None)
  1754. return super().map(mapper_kw)
  1755. def _add_attribute(
  1756. cls: Type[Any], key: str, value: MapperProperty[Any]
  1757. ) -> None:
  1758. """add an attribute to an existing declarative class.
  1759. This runs through the logic to determine MapperProperty,
  1760. adds it to the Mapper, adds a column to the mapped Table, etc.
  1761. """
  1762. if "__mapper__" in cls.__dict__:
  1763. mapped_cls = cast("MappedClassProtocol[Any]", cls)
  1764. def _table_or_raise(mc: MappedClassProtocol[Any]) -> Table:
  1765. if isinstance(mc.__table__, Table):
  1766. return mc.__table__
  1767. raise exc.InvalidRequestError(
  1768. f"Cannot add a new attribute to mapped class {mc.__name__!r} "
  1769. "because it's not mapped against a table."
  1770. )
  1771. if isinstance(value, Column):
  1772. _undefer_column_name(key, value)
  1773. _table_or_raise(mapped_cls).append_column(
  1774. value, replace_existing=True
  1775. )
  1776. mapped_cls.__mapper__.add_property(key, value)
  1777. elif isinstance(value, _MapsColumns):
  1778. mp = value.mapper_property_to_assign
  1779. for col, _ in value.columns_to_assign:
  1780. _undefer_column_name(key, col)
  1781. _table_or_raise(mapped_cls).append_column(
  1782. col, replace_existing=True
  1783. )
  1784. if not mp:
  1785. mapped_cls.__mapper__.add_property(key, col)
  1786. if mp:
  1787. mapped_cls.__mapper__.add_property(key, mp)
  1788. elif isinstance(value, MapperProperty):
  1789. mapped_cls.__mapper__.add_property(key, value)
  1790. elif isinstance(value, QueryableAttribute) and value.key != key:
  1791. # detect a QueryableAttribute that's already mapped being
  1792. # assigned elsewhere in userland, turn into a synonym()
  1793. value = SynonymProperty(value.key)
  1794. mapped_cls.__mapper__.add_property(key, value)
  1795. else:
  1796. type.__setattr__(cls, key, value)
  1797. mapped_cls.__mapper__._expire_memoizations()
  1798. else:
  1799. type.__setattr__(cls, key, value)
  1800. def _del_attribute(cls: Type[Any], key: str) -> None:
  1801. if (
  1802. "__mapper__" in cls.__dict__
  1803. and key in cls.__dict__
  1804. and not cast(
  1805. "MappedClassProtocol[Any]", cls
  1806. ).__mapper__._dispose_called
  1807. ):
  1808. value = cls.__dict__[key]
  1809. if isinstance(
  1810. value, (Column, _MapsColumns, MapperProperty, QueryableAttribute)
  1811. ):
  1812. raise NotImplementedError(
  1813. "Can't un-map individual mapped attributes on a mapped class."
  1814. )
  1815. else:
  1816. type.__delattr__(cls, key)
  1817. cast(
  1818. "MappedClassProtocol[Any]", cls
  1819. ).__mapper__._expire_memoizations()
  1820. else:
  1821. type.__delattr__(cls, key)
  1822. def _declarative_constructor(self: Any, **kwargs: Any) -> None:
  1823. """A simple constructor that allows initialization from kwargs.
  1824. Sets attributes on the constructed instance using the names and
  1825. values in ``kwargs``.
  1826. Only keys that are present as
  1827. attributes of the instance's class are allowed. These could be,
  1828. for example, any mapped columns or relationships.
  1829. """
  1830. cls_ = type(self)
  1831. for k in kwargs:
  1832. if not hasattr(cls_, k):
  1833. raise TypeError(
  1834. "%r is an invalid keyword argument for %s" % (k, cls_.__name__)
  1835. )
  1836. setattr(self, k, kwargs[k])
  1837. _declarative_constructor.__name__ = "__init__"
  1838. def _undefer_column_name(key: str, column: Column[Any]) -> None:
  1839. if column.key is None:
  1840. column.key = key
  1841. if column.name is None:
  1842. column.name = key