deprecations.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. # util/deprecations.py
  2. # Copyright (C) 2005-2022 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. """Helpers related to deprecation of functions, methods, classes, other
  8. functionality."""
  9. import os
  10. import re
  11. from . import compat
  12. from .langhelpers import _hash_limit_string
  13. from .langhelpers import _warnings_warn
  14. from .langhelpers import decorator
  15. from .langhelpers import inject_docstring_text
  16. from .langhelpers import inject_param_text
  17. from .. import exc
  18. SQLALCHEMY_WARN_20 = False
  19. if os.getenv("SQLALCHEMY_WARN_20", "false").lower() in ("true", "yes", "1"):
  20. SQLALCHEMY_WARN_20 = True
  21. def _warn_with_version(msg, version, type_, stacklevel, code=None):
  22. if (
  23. issubclass(type_, exc.Base20DeprecationWarning)
  24. and not SQLALCHEMY_WARN_20
  25. ):
  26. return
  27. warn = type_(msg, code=code)
  28. warn.deprecated_since = version
  29. _warnings_warn(warn, stacklevel=stacklevel + 1)
  30. def warn_deprecated(msg, version, stacklevel=3, code=None):
  31. _warn_with_version(
  32. msg, version, exc.SADeprecationWarning, stacklevel, code=code
  33. )
  34. def warn_deprecated_limited(msg, args, version, stacklevel=3, code=None):
  35. """Issue a deprecation warning with a parameterized string,
  36. limiting the number of registrations.
  37. """
  38. if args:
  39. msg = _hash_limit_string(msg, 10, args)
  40. _warn_with_version(
  41. msg, version, exc.SADeprecationWarning, stacklevel, code=code
  42. )
  43. def warn_deprecated_20(msg, stacklevel=3, code=None):
  44. _warn_with_version(
  45. msg,
  46. exc.RemovedIn20Warning.deprecated_since,
  47. exc.RemovedIn20Warning,
  48. stacklevel,
  49. code=code,
  50. )
  51. def deprecated_cls(version, message, constructor="__init__"):
  52. header = ".. deprecated:: %s %s" % (version, (message or ""))
  53. def decorate(cls):
  54. return _decorate_cls_with_warning(
  55. cls,
  56. constructor,
  57. exc.SADeprecationWarning,
  58. message % dict(func=constructor),
  59. version,
  60. header,
  61. )
  62. return decorate
  63. def deprecated_20_cls(
  64. clsname, alternative=None, constructor="__init__", becomes_legacy=False
  65. ):
  66. message = (
  67. ".. deprecated:: 1.4 The %s class is considered legacy as of the "
  68. "1.x series of SQLAlchemy and %s in 2.0."
  69. % (
  70. clsname,
  71. "will be removed"
  72. if not becomes_legacy
  73. else "becomes a legacy construct",
  74. )
  75. )
  76. if alternative:
  77. message += " " + alternative
  78. if becomes_legacy:
  79. warning_cls = exc.LegacyAPIWarning
  80. else:
  81. warning_cls = exc.RemovedIn20Warning
  82. def decorate(cls):
  83. return _decorate_cls_with_warning(
  84. cls,
  85. constructor,
  86. warning_cls,
  87. message,
  88. warning_cls.deprecated_since,
  89. message,
  90. )
  91. return decorate
  92. def deprecated(
  93. version,
  94. message=None,
  95. add_deprecation_to_docstring=True,
  96. warning=None,
  97. enable_warnings=True,
  98. ):
  99. """Decorates a function and issues a deprecation warning on use.
  100. :param version:
  101. Issue version in the warning.
  102. :param message:
  103. If provided, issue message in the warning. A sensible default
  104. is used if not provided.
  105. :param add_deprecation_to_docstring:
  106. Default True. If False, the wrapped function's __doc__ is left
  107. as-is. If True, the 'message' is prepended to the docs if
  108. provided, or sensible default if message is omitted.
  109. """
  110. # nothing is deprecated "since" 2.0 at this time. All "removed in 2.0"
  111. # should emit the RemovedIn20Warning, but messaging should be expressed
  112. # in terms of "deprecated since 1.4".
  113. if version == "2.0":
  114. if warning is None:
  115. warning = exc.RemovedIn20Warning
  116. version = "1.4"
  117. if add_deprecation_to_docstring:
  118. header = ".. deprecated:: %s %s" % (
  119. version,
  120. (message or ""),
  121. )
  122. else:
  123. header = None
  124. if message is None:
  125. message = "Call to deprecated function %(func)s"
  126. if warning is None:
  127. warning = exc.SADeprecationWarning
  128. if warning is not exc.RemovedIn20Warning:
  129. message += " (deprecated since: %s)" % version
  130. def decorate(fn):
  131. return _decorate_with_warning(
  132. fn,
  133. warning,
  134. message % dict(func=fn.__name__),
  135. version,
  136. header,
  137. enable_warnings=enable_warnings,
  138. )
  139. return decorate
  140. def moved_20(message, **kw):
  141. return deprecated(
  142. "2.0", message=message, warning=exc.MovedIn20Warning, **kw
  143. )
  144. def deprecated_20(api_name, alternative=None, becomes_legacy=False, **kw):
  145. type_reg = re.match("^:(attr|func|meth):", api_name)
  146. if type_reg:
  147. type_ = {"attr": "attribute", "func": "function", "meth": "method"}[
  148. type_reg.group(1)
  149. ]
  150. else:
  151. type_ = "construct"
  152. message = (
  153. "The %s %s is considered legacy as of the "
  154. "1.x series of SQLAlchemy and %s in 2.0."
  155. % (
  156. api_name,
  157. type_,
  158. "will be removed"
  159. if not becomes_legacy
  160. else "becomes a legacy construct",
  161. )
  162. )
  163. if ":attr:" in api_name:
  164. attribute_ok = kw.pop("warn_on_attribute_access", False)
  165. if not attribute_ok:
  166. assert kw.get("enable_warnings") is False, (
  167. "attribute %s will emit a warning on read access. "
  168. "If you *really* want this, "
  169. "add warn_on_attribute_access=True. Otherwise please add "
  170. "enable_warnings=False." % api_name
  171. )
  172. if alternative:
  173. message += " " + alternative
  174. if becomes_legacy:
  175. warning_cls = exc.LegacyAPIWarning
  176. else:
  177. warning_cls = exc.RemovedIn20Warning
  178. return deprecated("2.0", message=message, warning=warning_cls, **kw)
  179. def deprecated_params(**specs):
  180. """Decorates a function to warn on use of certain parameters.
  181. e.g. ::
  182. @deprecated_params(
  183. weak_identity_map=(
  184. "0.7",
  185. "the :paramref:`.Session.weak_identity_map parameter "
  186. "is deprecated."
  187. )
  188. )
  189. """
  190. messages = {}
  191. versions = {}
  192. version_warnings = {}
  193. for param, (version, message) in specs.items():
  194. versions[param] = version
  195. messages[param] = _sanitize_restructured_text(message)
  196. version_warnings[param] = (
  197. exc.RemovedIn20Warning
  198. if version == "2.0"
  199. else exc.SADeprecationWarning
  200. )
  201. def decorate(fn):
  202. spec = compat.inspect_getfullargspec(fn)
  203. if spec.defaults is not None:
  204. defaults = dict(
  205. zip(
  206. spec.args[(len(spec.args) - len(spec.defaults)) :],
  207. spec.defaults,
  208. )
  209. )
  210. check_defaults = set(defaults).intersection(messages)
  211. check_kw = set(messages).difference(defaults)
  212. else:
  213. check_defaults = ()
  214. check_kw = set(messages)
  215. check_any_kw = spec.varkw
  216. @decorator
  217. def warned(fn, *args, **kwargs):
  218. for m in check_defaults:
  219. if (defaults[m] is None and kwargs[m] is not None) or (
  220. defaults[m] is not None and kwargs[m] != defaults[m]
  221. ):
  222. _warn_with_version(
  223. messages[m],
  224. versions[m],
  225. version_warnings[m],
  226. stacklevel=3,
  227. )
  228. if check_any_kw in messages and set(kwargs).difference(
  229. check_defaults
  230. ):
  231. _warn_with_version(
  232. messages[check_any_kw],
  233. versions[check_any_kw],
  234. version_warnings[check_any_kw],
  235. stacklevel=3,
  236. )
  237. for m in check_kw:
  238. if m in kwargs:
  239. _warn_with_version(
  240. messages[m],
  241. versions[m],
  242. version_warnings[m],
  243. stacklevel=3,
  244. )
  245. return fn(*args, **kwargs)
  246. doc = fn.__doc__ is not None and fn.__doc__ or ""
  247. if doc:
  248. doc = inject_param_text(
  249. doc,
  250. {
  251. param: ".. deprecated:: %s %s"
  252. % ("1.4" if version == "2.0" else version, (message or ""))
  253. for param, (version, message) in specs.items()
  254. },
  255. )
  256. decorated = warned(fn)
  257. decorated.__doc__ = doc
  258. return decorated
  259. return decorate
  260. def _sanitize_restructured_text(text):
  261. def repl(m):
  262. type_, name = m.group(1, 2)
  263. if type_ in ("func", "meth"):
  264. name += "()"
  265. return name
  266. text = re.sub(r":ref:`(.+) <.*>`", lambda m: '"%s"' % m.group(1), text)
  267. return re.sub(r"\:(\w+)\:`~?(?:_\w+)?\.?(.+?)`", repl, text)
  268. def _decorate_cls_with_warning(
  269. cls, constructor, wtype, message, version, docstring_header=None
  270. ):
  271. doc = cls.__doc__ is not None and cls.__doc__ or ""
  272. if docstring_header is not None:
  273. if constructor is not None:
  274. docstring_header %= dict(func=constructor)
  275. if issubclass(wtype, exc.Base20DeprecationWarning):
  276. docstring_header += (
  277. " (Background on SQLAlchemy 2.0 at: "
  278. ":ref:`migration_20_toplevel`)"
  279. )
  280. doc = inject_docstring_text(doc, docstring_header, 1)
  281. if type(cls) is type:
  282. clsdict = dict(cls.__dict__)
  283. clsdict["__doc__"] = doc
  284. clsdict.pop("__dict__", None)
  285. cls = type(cls.__name__, cls.__bases__, clsdict)
  286. if constructor is not None:
  287. constructor_fn = clsdict[constructor]
  288. else:
  289. cls.__doc__ = doc
  290. if constructor is not None:
  291. constructor_fn = getattr(cls, constructor)
  292. if constructor is not None:
  293. setattr(
  294. cls,
  295. constructor,
  296. _decorate_with_warning(
  297. constructor_fn, wtype, message, version, None
  298. ),
  299. )
  300. return cls
  301. def _decorate_with_warning(
  302. func, wtype, message, version, docstring_header=None, enable_warnings=True
  303. ):
  304. """Wrap a function with a warnings.warn and augmented docstring."""
  305. message = _sanitize_restructured_text(message)
  306. if issubclass(wtype, exc.Base20DeprecationWarning):
  307. doc_only = (
  308. " (Background on SQLAlchemy 2.0 at: "
  309. ":ref:`migration_20_toplevel`)"
  310. )
  311. else:
  312. doc_only = ""
  313. @decorator
  314. def warned(fn, *args, **kwargs):
  315. skip_warning = not enable_warnings or kwargs.pop(
  316. "_sa_skip_warning", False
  317. )
  318. if not skip_warning:
  319. _warn_with_version(message, version, wtype, stacklevel=3)
  320. return fn(*args, **kwargs)
  321. doc = func.__doc__ is not None and func.__doc__ or ""
  322. if docstring_header is not None:
  323. docstring_header %= dict(func=func.__name__)
  324. docstring_header += doc_only
  325. doc = inject_docstring_text(doc, docstring_header, 1)
  326. decorated = warned(func)
  327. decorated.__doc__ = doc
  328. decorated._sa_warn = lambda: _warn_with_version(
  329. message, version, wtype, stacklevel=3
  330. )
  331. return decorated