3
3
4
4
import argparse
5
5
import collections
6
- from functools import wraps
6
+ from functools import wraps , partial
7
+ import inspect
7
8
import re
8
9
import sys
9
10
import warnings
@@ -28,35 +29,62 @@ def cmp(a, b):
28
29
return (a > b ) - (a < b )
29
30
30
31
31
- def deprecated (replace = None ):
32
+ def deprecated (func = None , replace = None , version = None , category = DeprecationWarning ):
32
33
"""
33
34
Decorates a function to output a deprecation warning.
34
35
35
- :param str replace: the name of the function which the deprecated
36
- function should be replaced with
37
-
38
36
This function will be removed once major version 4 of semver is
39
37
released.
40
- """
41
38
42
- def decorator (func ):
43
- r = replace or func .__name__ # __qualname__
39
+ :param str replace: the function to replace (use the full qualified
40
+ name like ``semver.VersionInfo.bump_major``.
41
+ :param str version: the first version when this function was deprecated.
42
+ :param category: allow you to specify the deprecation warning class
43
+ of your choice. By default, it's :class:`DeprecationWarning`, but
44
+ you can choose :class:`PendingDeprecationWarning``or a custom class.
45
+ """
44
46
47
+ def decorator ():
45
48
@wraps (func )
46
49
def wrapper (* args , ** kwargs ):
47
- msg = (
48
- "Function 'semver.{f}' is deprecated. "
49
- "Use the respective 'semver.VersionInfo.{r}' instead."
50
+ msg = ["Function 'semver.{f}' is deprecated." ]
51
+
52
+ if version :
53
+ msg .append ("Deprecated since version {v}." )
54
+
55
+ if replace :
56
+ msg .append ("Use {r!r} instead." )
57
+ else :
58
+ msg .append ("Use the respective 'semver.VersionInfo.{r}' instead." )
59
+
60
+ # warnings.simplefilter('always', category)
61
+ frame = inspect .currentframe ().f_back
62
+
63
+ msg = " " .join (msg )
64
+ msg = msg .format (f = func .__name__ , r = replace , v = version )
65
+ warnings .warn_explicit (
66
+ msg ,
67
+ category = category ,
68
+ # stacklevel=2,
69
+ filename = inspect .getfile (frame .f_code ),
70
+ lineno = frame .f_lineno ,
50
71
)
51
- warnings .warn (msg .format (f = func .__name__ , r = r ), category = DeprecationWarning )
72
+ # warnings.simplefilter('default', category)
73
+ # As recommended in the Python documentation
74
+ # https://docs.python.org/3/library/inspect.html#the-interpreter-stack
75
+ # better remove the interpreter stack:
76
+ del frame
52
77
return func (* args , ** kwargs )
53
78
54
79
return wrapper
55
80
56
- return decorator
81
+ if callable (func ):
82
+ return decorator ()
83
+ else :
84
+ return partial (deprecated , replace = replace , version = version , category = category )
57
85
58
86
59
- @deprecated ()
87
+ @deprecated (version = "2.9.2" )
60
88
def parse (version ):
61
89
"""
62
90
Parse version to major, minor, patch, pre-release, build parts.
@@ -229,8 +257,10 @@ def to_dict(self):
229
257
)
230
258
231
259
# For compatibility reasons:
232
- _astuple = deprecated ("to_tuple" )(to_tuple )
233
- _asdict = deprecated ("to_dict" )(to_dict )
260
+ _astuple = deprecated (replace = "semver.VersionInfo.to_tuple" , version = "2.9.2" )(
261
+ to_tuple
262
+ )
263
+ _asdict = deprecated (replace = "semver.VersionInfo.to_dict" , version = "2.9.2" )(to_dict )
234
264
235
265
def __iter__ (self ):
236
266
"""Implement iter(self)."""
@@ -450,7 +480,7 @@ def _to_dict(obj):
450
480
return obj
451
481
452
482
453
- @deprecated (" parse" )
483
+ @deprecated (replace = "semver.VersionInfo. parse" , version = "2.9.2 " )
454
484
def parse_version_info (version ):
455
485
"""
456
486
Parse version string to a VersionInfo instance.
@@ -640,7 +670,7 @@ def min_ver(ver1, ver2):
640
670
return ver2
641
671
642
672
643
- @deprecated ()
673
+ @deprecated (version = "2.9.2" )
644
674
def format_version (major , minor , patch , prerelease = None , build = None ):
645
675
"""
646
676
Format a version string according to the Semantic Versioning specification.
@@ -676,7 +706,7 @@ def _increment_string(string):
676
706
return string
677
707
678
708
679
- @deprecated ()
709
+ @deprecated (version = "2.9.2" )
680
710
def bump_major (version ):
681
711
"""
682
712
Raise the major part of the version string.
@@ -694,7 +724,7 @@ def bump_major(version):
694
724
return str (VersionInfo .parse (version ).bump_major ())
695
725
696
726
697
- @deprecated ()
727
+ @deprecated (version = "2.9.2" )
698
728
def bump_minor (version ):
699
729
"""
700
730
Raise the minor part of the version string.
@@ -712,7 +742,7 @@ def bump_minor(version):
712
742
return str (VersionInfo .parse (version ).bump_minor ())
713
743
714
744
715
- @deprecated ()
745
+ @deprecated (version = "2.9.2" )
716
746
def bump_patch (version ):
717
747
"""
718
748
Raise the patch part of the version string.
@@ -730,7 +760,7 @@ def bump_patch(version):
730
760
return str (VersionInfo .parse (version ).bump_patch ())
731
761
732
762
733
- @deprecated ()
763
+ @deprecated (version = "2.9.2" )
734
764
def bump_prerelease (version , token = "rc" ):
735
765
"""
736
766
Raise the prerelease part of the version string.
@@ -749,7 +779,7 @@ def bump_prerelease(version, token="rc"):
749
779
return str (VersionInfo .parse (version ).bump_prerelease (token ))
750
780
751
781
752
- @deprecated ()
782
+ @deprecated (version = "2.9.2" )
753
783
def bump_build (version , token = "build" ):
754
784
"""
755
785
Raise the build part of the version string.
@@ -768,7 +798,7 @@ def bump_build(version, token="build"):
768
798
return str (VersionInfo .parse (version ).bump_build (token ))
769
799
770
800
771
- @deprecated ()
801
+ @deprecated (version = "2.9.2" )
772
802
def finalize_version (version ):
773
803
"""
774
804
Remove any prerelease and build metadata from the version string.
@@ -790,7 +820,7 @@ def finalize_version(version):
790
820
return str (verinfo .finalize_version ())
791
821
792
822
793
- @deprecated ()
823
+ @deprecated (version = "2.9.2" )
794
824
def replace (version , ** parts ):
795
825
"""
796
826
Replace one or more parts of a version and return the new string.
0 commit comments