Wrap a function with a warnings.warn and augmented docstring.
(
func: _F,
wtype: Type[exc.SADeprecationWarning],
message: str,
version: str,
docstring_header: Optional[str] = None,
enable_warnings: bool = True,
)
| 357 | |
| 358 | |
| 359 | def _decorate_with_warning( |
| 360 | func: _F, |
| 361 | wtype: Type[exc.SADeprecationWarning], |
| 362 | message: str, |
| 363 | version: str, |
| 364 | docstring_header: Optional[str] = None, |
| 365 | enable_warnings: bool = True, |
| 366 | ) -> _F: |
| 367 | """Wrap a function with a warnings.warn and augmented docstring.""" |
| 368 | |
| 369 | message = _sanitize_restructured_text(message) |
| 370 | |
| 371 | if issubclass(wtype, exc.Base20DeprecationWarning): |
| 372 | doc_only = ( |
| 373 | " (Background on SQLAlchemy 2.0 at: " |
| 374 | ":ref:`migration_20_toplevel`)" |
| 375 | ) |
| 376 | else: |
| 377 | doc_only = "" |
| 378 | |
| 379 | @decorator |
| 380 | def warned(fn: _F, *args: Any, **kwargs: Any) -> _F: |
| 381 | skip_warning = not enable_warnings or kwargs.pop( |
| 382 | "_sa_skip_warning", False |
| 383 | ) |
| 384 | if not skip_warning: |
| 385 | _warn_with_version(message, version, wtype, stacklevel=3) |
| 386 | return fn(*args, **kwargs) # type: ignore[no-any-return] |
| 387 | |
| 388 | doc = func.__doc__ is not None and func.__doc__ or "" |
| 389 | if docstring_header is not None: |
| 390 | docstring_header %= dict(func=func.__name__) |
| 391 | |
| 392 | docstring_header += doc_only |
| 393 | |
| 394 | doc = inject_docstring_text(doc, docstring_header, 1) |
| 395 | |
| 396 | decorated = warned(func) |
| 397 | decorated.__doc__ = doc |
| 398 | decorated._sa_warn = lambda: _warn_with_version( # type: ignore |
| 399 | message, version, wtype, stacklevel=3 |
| 400 | ) |
| 401 | return decorated |
no test coverage detected