Skip to content

(not semver) Allow parsing of partial version string #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
$
""", re.VERBOSE)

_REGEX_PARTIAL = re.compile(
r"""
^
(?P<major>(?:0|[1-9][0-9]*))
(?:
\.(?P<minor>(?:0|[1-9][0-9]*))
)?
$
""",
re.VERBOSE
)

_LAST_NUMBER = re.compile(r'(?:[^\d]*(\d+)[^\d]*)+')

if not hasattr(__builtins__, 'cmp'):
Expand Down Expand Up @@ -73,6 +85,57 @@ def parse(version):
return version_parts


def try_parse(version):
"""Try parsing a version string to major, minor, patch, pre-release and
build parts. The version string may be incomplete in which case missing
parts are set to ``0`` or ``None``.

:param version: Version string.
:type version: str

:return: Dictionary with the keys 'build', 'major', 'minor', 'patch',
and 'prerelease'. The prerelease or build keys can be ``None``
if not provided.
:rtype: dict

>>> import semver
>>> ver = semver.try_parse('3')
>>> ver['major']
3
>>> ver['minor']
0
>>> ver['patch']
0
>>> ver['prerelease'] # None
>>> ver['build'] # None
>>> ver = semver.try_parse('3.1')
>>> ver['major']
3
>>> ver['minor']
1
>>> ver['patch']
0
>>> ver['prerelease'] # None
>>> ver['build'] # None
"""
try:
return parse(version)
except ValueError:
match = _REGEX_PARTIAL.match(version)
if match is None:
raise ValueError('could not parse version string %s' % version)

version_parts = match.groupdict()

version_parts['major'] = int(version_parts['major'])
version_parts['minor'] = int(version_parts['minor'] or 0)
version_parts['patch'] = 0
version_parts['prerelease'] = None
version_parts['build'] = None

return version_parts


class VersionInfo(object):
"""
:param int major: version when you make incompatible API changes.
Expand Down
51 changes: 51 additions & 0 deletions test_semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from semver import compare
from semver import match
from semver import parse
from semver import try_parse
from semver import format_version
from semver import bump_major
from semver import bump_minor
Expand Down Expand Up @@ -79,6 +80,56 @@ def test_should_parse_zero_prerelease(version, expected):
assert result == expected


@pytest.mark.parametrize("version, expected", [
# no. 1
(
"1",
{
'major': 1,
'minor': 0,
'patch': 0,
'prerelease': None,
'build': None,
}
),
# no. 2
(
"1.2",
{
'major': 1,
'minor': 2,
'patch': 0,
'prerelease': None,
'build': None,
}
),
# no. 3
(
"1.2.3",
{
'major': 1,
'minor': 2,
'patch': 3,
'prerelease': None,
'build': None,
}
),
])
def test_try_parse_should_allow_partial_version(version, expected):
result = try_parse(version)
assert result == expected


@pytest.mark.parametrize("version", [
'foo',
'1.x',
'1.0.x',
])
def test_try_parse_should_raise_value_error_for_invalid_value(version):
with pytest.raises(ValueError):
try_parse(version)


@pytest.mark.parametrize("left,right", [
("1.0.0", "2.0.0"),
('1.0.0-alpha', '1.0.0-alpha.1'),
Expand Down
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