forked from MemTensor/MemOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeprecation.py
More file actions
262 lines (198 loc) · 7.23 KB
/
deprecation.py
File metadata and controls
262 lines (198 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
"""
This module provides utilities for marking functions, classes, and parameters
as deprecated. It includes decorators for deprecation, a function to issue
warnings, and utilities to check deprecation status.
"""
import functools
import warnings
from collections.abc import Callable
from typing import Any, TypeVar
warnings.simplefilter("default", DeprecationWarning)
F = TypeVar("F", bound=Callable[..., Any])
C = TypeVar("C", bound=type)
def deprecated(
reason: str | None = None,
version: str | None = None,
alternative: str | None = None,
category: type[Warning] = DeprecationWarning,
stacklevel: int = 2,
) -> Callable[[F], F]:
"""
Decorator to mark functions as deprecated.
Args:
reason: Optional reason for deprecation
version: Version when the function was deprecated
alternative: Suggested alternative function/method
category: Warning category to use
stacklevel: Stack level for the warning
Example:
@deprecated(reason="Use new_function instead", version="1.2.0")
def old_function():
pass
"""
def decorator(func: F) -> F:
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Build deprecation message
msg_parts = [f"Function '{func.__name__}' is deprecated"]
if version:
msg_parts.append(f"since version {version}")
if reason:
msg_parts.append(f"- {reason}")
if alternative:
msg_parts.append(f"Use '{alternative}' instead")
message = ". ".join(msg_parts) + "."
warnings.warn(message, category=category, stacklevel=stacklevel)
return func(*args, **kwargs)
# Mark the wrapper as deprecated for introspection
wrapper.__deprecated__ = True
wrapper.__deprecation_info__ = {
"reason": reason,
"version": version,
"alternative": alternative,
"category": category,
}
return wrapper
return decorator
def deprecated_class(
reason: str | None = None,
version: str | None = None,
alternative: str | None = None,
category: type[Warning] = DeprecationWarning,
stacklevel: int = 2,
) -> Callable[[C], C]:
"""
Decorator to mark classes as deprecated.
Args:
reason: Optional reason for deprecation
version: Version when the class was deprecated
alternative: Suggested alternative class
category: Warning category to use
stacklevel: Stack level for the warning
Example:
@deprecated_class(reason="Use NewClass instead", version="1.2.0")
class OldClass:
pass
"""
def decorator(cls: C) -> C:
# Store original __init__
original_init = cls.__init__
@functools.wraps(original_init)
def new_init(self, *args, **kwargs):
# Build deprecation message
msg_parts = [f"Class '{cls.__name__}' is deprecated"]
if version:
msg_parts.append(f"since version {version}")
if reason:
msg_parts.append(f"- {reason}")
if alternative:
msg_parts.append(f"Use '{alternative}' instead")
message = ". ".join(msg_parts) + "."
warnings.warn(message, category=category, stacklevel=stacklevel)
original_init(self, *args, **kwargs)
# Replace __init__
cls.__init__ = new_init
# Mark the class as deprecated for introspection
cls.__deprecated__ = True
cls.__deprecation_info__ = {
"reason": reason,
"version": version,
"alternative": alternative,
"category": category,
}
return cls
return decorator
def deprecated_parameter(
parameter_name: str,
reason: str | None = None,
version: str | None = None,
alternative: str | None = None,
category: type[Warning] = DeprecationWarning,
stacklevel: int = 2,
) -> Callable[[F], F]:
"""
Decorator to mark specific parameters as deprecated.
Args:
parameter_name: Name of the deprecated parameter
reason: Optional reason for deprecation
version: Version when the parameter was deprecated
alternative: Suggested alternative parameter
category: Warning category to use
stacklevel: Stack level for the warning
Example:
@deprecated_parameter("old_param", alternative="new_param", version="1.2.0")
def my_function(new_param=None, old_param=None):
pass
"""
def decorator(func: F) -> F:
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Check if deprecated parameter is used
if parameter_name in kwargs:
# Build deprecation message
msg_parts = [
f"Parameter '{parameter_name}' in function '{func.__name__}' is deprecated"
]
if version:
msg_parts.append(f"since version {version}")
if reason:
msg_parts.append(f"- {reason}")
if alternative:
msg_parts.append(f"Use parameter '{alternative}' instead")
message = ". ".join(msg_parts) + "."
warnings.warn(message, category=category, stacklevel=stacklevel)
return func(*args, **kwargs)
return wrapper
return decorator
def warn_deprecated(
item_name: str,
item_type: str = "feature",
reason: str | None = None,
version: str | None = None,
alternative: str | None = None,
category: type[Warning] = DeprecationWarning,
stacklevel: int = 2,
) -> None:
"""
Issue a deprecation warning for any item.
Args:
item_name: Name of the deprecated item
item_type: Type of item (e.g., "function", "class", "parameter", "feature")
reason: Optional reason for deprecation
version: Version when the item was deprecated
alternative: Suggested alternative
category: Warning category to use
stacklevel: Stack level for the warning
Example:
warn_deprecated("old_method", "method", version="1.2.0", alternative="new_method")
"""
# Build deprecation message
msg_parts = [f"{item_type.capitalize()} '{item_name}' is deprecated"]
if version:
msg_parts.append(f"since version {version}")
if reason:
msg_parts.append(f"- {reason}")
if alternative:
msg_parts.append(f"Use '{alternative}' instead")
message = ". ".join(msg_parts) + "."
warnings.warn(message, category=category, stacklevel=stacklevel)
def is_deprecated(obj: Any) -> bool:
"""
Check if an object is marked as deprecated.
Args:
obj: Object to check
Returns:
True if the object is deprecated, False otherwise
"""
return getattr(obj, "__deprecated__", False)
def get_deprecation_info(obj: Any) -> dict | None:
"""
Get deprecation information for an object.
Args:
obj: Object to get deprecation info for
Returns:
Dictionary with deprecation info or None if not deprecated
"""
if is_deprecated(obj):
return getattr(obj, "__deprecation_info__", None)
return None