clsregistry.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. # orm/clsregistry.py
  2. # Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. """Routines to handle the string class registry used by declarative.
  8. This system allows specification of classes and expressions used in
  9. :func:`_orm.relationship` using strings.
  10. """
  11. from __future__ import annotations
  12. import re
  13. from typing import Any
  14. from typing import Callable
  15. from typing import cast
  16. from typing import Dict
  17. from typing import Generator
  18. from typing import Iterable
  19. from typing import List
  20. from typing import Mapping
  21. from typing import MutableMapping
  22. from typing import NoReturn
  23. from typing import Optional
  24. from typing import Set
  25. from typing import Tuple
  26. from typing import Type
  27. from typing import TYPE_CHECKING
  28. from typing import TypeVar
  29. from typing import Union
  30. import weakref
  31. from . import attributes
  32. from . import interfaces
  33. from .descriptor_props import SynonymProperty
  34. from .properties import ColumnProperty
  35. from .util import class_mapper
  36. from .. import exc
  37. from .. import inspection
  38. from .. import util
  39. from ..sql.schema import _get_table_key
  40. from ..util.typing import CallableReference
  41. if TYPE_CHECKING:
  42. from .relationships import RelationshipProperty
  43. from ..sql.schema import MetaData
  44. from ..sql.schema import Table
  45. _T = TypeVar("_T", bound=Any)
  46. _ClsRegistryType = MutableMapping[str, Union[type, "ClsRegistryToken"]]
  47. # strong references to registries which we place in
  48. # the _decl_class_registry, which is usually weak referencing.
  49. # the internal registries here link to classes with weakrefs and remove
  50. # themselves when all references to contained classes are removed.
  51. _registries: Set[ClsRegistryToken] = set()
  52. def add_class(
  53. classname: str, cls: Type[_T], decl_class_registry: _ClsRegistryType
  54. ) -> None:
  55. """Add a class to the _decl_class_registry associated with the
  56. given declarative class.
  57. """
  58. if classname in decl_class_registry:
  59. # class already exists.
  60. existing = decl_class_registry[classname]
  61. if not isinstance(existing, _MultipleClassMarker):
  62. decl_class_registry[classname] = _MultipleClassMarker(
  63. [cls, cast("Type[Any]", existing)]
  64. )
  65. else:
  66. decl_class_registry[classname] = cls
  67. try:
  68. root_module = cast(
  69. _ModuleMarker, decl_class_registry["_sa_module_registry"]
  70. )
  71. except KeyError:
  72. decl_class_registry["_sa_module_registry"] = root_module = (
  73. _ModuleMarker("_sa_module_registry", None)
  74. )
  75. tokens = cls.__module__.split(".")
  76. # build up a tree like this:
  77. # modulename: myapp.snacks.nuts
  78. #
  79. # myapp->snack->nuts->(classes)
  80. # snack->nuts->(classes)
  81. # nuts->(classes)
  82. #
  83. # this allows partial token paths to be used.
  84. while tokens:
  85. token = tokens.pop(0)
  86. module = root_module.get_module(token)
  87. for token in tokens:
  88. module = module.get_module(token)
  89. try:
  90. module.add_class(classname, cls)
  91. except AttributeError as ae:
  92. if not isinstance(module, _ModuleMarker):
  93. raise exc.InvalidRequestError(
  94. f'name "{classname}" matches both a '
  95. "class name and a module name"
  96. ) from ae
  97. else:
  98. raise
  99. def remove_class(
  100. classname: str, cls: Type[Any], decl_class_registry: _ClsRegistryType
  101. ) -> None:
  102. if classname in decl_class_registry:
  103. existing = decl_class_registry[classname]
  104. if isinstance(existing, _MultipleClassMarker):
  105. existing.remove_item(cls)
  106. else:
  107. del decl_class_registry[classname]
  108. try:
  109. root_module = cast(
  110. _ModuleMarker, decl_class_registry["_sa_module_registry"]
  111. )
  112. except KeyError:
  113. return
  114. tokens = cls.__module__.split(".")
  115. while tokens:
  116. token = tokens.pop(0)
  117. module = root_module.get_module(token)
  118. for token in tokens:
  119. module = module.get_module(token)
  120. try:
  121. module.remove_class(classname, cls)
  122. except AttributeError:
  123. if not isinstance(module, _ModuleMarker):
  124. pass
  125. else:
  126. raise
  127. def _key_is_empty(
  128. key: str,
  129. decl_class_registry: _ClsRegistryType,
  130. test: Callable[[Any], bool],
  131. ) -> bool:
  132. """test if a key is empty of a certain object.
  133. used for unit tests against the registry to see if garbage collection
  134. is working.
  135. "test" is a callable that will be passed an object should return True
  136. if the given object is the one we were looking for.
  137. We can't pass the actual object itself b.c. this is for testing garbage
  138. collection; the caller will have to have removed references to the
  139. object itself.
  140. """
  141. if key not in decl_class_registry:
  142. return True
  143. thing = decl_class_registry[key]
  144. if isinstance(thing, _MultipleClassMarker):
  145. for sub_thing in thing.contents:
  146. if test(sub_thing):
  147. return False
  148. else:
  149. raise NotImplementedError("unknown codepath")
  150. else:
  151. return not test(thing)
  152. class ClsRegistryToken:
  153. """an object that can be in the registry._class_registry as a value."""
  154. __slots__ = ()
  155. class _MultipleClassMarker(ClsRegistryToken):
  156. """refers to multiple classes of the same name
  157. within _decl_class_registry.
  158. """
  159. __slots__ = "on_remove", "contents", "__weakref__"
  160. contents: Set[weakref.ref[Type[Any]]]
  161. on_remove: CallableReference[Optional[Callable[[], None]]]
  162. def __init__(
  163. self,
  164. classes: Iterable[Type[Any]],
  165. on_remove: Optional[Callable[[], None]] = None,
  166. ):
  167. self.on_remove = on_remove
  168. self.contents = {
  169. weakref.ref(item, self._remove_item) for item in classes
  170. }
  171. _registries.add(self)
  172. def remove_item(self, cls: Type[Any]) -> None:
  173. self._remove_item(weakref.ref(cls))
  174. def __iter__(self) -> Generator[Optional[Type[Any]], None, None]:
  175. return (ref() for ref in self.contents)
  176. def attempt_get(self, path: List[str], key: str) -> Type[Any]:
  177. if len(self.contents) > 1:
  178. raise exc.InvalidRequestError(
  179. 'Multiple classes found for path "%s" '
  180. "in the registry of this declarative "
  181. "base. Please use a fully module-qualified path."
  182. % (".".join(path + [key]))
  183. )
  184. else:
  185. ref = list(self.contents)[0]
  186. cls = ref()
  187. if cls is None:
  188. raise NameError(key)
  189. return cls
  190. def _remove_item(self, ref: weakref.ref[Type[Any]]) -> None:
  191. self.contents.discard(ref)
  192. if not self.contents:
  193. _registries.discard(self)
  194. if self.on_remove:
  195. self.on_remove()
  196. def add_item(self, item: Type[Any]) -> None:
  197. # protect against class registration race condition against
  198. # asynchronous garbage collection calling _remove_item,
  199. # [ticket:3208] and [ticket:10782]
  200. modules = {
  201. cls.__module__
  202. for cls in [ref() for ref in list(self.contents)]
  203. if cls is not None
  204. }
  205. if item.__module__ in modules:
  206. util.warn(
  207. "This declarative base already contains a class with the "
  208. "same class name and module name as %s.%s, and will "
  209. "be replaced in the string-lookup table."
  210. % (item.__module__, item.__name__)
  211. )
  212. self.contents.add(weakref.ref(item, self._remove_item))
  213. class _ModuleMarker(ClsRegistryToken):
  214. """Refers to a module name within
  215. _decl_class_registry.
  216. """
  217. __slots__ = "parent", "name", "contents", "mod_ns", "path", "__weakref__"
  218. parent: Optional[_ModuleMarker]
  219. contents: Dict[str, Union[_ModuleMarker, _MultipleClassMarker]]
  220. mod_ns: _ModNS
  221. path: List[str]
  222. def __init__(self, name: str, parent: Optional[_ModuleMarker]):
  223. self.parent = parent
  224. self.name = name
  225. self.contents = {}
  226. self.mod_ns = _ModNS(self)
  227. if self.parent:
  228. self.path = self.parent.path + [self.name]
  229. else:
  230. self.path = []
  231. _registries.add(self)
  232. def __contains__(self, name: str) -> bool:
  233. return name in self.contents
  234. def __getitem__(self, name: str) -> ClsRegistryToken:
  235. return self.contents[name]
  236. def _remove_item(self, name: str) -> None:
  237. self.contents.pop(name, None)
  238. if not self.contents:
  239. if self.parent is not None:
  240. self.parent._remove_item(self.name)
  241. _registries.discard(self)
  242. def resolve_attr(self, key: str) -> Union[_ModNS, Type[Any]]:
  243. return self.mod_ns.__getattr__(key)
  244. def get_module(self, name: str) -> _ModuleMarker:
  245. if name not in self.contents:
  246. marker = _ModuleMarker(name, self)
  247. self.contents[name] = marker
  248. else:
  249. marker = cast(_ModuleMarker, self.contents[name])
  250. return marker
  251. def add_class(self, name: str, cls: Type[Any]) -> None:
  252. if name in self.contents:
  253. existing = cast(_MultipleClassMarker, self.contents[name])
  254. try:
  255. existing.add_item(cls)
  256. except AttributeError as ae:
  257. if not isinstance(existing, _MultipleClassMarker):
  258. raise exc.InvalidRequestError(
  259. f'name "{name}" matches both a '
  260. "class name and a module name"
  261. ) from ae
  262. else:
  263. raise
  264. else:
  265. self.contents[name] = _MultipleClassMarker(
  266. [cls], on_remove=lambda: self._remove_item(name)
  267. )
  268. def remove_class(self, name: str, cls: Type[Any]) -> None:
  269. if name in self.contents:
  270. existing = cast(_MultipleClassMarker, self.contents[name])
  271. existing.remove_item(cls)
  272. class _ModNS:
  273. __slots__ = ("__parent",)
  274. __parent: _ModuleMarker
  275. def __init__(self, parent: _ModuleMarker):
  276. self.__parent = parent
  277. def __getattr__(self, key: str) -> Union[_ModNS, Type[Any]]:
  278. try:
  279. value = self.__parent.contents[key]
  280. except KeyError:
  281. pass
  282. else:
  283. if value is not None:
  284. if isinstance(value, _ModuleMarker):
  285. return value.mod_ns
  286. else:
  287. assert isinstance(value, _MultipleClassMarker)
  288. return value.attempt_get(self.__parent.path, key)
  289. raise NameError(
  290. "Module %r has no mapped classes "
  291. "registered under the name %r" % (self.__parent.name, key)
  292. )
  293. class _GetColumns:
  294. __slots__ = ("cls",)
  295. cls: Type[Any]
  296. def __init__(self, cls: Type[Any]):
  297. self.cls = cls
  298. def __getattr__(self, key: str) -> Any:
  299. mp = class_mapper(self.cls, configure=False)
  300. if mp:
  301. if key not in mp.all_orm_descriptors:
  302. raise AttributeError(
  303. "Class %r does not have a mapped column named %r"
  304. % (self.cls, key)
  305. )
  306. desc = mp.all_orm_descriptors[key]
  307. if desc.extension_type is interfaces.NotExtension.NOT_EXTENSION:
  308. assert isinstance(desc, attributes.QueryableAttribute)
  309. prop = desc.property
  310. if isinstance(prop, SynonymProperty):
  311. key = prop.name
  312. elif not isinstance(prop, ColumnProperty):
  313. raise exc.InvalidRequestError(
  314. "Property %r is not an instance of"
  315. " ColumnProperty (i.e. does not correspond"
  316. " directly to a Column)." % key
  317. )
  318. return getattr(self.cls, key)
  319. inspection._inspects(_GetColumns)(
  320. lambda target: inspection.inspect(target.cls)
  321. )
  322. class _GetTable:
  323. __slots__ = "key", "metadata"
  324. key: str
  325. metadata: MetaData
  326. def __init__(self, key: str, metadata: MetaData):
  327. self.key = key
  328. self.metadata = metadata
  329. def __getattr__(self, key: str) -> Table:
  330. return self.metadata.tables[_get_table_key(key, self.key)]
  331. def _determine_container(key: str, value: Any) -> _GetColumns:
  332. if isinstance(value, _MultipleClassMarker):
  333. value = value.attempt_get([], key)
  334. return _GetColumns(value)
  335. class _class_resolver:
  336. __slots__ = (
  337. "cls",
  338. "prop",
  339. "arg",
  340. "fallback",
  341. "_dict",
  342. "_resolvers",
  343. "favor_tables",
  344. )
  345. cls: Type[Any]
  346. prop: RelationshipProperty[Any]
  347. fallback: Mapping[str, Any]
  348. arg: str
  349. favor_tables: bool
  350. _resolvers: Tuple[Callable[[str], Any], ...]
  351. def __init__(
  352. self,
  353. cls: Type[Any],
  354. prop: RelationshipProperty[Any],
  355. fallback: Mapping[str, Any],
  356. arg: str,
  357. favor_tables: bool = False,
  358. ):
  359. self.cls = cls
  360. self.prop = prop
  361. self.arg = arg
  362. self.fallback = fallback
  363. self._dict = util.PopulateDict(self._access_cls)
  364. self._resolvers = ()
  365. self.favor_tables = favor_tables
  366. def _access_cls(self, key: str) -> Any:
  367. cls = self.cls
  368. manager = attributes.manager_of_class(cls)
  369. decl_base = manager.registry
  370. assert decl_base is not None
  371. decl_class_registry = decl_base._class_registry
  372. metadata = decl_base.metadata
  373. if self.favor_tables:
  374. if key in metadata.tables:
  375. return metadata.tables[key]
  376. elif key in metadata._schemas:
  377. return _GetTable(key, getattr(cls, "metadata", metadata))
  378. if key in decl_class_registry:
  379. return _determine_container(key, decl_class_registry[key])
  380. if not self.favor_tables:
  381. if key in metadata.tables:
  382. return metadata.tables[key]
  383. elif key in metadata._schemas:
  384. return _GetTable(key, getattr(cls, "metadata", metadata))
  385. if "_sa_module_registry" in decl_class_registry and key in cast(
  386. _ModuleMarker, decl_class_registry["_sa_module_registry"]
  387. ):
  388. registry = cast(
  389. _ModuleMarker, decl_class_registry["_sa_module_registry"]
  390. )
  391. return registry.resolve_attr(key)
  392. elif self._resolvers:
  393. for resolv in self._resolvers:
  394. value = resolv(key)
  395. if value is not None:
  396. return value
  397. return self.fallback[key]
  398. def _raise_for_name(self, name: str, err: Exception) -> NoReturn:
  399. generic_match = re.match(r"(.+)\[(.+)\]", name)
  400. if generic_match:
  401. clsarg = generic_match.group(2).strip("'")
  402. raise exc.InvalidRequestError(
  403. f"When initializing mapper {self.prop.parent}, "
  404. f'expression "relationship({self.arg!r})" seems to be '
  405. "using a generic class as the argument to relationship(); "
  406. "please state the generic argument "
  407. "using an annotation, e.g. "
  408. f'"{self.prop.key}: Mapped[{generic_match.group(1)}'
  409. f"['{clsarg}']] = relationship()\""
  410. ) from err
  411. else:
  412. raise exc.InvalidRequestError(
  413. "When initializing mapper %s, expression %r failed to "
  414. "locate a name (%r). If this is a class name, consider "
  415. "adding this relationship() to the %r class after "
  416. "both dependent classes have been defined."
  417. % (self.prop.parent, self.arg, name, self.cls)
  418. ) from err
  419. def _resolve_name(self) -> Union[Table, Type[Any], _ModNS]:
  420. name = self.arg
  421. d = self._dict
  422. rval = None
  423. try:
  424. for token in name.split("."):
  425. if rval is None:
  426. rval = d[token]
  427. else:
  428. rval = getattr(rval, token)
  429. except KeyError as err:
  430. self._raise_for_name(name, err)
  431. except NameError as n:
  432. self._raise_for_name(n.args[0], n)
  433. else:
  434. if isinstance(rval, _GetColumns):
  435. return rval.cls
  436. else:
  437. if TYPE_CHECKING:
  438. assert isinstance(rval, (type, Table, _ModNS))
  439. return rval
  440. def __call__(self) -> Any:
  441. try:
  442. x = eval(self.arg, globals(), self._dict)
  443. if isinstance(x, _GetColumns):
  444. return x.cls
  445. else:
  446. return x
  447. except NameError as n:
  448. self._raise_for_name(n.args[0], n)
  449. _fallback_dict: Mapping[str, Any] = None # type: ignore
  450. def _resolver(cls: Type[Any], prop: RelationshipProperty[Any]) -> Tuple[
  451. Callable[[str], Callable[[], Union[Type[Any], Table, _ModNS]]],
  452. Callable[[str, bool], _class_resolver],
  453. ]:
  454. global _fallback_dict
  455. if _fallback_dict is None:
  456. import sqlalchemy
  457. from . import foreign
  458. from . import remote
  459. _fallback_dict = util.immutabledict(sqlalchemy.__dict__).union(
  460. {"foreign": foreign, "remote": remote}
  461. )
  462. def resolve_arg(arg: str, favor_tables: bool = False) -> _class_resolver:
  463. return _class_resolver(
  464. cls, prop, _fallback_dict, arg, favor_tables=favor_tables
  465. )
  466. def resolve_name(
  467. arg: str,
  468. ) -> Callable[[], Union[Type[Any], Table, _ModNS]]:
  469. return _class_resolver(cls, prop, _fallback_dict, arg)._resolve_name
  470. return resolve_name, resolve_arg