Debian Codegraph @

django_cte/_deprecated.py

raw
1 try:
2 from warnings import deprecated
3 except ImportError:
4 from warnings import warn
5
6 # Copied from Python 3.13, lightly modified for Python 3.9 compatibility.
7 # Can be removed when the oldest supported Python version is 3.13.
8 class deprecated:
9 """Indicate that a class, function or overload is deprecated.
10
11 When this decorator is applied to an object, the type checker
12 will generate a diagnostic on usage of the deprecated object.
13
14 Usage:
15
16 @deprecated("Use B instead")
17 class A:
18 pass
19
20 @deprecated("Use g instead")
21 def f():
22 pass
23
24 @overload
25 @deprecated("int support is deprecated")
26 def g(x: int) -> int: ...
27 @overload
28 def g(x: str) -> int: ...
29
30 The warning specified by *category* will be emitted at runtime
31 on use of deprecated objects. For functions, that happens on calls;
32 for classes, on instantiation and on creation of subclasses.
33 If the *category* is ``None``, no warning is emitted at runtime.
34 The *stacklevel* determines where the
35 warning is emitted. If it is ``1`` (the default), the warning
36 is emitted at the direct caller of the deprecated object; if it
37 is higher, it is emitted further up the stack.
38 Static type checker behavior is not affected by the *category*
39 and *stacklevel* arguments.
40
41 The deprecation message passed to the decorator is saved in the
42 ``__deprecated__`` attribute on the decorated object.
43 If applied to an overload, the decorator
44 must be after the ``@overload`` decorator for the attribute to
45 exist on the overload as returned by ``get_overloads()``.
46
47 See PEP 702 for details.
48
49 """
50 def __init__(
51 self,
52 message: str,
53 /,
54 *,
55 category=DeprecationWarning,
56 stacklevel=1,
57 ):
58 if not isinstance(message, str):
59 raise TypeError(
60 f"Expected an object of type str for 'message', not {type(message).__name__!r}"
61 )
62 self.message = message
63 self.category = category
64 self.stacklevel = stacklevel
65
66 def __call__(self, arg, /):
67 # Make sure the inner functions created below don't
68 # retain a reference to self.
69 msg = self.message
70 category = self.category
71 stacklevel = self.stacklevel
72 if category is None:
73 arg.__deprecated__ = msg
74 return arg
75 elif isinstance(arg, type):
76 import functools
77 from types import MethodType
78
79 original_new = arg.__new__
80
81 @functools.wraps(original_new)
82 def __new__(cls, /, *args, **kwargs):
83 if cls is arg:
84 warn(msg, category=category, stacklevel=stacklevel + 1)
85 if original_new is not object.__new__:
86 return original_new(cls, *args, **kwargs)
87 # Mirrors a similar check in object.__new__.
88 elif cls.__init__ is object.__init__ and (args or kwargs):
89 raise TypeError(f"{cls.__name__}() takes no arguments")
90 else:
91 return original_new(cls)
92
93 arg.__new__ = staticmethod(__new__)
94
95 original_init_subclass = arg.__init_subclass__
96 # We need slightly different behavior if __init_subclass__
97 # is a bound method (likely if it was implemented in Python)
98 if isinstance(original_init_subclass, MethodType):
99 original_init_subclass = original_init_subclass.__func__
100
101 @functools.wraps(original_init_subclass)
102 def __init_subclass__(*args, **kwargs):
103 warn(msg, category=category, stacklevel=stacklevel + 1)
104 return original_init_subclass(*args, **kwargs)
105
106 arg.__init_subclass__ = classmethod(__init_subclass__)
107 # Or otherwise, which likely means it's a builtin such as
108 # object's implementation of __init_subclass__.
109 else:
110 @functools.wraps(original_init_subclass)
111 def __init_subclass__(*args, **kwargs):
112 warn(msg, category=category, stacklevel=stacklevel + 1)
113 return original_init_subclass(*args, **kwargs)
114
115 arg.__init_subclass__ = __init_subclass__
116
117 arg.__deprecated__ = __new__.__deprecated__ = msg
118 __init_subclass__.__deprecated__ = msg
119 return arg
120 elif callable(arg):
121 import functools
122 import inspect
123
124 @functools.wraps(arg)
125 def wrapper(*args, **kwargs):
126 warn(msg, category=category, stacklevel=stacklevel + 1)
127 return arg(*args, **kwargs)
128
129 if inspect.iscoroutinefunction(arg):
130 wrapper = inspect.markcoroutinefunction(wrapper)
131
132 arg.__deprecated__ = wrapper.__deprecated__ = msg
133 return wrapper
134 else:
135 raise TypeError(
136 "@deprecated decorator with non-None category must be applied to "
137 f"a class or callable, not {arg!r}"
138 )