Skip to content

Commit 474c096

Browse files
authored
Merge pull request python-semver#42 from tomschr/docstrings
Improve Usability with Docstrings
2 parents 8ac1146 + 9f60ad5 commit 474c096

File tree

3 files changed

+159
-4
lines changed

3 files changed

+159
-4
lines changed

semver.py

Lines changed: 145 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
11
"""
22
Python helper for Semantic Versioning (http://semver.org/)
3+
4+
Examples:
5+
>>> import semver
6+
>>> semver.compare("1.0.0", "2.0.0")
7+
-1
8+
>>> semver.compare("2.0.0", "1.0.0")
9+
1
10+
>>> semver.compare("2.0.0", "2.0.0")
11+
0
12+
>>> semver.match("2.0.0", ">=1.0.0")
13+
True
14+
>>> semver.match("1.0.0", ">1.0.0")
15+
False
16+
>>> semver.format_version(3, 4, 5, 'pre.2', 'build.4')
17+
'3.4.5-pre.2+build.4'
18+
>>> version_parts = semver.parse("3.4.5-pre.2+build.4")
19+
>>> version_parts == {
20+
... 'major': 3, 'minor': 4, 'patch': 5,
21+
... 'prerelease': 'pre.2', 'build': 'build.4'}
22+
True
23+
>>> version_info = semver.parse_version_info("3.4.5-pre.2+build.4")
24+
>>> version_info
25+
VersionInfo(major=3, minor=4, patch=5, prerelease='pre.2', build='build.4')
26+
>>> version_info.major
27+
3
28+
>>> version_info > (1, 0)
29+
True
30+
>>> version_info < (3, 5)
31+
True
32+
>>> semver.bump_major("3.4.5")
33+
'4.0.0'
34+
>>> semver.bump_minor("3.4.5")
35+
'3.5.0'
36+
>>> semver.bump_patch("3.4.5")
37+
'3.4.6'
38+
>>> semver.max_ver("1.0.0", "2.0.0")
39+
'2.0.0'
40+
>>> semver.min_ver("1.0.0", "2.0.0")
41+
'1.0.0'
342
"""
443

544
import collections
645
import re
46+
import sys
747

848

949
__version__ = '2.7.2'
@@ -37,8 +77,13 @@ def cmp(a, b):
3777

3878

3979
def parse(version):
40-
"""
41-
Parse version to major, minor, patch, pre-release, build parts.
80+
"""Parse version to major, minor, patch, pre-release, build parts.
81+
82+
:param version: version string
83+
:return: dictionary with the keys 'build', 'major', 'minor', 'patch',
84+
and 'prerelease'. The prerelease or build keys can be None
85+
if not provided
86+
:rtype: dict
4287
"""
4388
match = _REGEX.match(version)
4489
if match is None:
@@ -56,10 +101,31 @@ def parse(version):
56101
VersionInfo = collections.namedtuple(
57102
'VersionInfo', 'major minor patch prerelease build')
58103

104+
# Only change it for Python > 3 as it is readonly
105+
# for version 2
106+
if sys.version_info > (3, 0):
107+
VersionInfo.__doc__ = """
108+
:param int major: version when you make incompatible API changes.
109+
:param int minor: version when you add functionality in
110+
a backwards-compatible manner.
111+
:param int patch: version when you make backwards-compatible bug fixes.
112+
:param str prerelease: an optional prerelease string
113+
:param str build: an optional build string
114+
115+
>>> import semver
116+
>>> ver = semver.parse('3.4.5-pre.2+build.4')
117+
>>> ver
118+
{'build': 'build.4', 'major': 3, 'minor': 4, 'patch': 5,
119+
'prerelease': 'pre.2'}
120+
"""
121+
59122

60123
def parse_version_info(version):
61-
"""
62-
Parse version string to a VersionInfo instance.
124+
"""Parse version string to a VersionInfo instance.
125+
126+
:param version: version string
127+
:return: a :class:`VersionInfo` instance
128+
:rtype: :class:`VersionInfo`
63129
"""
64130
parts = parse(version)
65131
version_info = VersionInfo(
@@ -70,6 +136,14 @@ def parse_version_info(version):
70136

71137

72138
def compare(ver1, ver2):
139+
"""Compare two versions
140+
141+
:param ver1: version string 1
142+
:param ver2: version string 2
143+
:return: The return value is negative if ver1 < ver2,
144+
zero if ver1 == ver2 and strictly positive if ver1 > ver2
145+
:rtype: int
146+
"""
73147
def nat_cmp(a, b):
74148
def convert(text):
75149
return (2, int(text)) if re.match('[0-9]+', text) else (1, text)
@@ -104,6 +178,19 @@ def compare_by_keys(d1, d2):
104178

105179

106180
def match(version, match_expr):
181+
"""Compare two versions through a comparison
182+
183+
:param str version: a version string
184+
:param str match_expr: operator and version; valid operators are
185+
< smaller than
186+
> greater than
187+
>= greator or equal than
188+
<= smaller or equal than
189+
== equal
190+
!= not equal
191+
:return: True if the expression matches the version, otherwise False
192+
:rtype: bool
193+
"""
107194
prefix = match_expr[:2]
108195
if prefix in ('>=', '<=', '==', '!='):
109196
match_version = match_expr[2:]
@@ -132,6 +219,13 @@ def match(version, match_expr):
132219

133220

134221
def max_ver(ver1, ver2):
222+
"""Returns the greater version of two versions
223+
224+
:param ver1: version string 1
225+
:param ver2: version string 2
226+
:return: the greater version of the two
227+
:rtype: :class:`VersionInfo`
228+
"""
135229
cmp_res = compare(ver1, ver2)
136230
if cmp_res == 0 or cmp_res == 1:
137231
return ver1
@@ -140,6 +234,13 @@ def max_ver(ver1, ver2):
140234

141235

142236
def min_ver(ver1, ver2):
237+
"""Returns the smaller version of two versions
238+
239+
:param ver1: version string 1
240+
:param ver2: version string 2
241+
:return: the smaller version of the two
242+
:rtype: :class:`VersionInfo`
243+
"""
143244
cmp_res = compare(ver1, ver2)
144245
if cmp_res == 0 or cmp_res == -1:
145246
return ver1
@@ -148,6 +249,16 @@ def min_ver(ver1, ver2):
148249

149250

150251
def format_version(major, minor, patch, prerelease=None, build=None):
252+
"""Format a version according to the Semantic Versioning specification
253+
254+
:param str major: the required major part of a version
255+
:param str minor: the required minor part of a version
256+
:param str patch: the required patch part of a version
257+
:param str prerelease: the optional prerelease part of a version
258+
:param str build: the optional build part of a version
259+
:return: the formatted string
260+
:rtype: str
261+
"""
151262
version = "%d.%d.%d" % (major, minor, patch)
152263
if prerelease is not None:
153264
version = version + "-%s" % prerelease
@@ -172,29 +283,59 @@ def _increment_string(string):
172283

173284

174285
def bump_major(version):
286+
"""Raise the major part of the version
287+
288+
:param: version string
289+
:return: the raised version string
290+
:rtype: str
291+
"""
175292
verinfo = parse(version)
176293
return format_version(verinfo['major'] + 1, 0, 0)
177294

178295

179296
def bump_minor(version):
297+
"""Raise the minor part of the version
298+
299+
:param: version string
300+
:return: the raised version string
301+
:rtype: str
302+
"""
180303
verinfo = parse(version)
181304
return format_version(verinfo['major'], verinfo['minor'] + 1, 0)
182305

183306

184307
def bump_patch(version):
308+
"""Raise the patch part of the version
309+
310+
:param: version string
311+
:return: the raised version string
312+
:rtype: str
313+
"""
185314
verinfo = parse(version)
186315
return format_version(verinfo['major'], verinfo['minor'],
187316
verinfo['patch'] + 1)
188317

189318

190319
def bump_prerelease(version):
320+
"""Raise the prerelease part of the version
321+
322+
:param: version string
323+
:return: the raised version string
324+
:rtype: str
325+
"""
191326
verinfo = parse(version)
192327
verinfo['prerelease'] = _increment_string(verinfo['prerelease'] or 'rc.0')
193328
return format_version(verinfo['major'], verinfo['minor'], verinfo['patch'],
194329
verinfo['prerelease'])
195330

196331

197332
def bump_build(version):
333+
"""Raise the build part of the version
334+
335+
:param: version string
336+
:return: the raised version string
337+
:rtype: str
338+
"""
198339
verinfo = parse(version)
199340
verinfo['build'] = _increment_string(verinfo['build'] or 'build.0')
200341
return format_version(verinfo['major'], verinfo['minor'], verinfo['patch'],

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def read_file(filename):
6969
with open(join(dirname(__file__), filename)) as f:
7070
return f.read()
7171

72+
7273
setup(
7374
name=package.__name__,
7475
version=package.__version__,

tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@
1313
from semver import max_ver
1414

1515

16+
SEMVERFUNCS = [
17+
compare, match, parse, format_version,
18+
bump_major, bump_minor, bump_patch, bump_prerelease, bump_build,
19+
max_ver, min_ver,
20+
]
21+
22+
23+
@pytest.mark.parametrize("func", SEMVERFUNCS,
24+
ids=[func.__name__ for func in SEMVERFUNCS])
25+
def test_fordocstrings(func):
26+
assert func.__doc__, "Need a docstring for function %r" % func.__name
27+
28+
1629
def test_should_parse_version():
1730
result = parse("1.2.3-alpha.1.2+build.11.e0f985a")
1831
assert result == {

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