Skip to content

Commit ac4b7ce

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 73f3c1f commit ac4b7ce

File tree

4 files changed

+112
-9
lines changed

4 files changed

+112
-9
lines changed

CHANGELOG.rst

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

21+
<<<<<<< HEAD
2122
* :gh:`224` (:pr:`226`): In ``setup.py``, replaced in class ``clean``,
2223
``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
2327

2428

2529
Additions
@@ -29,6 +33,17 @@ Additions
2933

3034
Removals
3135
--------
36+
* :gh:`225` (:pr:`229`): Output a DeprecationWarning for the following functions:
37+
38+
- ``semver.parse``
39+
- ``semver.parse_version_info``
40+
- ``semver.format_version``
41+
- ``semver.bump_{major,minor,patch,prerelease,build}``
42+
- ``semver.finalize_version``
43+
- ``semver.replace``
44+
45+
These functions will be removed in major 3 of semver.
46+
3247

3348

3449
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
@@ -395,13 +426,17 @@ def _to_dict(obj):
395426
return obj
396427

397428

429+
@deprecated("parse")
398430
def parse_version_info(version):
399431
"""
400432
Parse version string to a VersionInfo instance.
401433
402434
.. versionadded:: 2.7.2
403435
Added :func:`parse_version_info`
404436
437+
.. deprecated:: 2.9.2
438+
Use :func:`semver.VersionInfo.parse` instead.
439+
405440
:param version: version string
406441
:return: a :class:`VersionInfo` instance
407442
:rtype: :class:`VersionInfo`
@@ -578,10 +613,14 @@ def min_ver(ver1, ver2):
578613
return ver2
579614

580615

616+
@deprecated()
581617
def format_version(major, minor, patch, prerelease=None, build=None):
582618
"""
583619
Format a version string according to the Semantic Versioning specification.
584620
621+
.. deprecated:: 2.9.2
622+
Use :func:`semver.VersionInfo.format_version` instead.
623+
585624
:param int major: the required major part of a version
586625
:param int minor: the required minor part of a version
587626
:param int patch: the required patch part of a version
@@ -610,10 +649,14 @@ def _increment_string(string):
610649
return string
611650

612651

652+
@deprecated()
613653
def bump_major(version):
614654
"""
615655
Raise the major part of the version string.
616656
657+
.. deprecated:: 2.9.2
658+
Use :func:`semver.VersionInfo.bump_major` instead.
659+
617660
:param: version string
618661
:return: the raised version string
619662
:rtype: str
@@ -624,10 +667,14 @@ def bump_major(version):
624667
return str(VersionInfo.parse(version).bump_major())
625668

626669

670+
@deprecated()
627671
def bump_minor(version):
628672
"""
629673
Raise the minor part of the version string.
630674
675+
.. deprecated:: 2.9.2
676+
Use :func:`semver.VersionInfo.bump_minor` instead.
677+
631678
:param: version string
632679
:return: the raised version string
633680
:rtype: str
@@ -638,10 +685,14 @@ def bump_minor(version):
638685
return str(VersionInfo.parse(version).bump_minor())
639686

640687

688+
@deprecated()
641689
def bump_patch(version):
642690
"""
643691
Raise the patch part of the version string.
644692
693+
.. deprecated:: 2.9.2
694+
Use :func:`semver.VersionInfo.bump_patch` instead.
695+
645696
:param: version string
646697
:return: the raised version string
647698
:rtype: str
@@ -652,10 +703,14 @@ def bump_patch(version):
652703
return str(VersionInfo.parse(version).bump_patch())
653704

654705

706+
@deprecated()
655707
def bump_prerelease(version, token="rc"):
656708
"""
657709
Raise the prerelease part of the version string.
658710
711+
.. deprecated:: 2.9.2
712+
Use :func:`semver.VersionInfo.bump_prerelease` instead.
713+
659714
:param version: version string
660715
:param token: defaults to 'rc'
661716
:return: the raised version string
@@ -667,10 +722,14 @@ def bump_prerelease(version, token="rc"):
667722
return str(VersionInfo.parse(version).bump_prerelease(token))
668723

669724

725+
@deprecated()
670726
def bump_build(version, token="build"):
671727
"""
672728
Raise the build part of the version string.
673729
730+
.. deprecated:: 2.9.2
731+
Use :func:`semver.VersionInfo.bump_build` instead.
732+
674733
:param version: version string
675734
:param token: defaults to 'build'
676735
:return: the raised version string
@@ -682,13 +741,17 @@ def bump_build(version, token="build"):
682741
return str(VersionInfo.parse(version).bump_build(token))
683742

684743

744+
@deprecated()
685745
def finalize_version(version):
686746
"""
687747
Remove any prerelease and build metadata from the version string.
688748
689749
.. versionadded:: 2.7.9
690750
Added :func:`finalize_version`
691751
752+
.. deprecated:: 2.9.2
753+
Use :func:`semver.VersionInfo.bump_format_version` instead.
754+
692755
:param version: version string
693756
:return: the finalized version string
694757
:rtype: str
@@ -700,10 +763,14 @@ def finalize_version(version):
700763
return VersionInfo.format_version(verinfo.major, verinfo.minor, verinfo.patch)
701764

702765

766+
@deprecated()
703767
def replace(version, **parts):
704768
"""
705769
Replace one or more parts of a version and return the new string.
706770
771+
.. deprecated:: 2.9.2
772+
Use :func:`semver.VersionInfo.replace` instead.
773+
707774
:param str version: the version string to replace
708775
:param dict parts: the parts to be updated. Valid keys are:
709776
``major``, ``minor``, ``patch``, ``prerelease``, or ``build``

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[tool:pytest]
22
norecursedirs = .git build .env/ env/ .pyenv/ .tmp/ .eggs/
33
testpaths = . docs
4+
filterwarnings =
5+
ignore:Function 'semver.*:DeprecationWarning
46
addopts =
57
--no-cov-on-fail
68
--cov=semver

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy