@@ -51,12 +51,12 @@ A version can be created in different ways:
51
51
integers::
52
52
53
53
>>> semver.VersionInfo(1, 2, 3, 4, 5)
54
- VersionInfo(major=1, minor=2, patch=3, prerelease=4 , build=5 )
54
+ VersionInfo(major=1, minor=2, patch=3, prerelease='4' , build='5' )
55
55
56
56
If you pass an invalid version string you will get a ``ValueError ``::
57
57
58
58
>>> semver.parse("1.2")
59
- Traceback (most recent call last)
59
+ Traceback (most recent call last):
60
60
...
61
61
ValueError: 1.2 is not valid SemVer string
62
62
@@ -80,8 +80,8 @@ Parsing a Version String
80
80
81
81
* With :func: `semver.parse `::
82
82
83
- >>> semver.parse("3.4.5-pre.2+build.4")
84
- {'major': 3, 'minor': 4, 'patch': 5, 'prerelease': 'pre.2', 'build': 'build.4'}
83
+ >>> semver.parse("3.4.5-pre.2+build.4") == {'major': 3, 'minor': 4, 'patch': 5, 'prerelease': 'pre.2', 'build': 'build.4'}
84
+ True
85
85
86
86
87
87
Checking for a Valid Semver Version
@@ -92,9 +92,9 @@ classmethod :func:`semver.VersionInfo.isvalid`:
92
92
93
93
.. code-block :: python
94
94
95
- >> > VersionInfo.isvalid(" 1.0.0" )
95
+ >> > semver. VersionInfo.isvalid(" 1.0.0" )
96
96
True
97
- >> > VersionInfo.isvalid(" invalid" )
97
+ >> > semver. VersionInfo.isvalid(" invalid" )
98
98
False
99
99
100
100
@@ -106,7 +106,7 @@ parts of a version:
106
106
107
107
.. code-block :: python
108
108
109
- >> > v = VersionInfo.parse(" 3.4.5-pre.2+build.4" )
109
+ >> > v = semver. VersionInfo.parse(" 3.4.5-pre.2+build.4" )
110
110
>> > v.major
111
111
3
112
112
>> > v.minor
@@ -122,20 +122,20 @@ However, the attributes are read-only. You cannot change an attribute.
122
122
If you do, you get an ``AttributeError ``::
123
123
124
124
>>> v.minor = 5
125
- Traceback (most recent call last)
125
+ Traceback (most recent call last):
126
126
...
127
127
AttributeError: attribute 'minor' is readonly
128
128
129
129
In case you need the different parts of a version stepwise, iterate over the :class: `semver.VersionInfo ` instance::
130
130
131
- >>> for item in VersionInfo.parse("3.4.5-pre.2+build.4"):
131
+ >>> for item in semver. VersionInfo.parse("3.4.5-pre.2+build.4"):
132
132
... print(item)
133
133
3
134
134
4
135
135
5
136
136
pre.2
137
137
build.4
138
- >>> list(VersionInfo.parse("3.4.5-pre.2+build.4"))
138
+ >>> list(semver. VersionInfo.parse("3.4.5-pre.2+build.4"))
139
139
[3, 4, 5, 'pre.2', 'build.4']
140
140
141
141
@@ -160,12 +160,12 @@ unmodified, use one of the functions :func:`semver.replace` or
160
160
If you pass invalid keys you get an exception::
161
161
162
162
>>> semver.replace("1.2.3", invalidkey=2)
163
- Traceback (most recent call last)
163
+ Traceback (most recent call last):
164
164
...
165
165
TypeError: replace() got 1 unexpected keyword argument(s): invalidkey
166
166
>>> version = semver.VersionInfo.parse("1.4.5-pre.1+build.6")
167
167
>>> version.replace(invalidkey=2)
168
- Traceback (most recent call last)
168
+ Traceback (most recent call last):
169
169
...
170
170
TypeError: replace() got 1 unexpected keyword argument(s): invalidkey
171
171
@@ -209,8 +209,8 @@ Depending which function you call, you get different types
209
209
* From a :class: `semver.VersionInfo ` into a dictionary::
210
210
211
211
>>> v = semver.VersionInfo(major=3, minor=4, patch=5)
212
- >>> semver.parse(str(v))
213
- {'major': 3, 'minor': 4, 'patch': 5, 'prerelease': None, 'build': None}
212
+ >>> semver.parse(str(v)) == {'major': 3, 'minor': 4, 'patch': 5, 'prerelease': None, 'build': None}
213
+ True
214
214
215
215
216
216
Increasing Parts of a Version
@@ -267,8 +267,8 @@ To compare two versions depends on your type:
267
267
Use the specific operator. Currently, the operators ``< ``,
268
268
``<= ``, ``> ``, ``>= ``, ``== ``, and ``!= `` are supported::
269
269
270
- >>> v1 = VersionInfo.parse("3.4.5")
271
- >>> v2 = VersionInfo.parse("3.5.1")
270
+ >>> v1 = semver. VersionInfo.parse("3.4.5")
271
+ >>> v2 = semver. VersionInfo.parse("3.5.1")
272
272
>>> v1 < v2
273
273
True
274
274
>>> v1 > v2
@@ -278,7 +278,7 @@ To compare two versions depends on your type:
278
278
279
279
Use the operator as with two :class: `semver.VersionInfo ` types::
280
280
281
- >>> v = VersionInfo.parse("3.4.5")
281
+ >>> v = semver. VersionInfo.parse("3.4.5")
282
282
>>> v > (1, 0)
283
283
True
284
284
>>> v < (3, 5)
@@ -350,48 +350,9 @@ However, "basic" version strings consisting of major, minor,
350
350
and patch part, can be easy to convert. The following function extract this
351
351
information and returns a tuple with two items:
352
352
353
- .. code-block :: python
353
+ .. literalinclude :: coerce.py
354
+ :language: python
354
355
355
- import re
356
-
357
- BASEVERSION = re.compile(
358
- r """ [vV ]?
359
- ( ?P<major> 0| [1-9 ]\d * )
360
- ( \.
361
- ( ?P<minor> 0| [1-9 ]\d * )
362
- ( \.
363
- ( ?P<patch> 0| [1-9 ]\d * )
364
- ) ?
365
- ) ?
366
- """ ,
367
- re.VERBOSE ,
368
- )
369
- def coerce (version ):
370
- """
371
- Convert an incomplete version string into a semver-compatible VersionInfo
372
- object
373
-
374
- * Tries to detect a "basic" version string (``major.minor.patch``).
375
- * If not enough components can be found, missing components are
376
- set to zero to obtain a valid semver version.
377
-
378
- :param str version: the version string to convert
379
- :return: a tuple with a :class:`VersionInfo` instance (or ``None``
380
- if it's not a version) and the rest of the string which doesn't
381
- belong to a basic version.
382
- :rtype: tuple(:class:`VersionInfo` | None, str)
383
- """
384
- match = BASEVERSION .search(version)
385
- if not match:
386
- return (None , version)
387
-
388
- ver = {
389
- key: 0 if value is None else value
390
- for key, value in match.groupdict().items()
391
- }
392
- ver = semver.VersionInfo(** ver)
393
- rest = match.string[match.end() :]
394
- return ver, rest
395
356
396
357
The function returns a *tuple *, containing a :class: `VersionInfo `
397
358
instance or None as the first element and the rest as the second element.
0 commit comments