@@ -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
"""
@@ -386,31 +400,6 @@ def isvalid(cls, version):
386
400
except ValueError :
387
401
return False
388
402
389
- @staticmethod
390
- def format_version (major , minor , patch , prerelease = None , build = None ):
391
- """
392
- Format a version according to the Semantic Versioning specification.
393
-
394
- :param int major: the required major part of a version
395
- :param int minor: the required minor part of a version
396
- :param int patch: the required patch part of a version
397
- :param str prerelease: the optional prerelease part of a version
398
- :param str build: the optional build part of a version
399
- :return: the formatted string
400
- :rtype: str
401
-
402
- >>> semver.VersionInfo.format_version(3, 4, 5, 'pre.2', 'build.4')
403
- '3.4.5-pre.2+build.4'
404
- """
405
- version = "%d.%d.%d" % (major , minor , patch )
406
- if prerelease is not None :
407
- version = version + "-%s" % prerelease
408
-
409
- if build is not None :
410
- version = version + "+%s" % build
411
-
412
- return version
413
-
414
403
415
404
def _to_dict (obj ):
416
405
if isinstance (obj , VersionInfo ):
@@ -428,6 +417,9 @@ def parse_version_info(version):
428
417
.. deprecated:: 2.9.2
429
418
Use :func:`semver.VersionInfo.parse` instead.
430
419
420
+ .. versionadded:: 2.7.2
421
+ Added :func:`parse_version_info`
422
+
431
423
:param version: version string
432
424
:return: a :class:`VersionInfo` instance
433
425
:rtype: :class:`VersionInfo`
@@ -610,7 +602,7 @@ def format_version(major, minor, patch, prerelease=None, build=None):
610
602
Format a version according to the Semantic Versioning specification.
611
603
612
604
.. deprecated:: 2.9.2
613
- Use :func:`semver. VersionInfo.format_version ` instead.
605
+ Use ``str( VersionInfo(VERSION)` ` instead.
614
606
615
607
:param int major: the required major part of a version
616
608
:param int minor: the required minor part of a version
@@ -623,7 +615,7 @@ def format_version(major, minor, patch, prerelease=None, build=None):
623
615
>>> semver.format_version(3, 4, 5, 'pre.2', 'build.4')
624
616
'3.4.5-pre.2+build.4'
625
617
"""
626
- return VersionInfo . format_version (major , minor , patch , prerelease , build )
618
+ return str ( VersionInfo (major , minor , patch , prerelease , build ) )
627
619
628
620
629
621
def _increment_string (string ):
@@ -738,7 +730,7 @@ def finalize_version(version):
738
730
Remove any prerelease and build metadata from the version.
739
731
740
732
.. deprecated:: 2.9.2
741
- Use :func:`semver.VersionInfo.bump_format_version ` instead.
733
+ Use :func:`semver.VersionInfo.finalize_version ` instead.
742
734
743
735
:param version: version string
744
736
:return: the finalized version string
@@ -748,7 +740,7 @@ def finalize_version(version):
748
740
'1.2.3'
749
741
"""
750
742
verinfo = VersionInfo .parse (version )
751
- return VersionInfo . format_version (verinfo .major , verinfo . minor , verinfo . patch )
743
+ return str (verinfo .finalize_version () )
752
744
753
745
754
746
@deprecated ()
0 commit comments