Datasets:

ArXiv:
Elron commited on
Commit
4168af9
1 Parent(s): 0ae21d3

Upload deprecation_utils.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. deprecation_utils.py +94 -0
deprecation_utils.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import functools
2
+ import warnings
3
+
4
+ from .settings_utils import get_constants
5
+
6
+ constants = get_constants()
7
+
8
+
9
+ class DeprecationError(Exception):
10
+ """Custom exception for deprecated versions."""
11
+
12
+ pass
13
+
14
+
15
+ def compare_versions(version1, version2):
16
+ """Compare two semantic versioning strings and determine their relationship.
17
+
18
+ Parameters:
19
+ - version1 (str): The first version string to compare.
20
+ - version2 (str): The second version string to compare.
21
+
22
+ Returns:
23
+ - int: -1 if version1 < version2, 1 if version1 > version2, 0 if equal.
24
+
25
+ Example:
26
+ >>> compare_versions("1.2.0", "1.2.3")
27
+ -1
28
+ >>> compare_versions("1.3.0", "1.2.8")
29
+ 1
30
+ >>> compare_versions("1.0.0", "1.0.0")
31
+ 0
32
+ """
33
+ parts1 = [int(part) for part in version1.split(".")]
34
+ parts2 = [int(part) for part in version2.split(".")]
35
+ length_difference = len(parts1) - len(parts2)
36
+ if length_difference > 0:
37
+ parts2.extend([0] * length_difference)
38
+ elif length_difference < 0:
39
+ parts1.extend([0] * (-length_difference))
40
+ for part1, part2 in zip(parts1, parts2):
41
+ if part1 < part2:
42
+ return -1
43
+ if part1 > part2:
44
+ return 1
45
+ return 0
46
+
47
+
48
+ def depraction_wrapper(obj, version, alt_text):
49
+ """A wrapper function for deprecation handling, issuing warnings or errors based on version comparison.
50
+
51
+ Args:
52
+ obj (callable): The object to be wrapped, typically a function or class method.
53
+ version (str): The version at which the object becomes deprecated.
54
+ alt_text (str): Additional text to display, usually suggests an alternative.
55
+
56
+ Returns:
57
+ callable: A wrapped version of the original object that checks for deprecation.
58
+ """
59
+
60
+ @functools.wraps(obj)
61
+ def wrapper(*args, **kwargs):
62
+ if constants.version < version:
63
+ warnings.warn(
64
+ f"{obj.__name__} is deprecated.", DeprecationWarning, stacklevel=2
65
+ )
66
+ elif constants.version >= version:
67
+ raise DeprecationError(f"{obj.__name__} is no longer supported.{alt_text}")
68
+ return obj(*args, **kwargs)
69
+
70
+ return wrapper
71
+
72
+
73
+ def deprecation(version, alternative=None):
74
+ """Decorator for marking functions or class methods as deprecated.
75
+
76
+ Args:
77
+ version (str): The version at which the function or method becomes deprecated.
78
+ alternative (str, optional): Suggested alternative to the deprecated functionality.
79
+
80
+ Returns:
81
+ callable: A decorator that can be applied to functions or class methods.
82
+ """
83
+
84
+ def decorator(obj):
85
+ alt_text = f" Use {alternative} instead." if alternative else ""
86
+ if callable(obj):
87
+ func = obj
88
+ elif hasattr(obj, "__init__"):
89
+ func = obj.__init__
90
+ else:
91
+ raise ValueError("Unsupported object type for deprecation.")
92
+ return depraction_wrapper(func, version, alt_text)
93
+
94
+ return decorator