Skip to content

Preserve version order when switching prerelease tokens - #498

Open
Apeksha9389 wants to merge 1 commit into
python-semver:masterfrom
Apeksha9389:fix/ordered-prerelease-token-switch
Open

Apeksha9389 wants to merge 1 commit into
python-semver:masterfrom
Apeksha9389:fix/ordered-prerelease-token-switch

Conversation

@Apeksha9389

Copy link
Copy Markdown

Version.parse("1.2.3-rc.1").next_version("prerelease", "alpha") currently returns 1.2.3-alpha.1, which is older than the original version despite the method's ordering guarantee.

When resetting the token would move backward, advance the patch version and start the requested prerelease there. This example becomes 1.2.4-alpha.1. Forward changes such as alpha to rc retain the patch version, and same-token increments keep their existing behavior. Build metadata is cleared and the original object is unchanged.

Fixes #497. This implements the patch-advance option from that issue so valid token changes continue returning a newer version instead of introducing a new exception.

Validation: five regression cases failed before the fix. The full suite now passes 494 tests with one skip and reports 100% coverage. Cases cover legacy counters, numeric tokens, dotted tokens, forward changes and build metadata. One older expectation was updated because it also allowed a backward token change. Documentation and a changelog fragment describe the behavior.

Full Ruff reports 159 existing findings versus 160 on unchanged upstream, with no new findings by file, rule and message. Mypy reports the same missing optional fast_semver_rs_backend module on both trees. git diff --check passes. Tested on Python 3.12.

AI-assisted implementation and tests.

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

Hi @Apeksha9389, 👋

thank you very much for submitting this pull request and tackling issue #497! Much appreciated. 😍

Ensuring that Version.next_version("prerelease", prerelease_token=...) preserves monotonic ordering when switching between prerelease tokens is a great improvement and prevents subtle version downgrade bugs.

To help users understand how token transitions work, could you please include a short section and example in docs/usage/increase-parts-of-a-version_prereleases.rst?

Here is the suggested addition:

Documentation change
When switching between prerelease tokens, :meth:`~semver.version.Version.next_version`
preserves natural ordering. Advancing to a higher token retains the patch version,
while switching to an earlier token increments the patch version to prevent a downgrade:

.. code-block:: python

    >>> v = Version.parse("1.2.3-alpha.1")
    >>> str(v.next_version("prerelease", prerelease_token="beta"))
    '1.2.3-beta.1'
    >>> str(Version.parse("1.2.3-rc.1").next_version("prerelease", prerelease_token="alpha"))
    '1.2.4-alpha.1'

Once this is added and the tests pass, this PR should be ready to merge. Thanks again for your contribution! ❤️

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.

next_version("prerelease") returns a lower version when switching to an earlier prerelease token

2 participants