|
64 | 64 | overload, |
65 | 65 | ) |
66 | 66 |
|
| 67 | +from typing_extensions import deprecated as ext_deprecated |
| 68 | + |
67 | 69 | if TYPE_CHECKING: |
68 | 70 | from discord import ( |
69 | 71 | Client, |
|
94 | 96 | __all__ = ( |
95 | 97 | "parse_time", |
96 | 98 | "warn_deprecated", |
| 99 | + "deprecated", |
97 | 100 | "oauth_url", |
98 | 101 | "snowflake_time", |
99 | 102 | "time_snowflake", |
@@ -344,6 +347,63 @@ def warn_deprecated( |
344 | 347 | warnings.warn(message, stacklevel=stacklevel, category=DeprecationWarning) |
345 | 348 |
|
346 | 349 |
|
| 350 | +@ext_deprecated( |
| 351 | + "deprecated is deprecated since version 2.8, consider using warnings.deprecated instead." |
| 352 | +) |
| 353 | +def deprecated( |
| 354 | + instead: str | None = None, |
| 355 | + since: str | None = None, |
| 356 | + removed: str | None = None, |
| 357 | + reference: str | None = None, |
| 358 | + stacklevel: int = 3, |
| 359 | + *, |
| 360 | + use_qualname: bool = True, |
| 361 | +) -> Callable[[Callable[[P], T]], Callable[[P], T]]: |
| 362 | + """A decorator implementation of :func:`warn_deprecated`. This will automatically call :func:`warn_deprecated` when |
| 363 | + the decorated function is called. |
| 364 | +
|
| 365 | + .. deprecated:: 2.8 |
| 366 | + Deprecated in favor of :func:`warnings.deprecated`. |
| 367 | +
|
| 368 | + Parameters |
| 369 | + ---------- |
| 370 | + instead: Optional[:class:`str`] |
| 371 | + A recommended alternative to the function. |
| 372 | + since: Optional[:class:`str`] |
| 373 | + The version in which the function was deprecated. This should be in the format ``major.minor(.patch)``, where |
| 374 | + the patch version is optional. |
| 375 | + removed: Optional[:class:`str`] |
| 376 | + The version in which the function is planned to be removed. This should be in the format |
| 377 | + ``major.minor(.patch)``, where the patch version is optional. |
| 378 | + reference: Optional[:class:`str`] |
| 379 | + A reference that explains the deprecation, typically a URL to a page such as a changelog entry or a GitHub |
| 380 | + issue/PR. |
| 381 | + stacklevel: :class:`int` |
| 382 | + The stacklevel kwarg passed to :func:`warnings.warn`. Defaults to 3. |
| 383 | + use_qualname: :class:`bool` |
| 384 | + Whether to use the qualified name of the function in the deprecation warning. If ``False``, the short name of |
| 385 | + the function will be used instead. For example, __qualname__ will display as ``Client.login`` while __name__ |
| 386 | + will display as ``login``. Defaults to ``True``. |
| 387 | + """ |
| 388 | + |
| 389 | + def actual_decorator(func: Callable[[P], T]) -> Callable[[P], T]: |
| 390 | + @functools.wraps(func) |
| 391 | + def decorated(*args: P.args, **kwargs: P.kwargs) -> T: |
| 392 | + warn_deprecated( |
| 393 | + name=func.__qualname__ if use_qualname else func.__name__, |
| 394 | + instead=instead, |
| 395 | + since=since, |
| 396 | + removed=removed, |
| 397 | + reference=reference, |
| 398 | + stacklevel=stacklevel, |
| 399 | + ) |
| 400 | + return func(*args, **kwargs) |
| 401 | + |
| 402 | + return decorated |
| 403 | + |
| 404 | + return actual_decorator |
| 405 | + |
| 406 | + |
347 | 407 | def oauth_url( |
348 | 408 | client_id: int | str, |
349 | 409 | *, |
|
0 commit comments