You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

406 lines
11KB

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