|
5 | 5 | import sys
|
6 | 6 | import os
|
7 | 7 | import re
|
| 8 | +import warnings |
8 | 9 |
|
9 | 10 | import stripe
|
10 | 11 | from urllib.parse import parse_qsl, quote_plus
|
11 | 12 |
|
12 | 13 | from typing_extensions import Type, TYPE_CHECKING
|
13 | 14 | from typing import (
|
| 15 | + Callable, |
14 | 16 | TypeVar,
|
15 | 17 | Union,
|
16 | 18 | overload,
|
|
22 | 24 | )
|
23 | 25 |
|
24 | 26 | from stripe.stripe_response import StripeResponse
|
| 27 | +import typing_extensions |
25 | 28 |
|
26 | 29 | if TYPE_CHECKING:
|
27 | 30 | from stripe.stripe_object import StripeObject
|
|
37 | 40 | "log_debug",
|
38 | 41 | "dashboard_link",
|
39 | 42 | "logfmt",
|
| 43 | + "deprecated", |
40 | 44 | ]
|
41 | 45 |
|
| 46 | +if hasattr(typing_extensions, "deprecated"): |
| 47 | + deprecated = typing_extensions.deprecated |
| 48 | +elif not TYPE_CHECKING: |
| 49 | + _T = TypeVar("_T") |
| 50 | + |
| 51 | + # Copied from python/typing_extensions, as this was added in typing_extensions 4.5.0 which is incompatible with |
| 52 | + # python 3.6. We still need `deprecated = typing_extensions.deprecated` in addition to this fallback, as |
| 53 | + # IDEs (pylance) specially detect references to symbols defined in `typing_extensions` |
| 54 | + # |
| 55 | + # https://github.com/python/typing_extensions/blob/5d20e9eed31de88667542ba5a6f66e6dc439b681/src/typing_extensions.py#L2289-L2370 |
| 56 | + def deprecated( |
| 57 | + __msg: str, |
| 58 | + *, |
| 59 | + category: Optional[Type[Warning]] = DeprecationWarning, |
| 60 | + stacklevel: int = 1, |
| 61 | + ) -> Callable[[_T], _T]: |
| 62 | + def decorator(__arg: _T) -> _T: |
| 63 | + if category is None: |
| 64 | + __arg.__deprecated__ = __msg |
| 65 | + return __arg |
| 66 | + elif isinstance(__arg, type): |
| 67 | + original_new = __arg.__new__ |
| 68 | + has_init = __arg.__init__ is not object.__init__ |
| 69 | + |
| 70 | + @functools.wraps(original_new) |
| 71 | + def __new__(cls, *args, **kwargs): |
| 72 | + warnings.warn( |
| 73 | + __msg, category=category, stacklevel=stacklevel + 1 |
| 74 | + ) |
| 75 | + if original_new is not object.__new__: |
| 76 | + return original_new(cls, *args, **kwargs) |
| 77 | + # Mirrors a similar check in object.__new__. |
| 78 | + elif not has_init and (args or kwargs): |
| 79 | + raise TypeError(f"{cls.__name__}() takes no arguments") |
| 80 | + else: |
| 81 | + return original_new(cls) |
| 82 | + |
| 83 | + __arg.__new__ = staticmethod(__new__) |
| 84 | + __arg.__deprecated__ = __new__.__deprecated__ = __msg |
| 85 | + return __arg |
| 86 | + elif callable(__arg): |
| 87 | + |
| 88 | + @functools.wraps(__arg) |
| 89 | + def wrapper(*args, **kwargs): |
| 90 | + warnings.warn( |
| 91 | + __msg, category=category, stacklevel=stacklevel + 1 |
| 92 | + ) |
| 93 | + return __arg(*args, **kwargs) |
| 94 | + |
| 95 | + __arg.__deprecated__ = wrapper.__deprecated__ = __msg |
| 96 | + return wrapper |
| 97 | + else: |
| 98 | + raise TypeError( |
| 99 | + "@deprecated decorator with non-None category must be applied to " |
| 100 | + f"a class or callable, not {__arg!r}" |
| 101 | + ) |
| 102 | + |
| 103 | + return decorator |
| 104 | + |
42 | 105 |
|
43 | 106 | def is_appengine_dev():
|
44 | 107 | return "APPENGINE_RUNTIME" in os.environ and "Dev" in os.environ.get(
|
|
0 commit comments