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

Negative fractional version parts are silently truncated before validation #531

Not planned

Description

@SonicHedghog

Which version of Python is the problem with?

3.10

What semver version are you using?

3.2.0

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.

How to reproduce

from semver import Version

print(Version(-0.5).to_tuple())
print(Version(1.9).to_tuple())

try:
    Version(float("inf"))
except Exception as exc:
    print(type(exc).__name__, exc)

Output on semver 3.2.0:

(0, 0, 0, None, None)
(1, 0, 0, None, None)
OverflowError cannot convert float infinity to integer

Expected behavior

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.

Activity

  1. tomschr commented on Dec 3, 2026

    @tomschr
    Member

    Thank you, that sounds like a valid report. Many thanks! 👍

  2. SonicHedghog commented on Dec 3, 2026

    @SonicHedghog
    ContributorAuthor

    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 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.

  3. tomschr commented on Dec 3, 2026

    @tomschr
    Member

    Yes, good plan. Please go ahead. 👍

  4. SonicHedghog commented on Dec 3, 2026

    @SonicHedghog
    ContributorAuthor

    Thanks! The PR is ready for review: #533

  5. tomschr commented on Dec 3, 2026

    @tomschr
    Member

    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.

  6. SonicHedghog commented on Dec 3, 2026

    @SonicHedghog
    ContributorAuthor

    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.

  7. added
    Duplicate
    on Dec 3, 2026
  8. added a commit that references this issue on Dec 4, 2026
    0358e49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Duplicate

    Type

    No type

    Fields

    SIze

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions