Skip to content

Commit ecd7017

Browse files
committed
Fix #225: Deprecate module level functions
* Add test cases * In `setup.cfg`, add deprecation warnings filter for pytest * Implement DeprecationWarning with warnings module and the new decorator `deprecated` * Output a DeprecationWarning for the following functions: - semver.parse - semver.parse_version_info - semver.format_version - semver.bump_{major,minor,patch,prerelease,build} - semver.finalize_version - semver.replace Add also a deprecation notice in the docstrings of these functions * Update CHANGELOG.rst
1 parent 06cb11b commit ecd7017

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

CHANGELOG.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ Features
1818
Bug Fixes
1919
---------
2020

21-
<<<<<<< HEAD
2221
* :gh:`224` (:pr:`226`): In ``setup.py``, replaced in class ``clean``,
2322
``super(CleanCommand, self).run()`` with ``CleanCommand.run(self)``
24-
=======
25-
* :gh:`224` (:pr:`226`): Replaced in class ``clean``, ``super(CleanCommand, self).run()`` with ``CleanCommand.run(self)``
26-
>>>>>>> Fix #225: Deprecate module level functions
2723

2824

2925
Additions

semver.py

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,17 @@ def deprecated(replace=None):
3535
This function will be removed once major version 3 of semver is
3636
released.
3737
"""
38-
# we can't use the 'nonlocal' keyword in Python2, so we need
39-
# to circumvent that with a dictionary:
40-
r = {"r": replace}
4138

4239
def decorator(func):
40+
r = replace or func.__name__
41+
4342
@wraps(func)
4443
def wrapper(*args, **kwargs):
45-
# nonlocal replace
46-
replace = r["r"] if r["r"] is not None else func.__name__
4744
msg = (
4845
"Function 'semver.{f}' is deprecated. "
4946
"Use the respective 'semver.VersionInfo.{r}' instead."
5047
)
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)
5449
return func(*args, **kwargs)
5550

5651
return wrapper
@@ -64,7 +59,7 @@ def parse(version):
6459
Parse version to major, minor, patch, pre-release, build parts.
6560
6661
.. deprecated:: 2.9.2
67-
Use :func:`semver.VersionInfo.format_parse` instead.
62+
Use :func:`semver.VersionInfo.parse` instead.
6863
6964
:param version: version string
7065
:return: dictionary with the keys 'build', 'major', 'minor', 'patch',
@@ -318,11 +313,30 @@ def __repr__(self):
318313
return "%s(%s)" % (type(self).__name__, s)
319314

320315
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
322323

323324
def __hash__(self):
324325
return hash(self._astuple())
325326

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+
326340
@staticmethod
327341
def parse(version):
328342
"""
@@ -386,31 +400,6 @@ def isvalid(cls, version):
386400
except ValueError:
387401
return False
388402

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-
414403

415404
def _to_dict(obj):
416405
if isinstance(obj, VersionInfo):
@@ -428,6 +417,9 @@ def parse_version_info(version):
428417
.. deprecated:: 2.9.2
429418
Use :func:`semver.VersionInfo.parse` instead.
430419
420+
.. versionadded:: 2.7.2
421+
Added :func:`parse_version_info`
422+
431423
:param version: version string
432424
:return: a :class:`VersionInfo` instance
433425
:rtype: :class:`VersionInfo`
@@ -610,7 +602,7 @@ def format_version(major, minor, patch, prerelease=None, build=None):
610602
Format a version according to the Semantic Versioning specification.
611603
612604
.. deprecated:: 2.9.2
613-
Use :func:`semver.VersionInfo.format_version` instead.
605+
Use ``str(VersionInfo(VERSION)`` instead.
614606
615607
:param int major: the required major part of a version
616608
:param int minor: the required minor part of a version
@@ -623,7 +615,7 @@ def format_version(major, minor, patch, prerelease=None, build=None):
623615
>>> semver.format_version(3, 4, 5, 'pre.2', 'build.4')
624616
'3.4.5-pre.2+build.4'
625617
"""
626-
return VersionInfo.format_version(major, minor, patch, prerelease, build)
618+
return str(VersionInfo(major, minor, patch, prerelease, build))
627619

628620

629621
def _increment_string(string):
@@ -738,7 +730,7 @@ def finalize_version(version):
738730
Remove any prerelease and build metadata from the version.
739731
740732
.. deprecated:: 2.9.2
741-
Use :func:`semver.VersionInfo.bump_format_version` instead.
733+
Use :func:`semver.VersionInfo.finalize_version` instead.
742734
743735
:param version: version string
744736
:return: the finalized version string
@@ -748,7 +740,7 @@ def finalize_version(version):
748740
'1.2.3'
749741
"""
750742
verinfo = VersionInfo.parse(version)
751-
return VersionInfo.format_version(verinfo.major, verinfo.minor, verinfo.patch)
743+
return str(verinfo.finalize_version())
752744

753745

754746
@deprecated()

0 commit comments

Comments
 (0)