Skip to content
Merged
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
3 changes: 3 additions & 0 deletions changelog.d/pr478.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``Version.parse()`` and ``Version.is_valid()`` now reject non-ASCII digits and
trailing newlines, in accordance with the SemVer grammar. Optional minor and
patch parsing applies the same character restrictions.
6 changes: 3 additions & 3 deletions src/semver/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@ class Version:
[0-9a-zA-Z-]+
(?:\.[0-9a-zA-Z-]+)*
))?
$
\Z
"""
#: Regex for a semver version
_REGEX: ClassVar[Pattern[str]] = re.compile(
_REGEX_TEMPLATE.format(opt_patch="", opt_minor=""),
re.VERBOSE,
re.VERBOSE | re.ASCII,
)
#: Regex for a semver version that might be shorter
_REGEX_OPTIONAL_MINOR_AND_PATCH: ClassVar[Pattern[str]] = re.compile(
_REGEX_TEMPLATE.format(opt_patch="?", opt_minor="?"),
re.VERBOSE,
re.VERBOSE | re.ASCII,
)

def __init__(
Expand Down
33 changes: 33 additions & 0 deletions tests/test_strict_parsing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest

from semver import Version


@pytest.mark.parametrize(
"version",
[
"1.2.3\n",
"1.2.3-rc.1\n",
"1.2.3+build.1\n",
"1\u0662.2.3",
"1.2\u0663.3",
"1.2.3\u0664",
"1.2.3-1\u0662",
"1.2.3-\u0661alpha",
],
)
@pytest.mark.parametrize("optional_minor_and_patch", [False, True])
def test_parse_rejects_non_semver_characters(version, optional_minor_and_patch):
with pytest.raises(ValueError):
Version.parse(version, optional_minor_and_patch=optional_minor_and_patch)


@pytest.mark.parametrize("version", ["1\n", "1.2\n", "1\u0662", "1.2\u0663"])
def test_optional_parse_rejects_non_semver_characters(version):
with pytest.raises(ValueError):
Version.parse(version, optional_minor_and_patch=True)


@pytest.mark.parametrize("version", ["1.2.3\n", "1\u0662.2.3", "1.2.3-\u0661alpha"])
def test_is_valid_rejects_non_semver_characters(version):
assert not Version.is_valid(version)
Loading