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

Replace method not compatible with SubClasses #426

Closed

Description

@dannystaple

Situation

Using the demonstration subclass from https://python-semver.readthedocs.io/en/latest/advanced/create-subclasses-from-version.html#creating-subclasses-from-version, the bump methods will create a new instance of the same SubClass.

When using the replace method, an instance of the Version class is created and not the subclass.

To Reproduce

  1. Create a subclass as shown in the document above.
  2. version = SemVerWithVPrefix.parse("v1.1.0")
  3. dev_version = version.replace(prerelease="dev.0")
  4. Print str(dev_version)
  5. This results in 1.1.0-dev.0 - dev_version is of type Version not SemVerWithVPrefix.

Expected Behavior

I expect v1.1.0-dev.0 and dev_version to be an instance of SemVerWithVPrefix (or my class).

Environment

  • OS: [e.g. Linux, MacOS, Windows, ...] - Python 3.11 container - will be Debian.
  • Python version [e.g. 3.6, 3.7, ...] - Python 3.11.5
  • Version of semver library [e.g. 3.0.0] - semver==3.0.1

Additional context

I suggest changing this the same way that the bump methods work:
Add this near the top: cls = type(self)
And near the bottom, replace return Version(**version) with return cls(**version).

Activity

  1. added a commit that references this issue on Sep 28, 2023
    3163453
  2. tomschr commented on Sep 28, 2023

    @tomschr
    Member

    Thanks for your bug report, much appreciated! 👍

    I've created a PR which should fix this (currently having CI issues).

  3. dannystaple commented on Sep 28, 2023

    @dannystaple
    ContributorAuthor

    I've reviewed that code, it makes sense - and since the type is only in one place, why you likely don't need the extra cls variable. Thanks!

  4. added 3 commits that reference this issue on Sep 29, 2023
    4987042
    232d8e8
    4b03f86
  5. added a commit that references this issue on Sep 29, 2023
    c5c779a
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

    No labels
    No labels

    Type

    No type

    Fields

    SIze

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions