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