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
Replace method not compatible with SubClasses #426
When using the replace method, an instance of the Version class is created and not the subclass.
To Reproduce
Create a subclass as shown in the document above.
version = SemVerWithVPrefix.parse("v1.1.0")
dev_version = version.replace(prerelease="dev.0")
Print str(dev_version)
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
Fix python-semver#426: call subclass when deriving from Version
tomschr commentedon Sep 28, 2023
Thanks for your bug report, much appreciated! 👍
I've created a PR which should fix this (currently having CI issues).
dannystaple commentedon Sep 28, 2023
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!
Fix python-semver#426: call subclass when deriving from Version
Fix python-semver#426: call subclass when deriving from Version
Fix python-semver#426: call subclass when deriving from Version
Merge pull request #427 from tomschr/fix-426-match-derivedclass