deprecations.py 13 KB

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