Skip to content

Commit a32df30

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 c3cdfd5 commit a32df30

File tree

4 files changed

+110
-12
lines changed

4 files changed

+110
-12
lines changed

CHANGELOG.rst

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

21-
* :gh:`224` (:pr:`226`): Replaced in class ``clean``, ``super(CleanCommand, self).run()`` with
22-
``CleanCommand.run(self)``
21+
* :gh:`224` (:pr:`226`): Replaced in class ``clean``, ``super(CleanCommand, self).run()`` with ``CleanCommand.run(self)``
2322

2423

2524
Removals
2625
--------
26+
* :gh:`225` (:pr:`229`): Output a DeprecationWarning for the following functions:
27+
28+
- ``semver.parse``
29+
- ``semver.parse_version_info``
30+
- ``semver.format_version``
31+
- ``semver.bump_{major,minor,patch,prerelease,build}``
32+
- ``semver.finalize_version``
33+
- ``semver.replace``
34+
35+
These functions will be removed in major 3 of semver.
36+
2737

2838

2939
Version 2.9.1

semver.py

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from functools import wraps
77
import re
88
import sys
9+
import warnings
910

1011

1112
__version__ = "2.9.1"
@@ -27,10 +28,44 @@ def cmp(a, b):
2728
return (a > b) - (a < b)
2829

2930

31+
def deprecated(replace=None):
32+
"""
33+
Decorates a function to output a deprecation warning.
34+
35+
This function will be removed once major version 3 of semver is
36+
released.
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+
42+
def decorator(func):
43+
@wraps(func)
44+
def wrapper(*args, **kwargs):
45+
# nonlocal replace
46+
replace = r["r"] if r["r"] is not None else func.__name__
47+
msg = (
48+
"Function 'semver.{f}' is deprecated. "
49+
"Use the respective 'semver.VersionInfo.{r}' instead."
50+
)
51+
warnings.warn(
52+
msg.format(f=func.__name__, r=replace), category=DeprecationWarning
53+
)
54+
return func(*args, **kwargs)
55+
56+
return wrapper
57+
58+
return decorator
59+
60+
61+
@deprecated()
3062
def parse(version):
3163
"""
3264
Parse version to major, minor, patch, pre-release, build parts.
3365
66+
.. deprecated:: 2.9.2
67+
Use :func:`semver.VersionInfo.format_parse` instead.
68+
3469
:param version: version string
3570
:return: dictionary with the keys 'build', 'major', 'minor', 'patch',
3671
and 'prerelease'. The prerelease or build keys can be None
@@ -186,7 +221,7 @@ def bump_major(self):
186221
VersionInfo(major=4, minor=0, patch=0, prerelease=None, build=None)
187222
"""
188223
cls = type(self)
189-
return cls(self._major+1)
224+
return cls(self._major + 1)
190225

191226
def bump_minor(self):
192227
"""
@@ -201,7 +236,7 @@ def bump_minor(self):
201236
VersionInfo(major=3, minor=5, patch=0, prerelease=None, build=None)
202237
"""
203238
cls = type(self)
204-
return cls(self._major, self._minor+1)
239+
return cls(self._major, self._minor + 1)
205240

206241
def bump_patch(self):
207242
"""
@@ -216,7 +251,7 @@ def bump_patch(self):
216251
VersionInfo(major=3, minor=4, patch=6, prerelease=None, build=None)
217252
"""
218253
cls = type(self)
219-
return cls(self._major, self._minor, self._patch+1)
254+
return cls(self._major, self._minor, self._patch + 1)
220255

221256
def bump_prerelease(self, token="rc"):
222257
"""
@@ -233,9 +268,7 @@ def bump_prerelease(self, token="rc"):
233268
build=None)
234269
"""
235270
cls = type(self)
236-
prerelease = _increment_string(
237-
self._prerelease or (token or "rc") + ".0"
238-
)
271+
prerelease = _increment_string(self._prerelease or (token or "rc") + ".0")
239272
return cls(self._major, self._minor, self._patch, prerelease)
240273

241274
def bump_build(self, token="build"):
@@ -253,9 +286,7 @@ def bump_build(self, token="build"):
253286
build='build.10')
254287
"""
255288
cls = type(self)
256-
build = _increment_string(
257-
self._build or (token or "build") + ".0"
258-
)
289+
build = _increment_string(self._build or (token or "build") + ".0")
259290
return cls(self._major, self._minor, self._patch, self._prerelease, build)
260291

261292
@comparator
@@ -389,10 +420,14 @@ def _to_dict(obj):
389420
return obj
390421

391422

423+
@deprecated("parse")
392424
def parse_version_info(version):
393425
"""
394426
Parse version string to a VersionInfo instance.
395427
428+
.. deprecated:: 2.9.2
429+
Use :func:`semver.VersionInfo.parse` instead.
430+
396431
:param version: version string
397432
:return: a :class:`VersionInfo` instance
398433
:rtype: :class:`VersionInfo`
@@ -569,10 +604,14 @@ def min_ver(ver1, ver2):
569604
return ver2
570605

571606

607+
@deprecated()
572608
def format_version(major, minor, patch, prerelease=None, build=None):
573609
"""
574610
Format a version according to the Semantic Versioning specification.
575611
612+
.. deprecated:: 2.9.2
613+
Use :func:`semver.VersionInfo.format_version` instead.
614+
576615
:param int major: the required major part of a version
577616
:param int minor: the required minor part of a version
578617
:param int patch: the required patch part of a version
@@ -601,10 +640,14 @@ def _increment_string(string):
601640
return string
602641

603642

643+
@deprecated()
604644
def bump_major(version):
605645
"""
606646
Raise the major part of the version.
607647
648+
.. deprecated:: 2.9.2
649+
Use :func:`semver.VersionInfo.bump_major` instead.
650+
608651
:param: version string
609652
:return: the raised version string
610653
:rtype: str
@@ -615,10 +658,14 @@ def bump_major(version):
615658
return str(VersionInfo.parse(version).bump_major())
616659

617660

661+
@deprecated()
618662
def bump_minor(version):
619663
"""
620664
Raise the minor part of the version.
621665
666+
.. deprecated:: 2.9.2
667+
Use :func:`semver.VersionInfo.bump_minor` instead.
668+
622669
:param: version string
623670
:return: the raised version string
624671
:rtype: str
@@ -629,10 +676,14 @@ def bump_minor(version):
629676
return str(VersionInfo.parse(version).bump_minor())
630677

631678

679+
@deprecated()
632680
def bump_patch(version):
633681
"""
634682
Raise the patch part of the version.
635683
684+
.. deprecated:: 2.9.2
685+
Use :func:`semver.VersionInfo.bump_patch` instead.
686+
636687
:param: version string
637688
:return: the raised version string
638689
:rtype: str
@@ -643,10 +694,14 @@ def bump_patch(version):
643694
return str(VersionInfo.parse(version).bump_patch())
644695

645696

697+
@deprecated()
646698
def bump_prerelease(version, token="rc"):
647699
"""
648700
Raise the prerelease part of the version.
649701
702+
.. deprecated:: 2.9.2
703+
Use :func:`semver.VersionInfo.bump_prerelease` instead.
704+
650705
:param version: version string
651706
:param token: defaults to 'rc'
652707
:return: the raised version string
@@ -658,10 +713,14 @@ def bump_prerelease(version, token="rc"):
658713
return str(VersionInfo.parse(version).bump_prerelease(token))
659714

660715

716+
@deprecated()
661717
def bump_build(version, token="build"):
662718
"""
663719
Raise the build part of the version.
664720
721+
.. deprecated:: 2.9.2
722+
Use :func:`semver.VersionInfo.bump_build` instead.
723+
665724
:param version: version string
666725
:param token: defaults to 'build'
667726
:return: the raised version string
@@ -673,10 +732,14 @@ def bump_build(version, token="build"):
673732
return str(VersionInfo.parse(version).bump_build(token))
674733

675734

735+
@deprecated()
676736
def finalize_version(version):
677737
"""
678738
Remove any prerelease and build metadata from the version.
679739
740+
.. deprecated:: 2.9.2
741+
Use :func:`semver.VersionInfo.bump_format_version` instead.
742+
680743
:param version: version string
681744
:return: the finalized version string
682745
:rtype: str
@@ -688,10 +751,14 @@ def finalize_version(version):
688751
return VersionInfo.format_version(verinfo.major, verinfo.minor, verinfo.patch)
689752

690753

754+
@deprecated()
691755
def replace(version, **parts):
692756
"""
693757
Replace one or more parts of a version and return the new string.
694758
759+
.. deprecated:: 2.9.2
760+
Use :func:`semver.VersionInfo.replace` instead.
761+
695762
:param str version: the version string to replace
696763
:param dict parts: the parts to be updated. Valid keys are:
697764
``major``, ``minor``, ``patch``, ``prerelease``, or ``build``

setup.cfg

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
[tool:pytest]
22
norecursedirs = .git build .env/ env/ .pyenv/ .tmp/ .eggs/
3+
filterwarnings =
4+
ignore:Function 'semver.*:DeprecationWarning
35
addopts =
46
--ignore=.eggs/
57
--no-cov-on-fail
@@ -17,4 +19,4 @@ exclude =
1719
.git,
1820
__pycache__,
1921
build,
20-
dist
22+
dist

test_semver.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,3 +827,22 @@ def test_replace_raises_ValueError_for_non_numeric_values():
827827
def test_should_versioninfo_isvalid():
828828
assert VersionInfo.isvalid("1.0.0") is True
829829
assert VersionInfo.isvalid("foo") is False
830+
831+
832+
@pytest.mark.parametrize(
833+
"func, args, kwargs",
834+
[
835+
(bump_build, ("1.2.3",), {}),
836+
(bump_major, ("1.2.3",), {}),
837+
(bump_minor, ("1.2.3",), {}),
838+
(bump_patch, ("1.2.3",), {}),
839+
(bump_prerelease, ("1.2.3",), {}),
840+
(format_version, (3, 4, 5), {}),
841+
(parse, ("1.2.3",), {}),
842+
(parse_version_info, ("1.2.3",), {}),
843+
(replace, ("1.2.3",), dict(major=2, patch=10)),
844+
],
845+
)
846+
def test_should_raise_deprecation_warnings(func, args, kwargs):
847+
with pytest.deprecated_call():
848+
func(*args, **kwargs)

0 commit comments

Comments
 (0)