@@ -35,22 +35,17 @@ def deprecated(replace=None):
35
35
This function will be removed once major version 3 of semver is
36
36
released.
37
37
"""
38
- # we can't use the 'nonlocal' keyword in Python2, so we need
39
- # to circumvent that with a dictionary:
40
- r = {"r" : replace }
41
38
42
39
def decorator (func ):
40
+ r = replace or func .__name__
41
+
43
42
@wraps (func )
44
43
def wrapper (* args , ** kwargs ):
45
- # nonlocal replace
46
- replace = r ["r" ] if r ["r" ] is not None else func .__name__
47
44
msg = (
48
45
"Function 'semver.{f}' is deprecated. "
49
46
"Use the respective 'semver.VersionInfo.{r}' instead."
50
47
)
51
- warnings .warn (
52
- msg .format (f = func .__name__ , r = replace ), category = DeprecationWarning
53
- )
48
+ warnings .warn (msg .format (f = func .__name__ , r = r ), category = DeprecationWarning )
54
49
return func (* args , ** kwargs )
55
50
56
51
return wrapper
@@ -64,7 +59,7 @@ def parse(version):
64
59
Parse version to major, minor, patch, pre-release, build parts.
65
60
66
61
.. deprecated:: 2.9.2
67
- Use :func:`semver.VersionInfo.format_parse ` instead.
62
+ Use :func:`semver.VersionInfo.parse ` instead.
68
63
69
64
:param version: version string
70
65
:return: dictionary with the keys 'build', 'major', 'minor', 'patch',
@@ -318,11 +313,30 @@ def __repr__(self):
318
313
return "%s(%s)" % (type (self ).__name__ , s )
319
314
320
315
def __str__ (self ):
321
- return VersionInfo .format_version (* (self ._astuple ()))
316
+ """str(self)"""
317
+ version = "%d.%d.%d" % (self .major , self .minor , self .patch )
318
+ if self .prerelease :
319
+ version += "-%s" % self .prerelease
320
+ if self .build :
321
+ version += "+%s" % self .build
322
+ return version
322
323
323
324
def __hash__ (self ):
324
325
return hash (self ._astuple ())
325
326
327
+ def finalize_version (self ):
328
+ """
329
+ Remove any prerelease and build metadata from the version.
330
+
331
+ :return: a new instance with the finalized version string
332
+ :rtype: :class:`VersionInfo`
333
+
334
+ >>> str(semver.VersionInfo.parse('1.2.3-rc.5').finalize_version())
335
+ '1.2.3'
336
+ """
337
+ cls = type (self )
338
+ return cls (self .major , self .minor , self .patch )
339
+
326
340
@staticmethod
327
341
def parse (version ):
328
342
"""
@@ -392,31 +406,6 @@ def isvalid(cls, version):
392
406
except ValueError :
393
407
return False
394
408
395
- @staticmethod
396
- def format_version (major , minor , patch , prerelease = None , build = None ):
397
- """
398
- Format a version according to the Semantic Versioning specification.
399
-
400
- :param int major: the required major part of a version
401
- :param int minor: the required minor part of a version
402
- :param int patch: the required patch part of a version
403
- :param str prerelease: the optional prerelease part of a version
404
- :param str build: the optional build part of a version
405
- :return: the formatted string
406
- :rtype: str
407
-
408
- >>> semver.VersionInfo.format_version(3, 4, 5, 'pre.2', 'build.4')
409
- '3.4.5-pre.2+build.4'
410
- """
411
- version = "%d.%d.%d" % (major , minor , patch )
412
- if prerelease is not None :
413
- version = version + "-%s" % prerelease
414
-
415
- if build is not None :
416
- version = version + "+%s" % build
417
-
418
- return version
419
-
420
409
421
410
def _to_dict (obj ):
422
411
if isinstance (obj , VersionInfo ):
@@ -437,6 +426,9 @@ def parse_version_info(version):
437
426
.. deprecated:: 2.9.2
438
427
Use :func:`semver.VersionInfo.parse` instead.
439
428
429
+ .. versionadded:: 2.7.2
430
+ Added :func:`parse_version_info`
431
+
440
432
:param version: version string
441
433
:return: a :class:`VersionInfo` instance
442
434
:rtype: :class:`VersionInfo`
@@ -619,7 +611,7 @@ def format_version(major, minor, patch, prerelease=None, build=None):
619
611
Format a version string according to the Semantic Versioning specification.
620
612
621
613
.. deprecated:: 2.9.2
622
- Use :func:`semver. VersionInfo.format_version ` instead.
614
+ Use ``str( VersionInfo(VERSION)` ` instead.
623
615
624
616
:param int major: the required major part of a version
625
617
:param int minor: the required minor part of a version
@@ -632,7 +624,7 @@ def format_version(major, minor, patch, prerelease=None, build=None):
632
624
>>> semver.format_version(3, 4, 5, 'pre.2', 'build.4')
633
625
'3.4.5-pre.2+build.4'
634
626
"""
635
- return VersionInfo . format_version (major , minor , patch , prerelease , build )
627
+ return str ( VersionInfo (major , minor , patch , prerelease , build ) )
636
628
637
629
638
630
def _increment_string (string ):
@@ -750,7 +742,7 @@ def finalize_version(version):
750
742
Added :func:`finalize_version`
751
743
752
744
.. deprecated:: 2.9.2
753
- Use :func:`semver.VersionInfo.bump_format_version ` instead.
745
+ Use :func:`semver.VersionInfo.finalize_version ` instead.
754
746
755
747
:param version: version string
756
748
:return: the finalized version string
@@ -760,14 +752,17 @@ def finalize_version(version):
760
752
'1.2.3'
761
753
"""
762
754
verinfo = VersionInfo .parse (version )
763
- return VersionInfo . format_version (verinfo .major , verinfo . minor , verinfo . patch )
755
+ return str (verinfo .finalize_version () )
764
756
765
757
766
758
@deprecated ()
767
759
def replace (version , ** parts ):
768
760
"""
769
761
Replace one or more parts of a version and return the new string.
770
762
763
+ .. versionadded:: 2.9.0
764
+ Added :func:`replace`
765
+
771
766
.. deprecated:: 2.9.2
772
767
Use :func:`semver.VersionInfo.replace` instead.
773
768
@@ -927,28 +922,6 @@ def main(cliargs=None):
927
922
return 2
928
923
929
924
930
- def replace (version , ** parts ):
931
- """
932
- Replace one or more parts of a version and return the new string.
933
-
934
- .. versionadded:: 2.9.0
935
- Added :func:`replace`
936
-
937
- :param str version: the version string to replace
938
- :param dict parts: the parts to be updated. Valid keys are:
939
- ``major``, ``minor``, ``patch``, ``prerelease``, or ``build``
940
- :return: the replaced version string
941
- :raises: TypeError, if ``parts`` contains invalid keys
942
- :rtype: str
943
-
944
- >>> import semver
945
- >>> semver.replace("1.2.3", major=2, patch=10)
946
- '2.2.10'
947
- """
948
- version = parse_version_info (version )
949
- return str (version .replace (** parts ))
950
-
951
-
952
925
if __name__ == "__main__" :
953
926
import doctest
954
927
0 commit comments