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

Validate negative version parts before lossy int() conversion - #533

Closed
SonicHedghog wants to merge 1 commit into
python-semver:masterfrom
SonicHedghog:fix-531-negative-fractional-parts
Closed

SonicHedghog wants to merge 1 commit into
python-semver:masterfrom
SonicHedghog:fix-531-negative-fractional-parts

Conversation

@SonicHedghog

Copy link
Copy Markdown
Contributor

Fixes #531

Summary

int() truncates toward zero, so a negative fractional input such as Version(-0.5) passed the post-conversion negativity check as 0 and produced the valid but unintended release 0.0.0:

>>> from semver import Version
>>> Version(-0.5)   # before this change
Version(major=0, minor=0, patch=0, prerelease=None, build=None)

This PR validates numeric parts for negativity before the lossy conversion, so negative fractional inputs now raise the documented ValueError:

>>> Version(-0.5)   # after this change
ValueError: 'major' is negative. A version can only be positive.

The conversion behavior for all non-negative inputs is unchanged: positive fractional values still truncate exactly as before, string parts still convert as before, and the post-conversion checks (including the maximum-length checks) are untouched. Inputs that cannot be compared with zero fall back to the existing post-conversion validation.

Tests

  • Regression tests covering negative fractional input in each of major, minor, and patch, including values below -1 that were already caught after conversion
  • A test pinning the unchanged truncation behavior for positive fractional input
  • Full test suite passes: 586 passed, 1 skipped

int() truncates toward zero, so negative fractional inputs such as
-0.5 passed the post-conversion validation as 0 and produced a valid
but unintended 0.0.0 release version. Validate numeric parts for
negativity before converting them, keeping the existing conversion
behavior for all non-negative inputs.

Fixes python-semver#531
@tomschr

tomschr commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

@SonicHedghog I'm sorry, I was a bit slow to respond today. I realized too late that there is already PR#532 that covers the same that you already implemented. I already reviewed the former PR.

Perhaps we can work on PR #532? as a base for this issue and close this one? If you have further ideas or suggestions, please add them there.

I'm really sorry that I didn't tell you earlier, but it was a busy day. Nevertheless thanks for your contribution! 👍

@SonicHedghog

Copy link
Copy Markdown
Contributor Author

No need to apologize — thanks for pointing it out! I agree it makes no sense to keep two parallel fixes for the same issue. I'll close this one and add my notes to #532 instead.

I reviewed the diff there: the post-conversion check against the original value covers the truncation cases correctly, and the regression tests are thorough (nice catch covering replace() and the signed-zero cases, which my version here did not). One small difference worth mentioning over there: this PR validated negativity before the lossy conversion, which also covers negative numeric types outside the Real/Decimal hierarchy — I'll leave the details as a suggestion on #532 in case they're useful.

@tomschr

tomschr commented Dec 3, 2026

Copy link
Copy Markdown
Member

Thank you very much for your help and understanding! Much appreciated!

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.

Negative fractional version parts are silently truncated before validation

2 participants