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
1 change: 1 addition & 0 deletions changelog.d/pr495.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The type annotation of ``Version.is_valid()`` now includes its existing support for UTF-8 encoded byte strings.
4 changes: 2 additions & 2 deletions src/semver/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ def replace(self, **parts: Union[int, Optional[str]]) -> "Version":
raise TypeError(error)

@classmethod
def is_valid(cls, version: str) -> bool:
def is_valid(cls, version: String) -> bool:
"""
Check if the string is a valid semver version.
Expand All @@ -798,7 +798,7 @@ def is_valid(cls, version: str) -> bool:
.. versionchanged:: 3.0.0
Renamed from :meth:`~semver.version.Version.isvalid`
:param version: the version string to check
:param version: the version string to check, as str or UTF-8 encoded bytes
:return: True if the version string is a valid semver version, False
otherwise.
"""
Expand Down
15 changes: 15 additions & 0 deletions tests/test_semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from semver import Version

UTF8_VERSION = "1.0.0"


@pytest.mark.parametrize(
"string,expected", [("rc", "rc"), ("rc.1", "rc.2"), ("2x", "3x")]
Expand Down Expand Up @@ -77,6 +79,19 @@ def test_should_versioninfo_isvalid():
assert Version.is_valid("foo") is False


@pytest.mark.parametrize(
"version,expected",
[
pytest.param(b"1.0.0", True, id="valid-bytes"),
pytest.param(b"foo", False, id="invalid-version-bytes"),
pytest.param(UTF8_VERSION.encode("UTF-8"), True, id="utf8-encoded-version"),
pytest.param(b"\xff", False, id="invalid-utf8"),
],
)
def test_is_valid_bytes(version, expected):
assert Version.is_valid(version) is expected


def test_versioninfo_compare_should_raise_when_passed_invalid_value():
with pytest.raises(TypeError):
Version(1, 2, 3).compare(4)
Expand Down
Loading