diff --git a/changelog.d/pr495.bugfix.rst b/changelog.d/pr495.bugfix.rst new file mode 100644 index 0000000..ac01fa8 --- /dev/null +++ b/changelog.d/pr495.bugfix.rst @@ -0,0 +1 @@ +The type annotation of ``Version.is_valid()`` now includes its existing support for UTF-8 encoded byte strings. diff --git a/src/semver/version.py b/src/semver/version.py index 7ead382..6160bf7 100644 --- a/src/semver/version.py +++ b/src/semver/version.py @@ -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. @@ -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. """ diff --git a/tests/test_semver.py b/tests/test_semver.py index 9978b94..fabf827 100644 --- a/tests/test_semver.py +++ b/tests/test_semver.py @@ -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")] @@ -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)