You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Negative fractional version parts are silently truncated before validation #531
What OS are you using? (Add more in the Environment section)
Linux
Situation
Numeric version parts are converted with int() before the negative-value validation runs. As a result, a negative fractional input can be truncated to zero and accepted, while positive fractional inputs are silently truncated too. The usage documentation says major, minor, and patch must be positive integers or strings, and that invalid/negative values raise ValueError.
Version(-0.5) should not silently become 0.0.0: the negative input should raise ValueError. More generally, non-integral numeric parts should either be rejected consistently with the documented “integers or strings” domain, or the documentation should explicitly bless truncation.
This matters because the constructor currently rewrites version coordinates without any signal; a small input mistake such as -0.5 becomes a valid but different release version. The OverflowError for infinity is also inconsistent with the documented ValueError surface.
A possible resolution would be to validate numeric parts before lossy conversion (at minimum, check negative inputs before calling int()), and potentially reject non-integral floats rather than truncating them. I would be happy to implement the agreed behavior with regression tests.
Environment
Reproduced with the semver 3.2.0 wheel from PyPI on CPython 3.10/Linux. The same conversion-then-validation order is present on current master.
Unless you'd prefer a different direction, I'll prepare a PR that validates numeric parts before the lossy int() conversion, so that negative numeric inputs raise ValueError before any truncation happens (-0.5 no longer silently becomes 0.0.0), with regression tests covering the boundary cases. I'll keep the change minimal and otherwise preserve the current conversion behavior for valid inputs.
Of course, that's fine — consolidating on #532 sounds good to me. I've closed #533 and left a couple of small implementation notes on the other PR. Happy to help with anything remaining there.
Activity
tomschr commentedon Dec 3, 2026
Thank you, that sounds like a valid report. Many thanks! 👍
SonicHedghog commentedon Dec 3, 2026
Thanks for the quick look!
Unless you'd prefer a different direction, I'll prepare a PR that validates numeric parts before the lossy
int()conversion, so that negative numeric inputs raiseValueErrorbefore any truncation happens (-0.5no longer silently becomes0.0.0), with regression tests covering the boundary cases. I'll keep the change minimal and otherwise preserve the current conversion behavior for valid inputs.tomschr commentedon Dec 3, 2026
Yes, good plan. Please go ahead. 👍
SonicHedghog commentedon Dec 3, 2026
Thanks! The PR is ready for review: #533
tomschr commentedon Dec 3, 2026
As I already wrote, that's a duplicate for issue #532. I'm really sorry that I discovered this too late.
If you are okay with that, I would make this a duplicate of #531 and close this. We can continue on the other issue/PR.
SonicHedghog commentedon Dec 3, 2026
Of course, that's fine — consolidating on #532 sounds good to me. I've closed #533 and left a couple of small implementation notes on the other PR. Happy to help with anything remaining there.
Reject negative fractional version parts that truncate to zero (#532)