← 输入框|github.com|刷新
Skip to content

Decode bytes prerelease and build parts in Version - #536

Open
kevincommits wants to merge 1 commit into
python-semver:masterfrom
kevincommits:fix/decode-bytes-prerelease-build
Open

kevincommits wants to merge 1 commit into
python-semver:masterfrom
kevincommits:fix/decode-bytes-prerelease-build

Conversation

@kevincommits

Copy link
Copy Markdown

The Version constructor annotates prerelease and build as Optional[Union[String, int]], where String is Union[str, bytes], and major, minor and patch already accept bytes. But bytes passed as prerelease or build go through str(), which stores their repr:

>>> from semver import Version
>>> str(Version(1, 2, 3, b"rc.1", b"build.4"))
"1.2.3-b'rc.1'+b'build.4'"

Expected '1.2.3-rc.1+build.4', the same result as Version.parse(b"1.2.3-rc.1+build.4"). The current string is not valid SemVer, cannot be parsed back, and the version compares unequal to the parsed one.

The constructor now decodes bytes prerelease and build values as UTF-8, the same way Version.parse() decodes bytes input. Other types are converted with str() as before.

The new test in tests/test_semver.py builds a version from bytes parts and checks both properties and str(). A towncrier fragment is included.

An alternative would be to drop bytes from the annotation and reject it; I went with decoding because parse() and the numeric parts already accept bytes. #317 reworks __init__ and would also cover this if it lands; this is a small fix that does not depend on it.

What I ran: pytest (600 passed, 1 skipped, 100% coverage) on Python 3.13; ruff check, mypy src and docformatter --check src report the same findings before and after this change; towncrier build --draft.

What I did not run: the other Python versions in the tox matrix.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant