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

Reject negative fractional version parts that truncate to zero - #532

Merged
tomschr merged 2 commits into
python-semver:masterfrom
echo666939:fix/reject-negative-fractional-parts
Dec 4, 2026
Merged

tomschr merged 2 commits into
python-semver:masterfrom
echo666939:fix/reject-negative-fractional-parts

Conversation

@echo666939

@echo666939 echo666939 commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Refs #531.

Version(-0.5) used to produce 0.0.0. The constructor now checks both the converted integer and the sign of inputs that support a numeric comparison, including custom number types. Major, minor and patch all raise ValueError for a negative part.

Positive fractions retain their existing integer conversion. Signed zero and unordered objects with a valid __int__ remain supported. Integer conversion runs before the sign comparison, so NaN (including signaling Decimal NaN) and infinity retain their existing conversion errors.

The regression tests use pytest and cover construction and replace(). Four new cases failed on the previous PR revision and passed with this update. On Windows, the project tests and documentation examples passed on CPython 3.11.8 and 3.12.14 (667 passed, 1 optional native-backend test skipped on each). Coverage was disabled. The local runner loaded the two symlinked example modules from their target directory and shortened oversized pytest parameter IDs for Windows.

AI assistance was used to develop and test this change.

@tomschr tomschr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @echo666939 for your contribution. 👍❤️

I have a few suggestions. Could you look at them? Would love to hear your thoughts.

Comment thread changelog.d/531.bugfix.rst Resolved
Comment thread tests/test_negative_fractional_parts.py Resolved
Comment thread src/semver/version.py Resolved

@SonicHedghog SonicHedghog left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix looks correct for the truncation cases, and the test matrix covers cases my closed parallel attempt missed (signed zero, replace(), non-finite inputs). two notes in case either is useful:

  1. pre-conversion validation as a possible simplification. in #533 i checked negativity on the original input before the lossy int() call, with a TypeError-guarded comparison:

    try:
        is_negative = value < 0
    except TypeError:
        is_negative = False

    that also rejects negative numeric types outside the Real/Decimal hierarchy (a custom Number subclass whose < 0 works and whose __int__ truncates to zero), which the isinstance gate here doesn't cover. marginal case either way, just surfacing the difference.

  2. the repo's tests are pytest-style functions (see tests/test_semver.py), so matching that might cut review friction — maintainers' call though.

@tomschr

tomschr commented Dec 3, 2026

Copy link
Copy Markdown
Member

Thank you @SonicHedghog for your comments.

  1. I agree, that would be a good addition and I think, we should add that.
  2. I already covered that in my review. 😉 I would prefer pytest over unittest.

@echo666939

Copy link
Copy Markdown
Contributor Author

@tomschr, could you take another look at ee5d725? The changelog now uses the Sphinx exception role for ValueError, and the regression tests use pytest.

I also addressed the numeric edge cases you and SonicHedghog raised. Integer conversion happens first, so a signaling Decimal NaN still raises ValueError and infinity still raises OverflowError. For values that convert successfully, the check now catches negative comparable objects even when their integer conversion loses the sign. The new tests also cover objects that support integer conversion but do not support ordering.

The tests and documentation examples passed on Windows with Python 3.11 and 3.12 (667 passed, one optional-backend test skipped on each). Thanks for the review.

@tomschr

tomschr commented Dec 4, 2026

Copy link
Copy Markdown
Member

Thank you @echo666939 for all the addition! 👍

From my side it looks good. Let's ask @SonicHedghog if he has some comments or suggestion.

Thank you both of all the help! ❤️

@tomschr
tomschr requested a review from SonicHedghog December 4, 2026 09:24

@SonicHedghog SonicHedghog left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. skipping the comparison for exact int/str/bytes is right since int() preserves their sign, and the TypeError guard covers unordered SupportsInt objects. good validation order too. conversion first means nan and infinity keep their existing errors. nice work @echo666939

@tomschr

tomschr commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Ok, thanks to all. I added both of you in the "Co-authored-by" line.

Happy to merge it. 🚀

@tomschr
tomschr merged commit 0358e49 into python-semver:master Dec 4, 2026
17 checks passed
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.

3 participants