Skip to content

Commit 2fb427d

Browse files
committed
Properly coerce versions with leading zeroes.
A leading zero is forbidden in the SemVer spec, but could be valid under other schemes; when coercing, it can easily be removed. Closes rbarrois#89, thanks to Andrew Ni for the report.
1 parent 25db246 commit 2fb427d

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

ChangeLog

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ ChangeLog
44
2.8.4 (unreleased)
55
------------------
66

7-
- Nothing changed yet.
7+
*Bugfix:*
8+
9+
* `#89 <https://github.com/rbarrois/python-semanticversion/issues/89>`_:
10+
Properly coerce versions with leading zeroes in components (e.g.
11+
``1.01.007``)
812

913

1014
2.8.3 (2019-11-21)

semantic_version/base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,14 @@ def coerce(cls, version_string, partial=False):
244244
while version.count('.') < 2:
245245
version += '.0'
246246

247+
# Strip leading zeros in components
248+
# Version is of the form nn, nn.pp or nn.pp.qq
249+
version = '.'.join(
250+
# If the part was '0', we end up with an empty string.
251+
part.lstrip('0') or '0'
252+
for part in version.split('.')
253+
)
254+
247255
if match.end() == len(version_string):
248256
return Version(version, partial=partial)
249257

tests/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ def subTest(self, **kwargs):
569569
examples = {
570570
# Dict of target: [list of equivalents]
571571
'0.0.0': ('0', '0.0', '0.0.0', '0.0.0+', '0-'),
572-
'0.1.0': ('0.1', '0.1+', '0.1-', '0.1.0'),
572+
'0.1.0': ('0.1', '0.1+', '0.1-', '0.1.0', '0.01.0', '000.0001.0000000000'),
573573
'0.1.0+2': ('0.1.0+2', '0.1.0.2'),
574574
'0.1.0+2.3.4': ('0.1.0+2.3.4', '0.1.0+2+3+4', '0.1.0.2+3+4'),
575575
'0.1.0+2-3.4': ('0.1.0+2-3.4', '0.1.0+2-3+4', '0.1.0.2-3+4', '0.1.0.2_3+4'),

0 commit comments

Comments
 (0)