Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix #426: call subclass when deriving from Version #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
cache: 'pip'
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip setuptools setuptools-scm
python3 -m pip install --upgrade pip setuptools>60 setuptools-scm>=60
pip install tox tox-gh-actions
- name: Check
run: |
Expand Down
2 changes: 2 additions & 0 deletions changelog.d/426.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :meth:`~semver.version.Version.replace` method to use the derived class
of an instance instead of :class:`~semver.version.Version` class.
6 changes: 3 additions & 3 deletions src/semver/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,8 @@ def parse(

def replace(self, **parts: Union[int, Optional[str]]) -> "Version":
"""
Replace one or more parts of a version and return a new
:class:`Version` object, but leave self untouched
Replace one or more parts of a version and return a new :class:`Version`
object, but leave self untouched.

.. versionadded:: 2.9.0
Added :func:`Version.replace`
Expand All @@ -670,7 +670,7 @@ def replace(self, **parts: Union[int, Optional[str]]) -> "Version":
version = self.to_dict()
version.update(parts)
try:
return Version(**version) # type: ignore
return type(self)(**version) # type: ignore
except TypeError:
unknownkeys = set(parts) - set(self.to_dict())
error = "replace() got %d unexpected keyword argument(s): %s" % (
Expand Down
34 changes: 34 additions & 0 deletions tests/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,37 @@ def __str__(self):

v = SemVerWithVPrefix.parse("v1.2.3")
assert str(v) == "v1.2.3"


def test_replace_from_subclass():
# Issue#426
# Taken from the example "Creating Subclasses from Version"
class SemVerWithVPrefix(Version):
"""
A subclass of Version which allows a "v" prefix
"""

@classmethod
def parse(cls, version: str) -> "SemVerWithVPrefix":
"""
Parse version string to a Version instance.

:param version: version string with "v" or "V" prefix
:raises ValueError: when version does not start with "v" or "V"
:return: a new instance
"""
if not version[0] in ("v", "V"):
raise ValueError(
f"{version!r}: not a valid semantic version tag. "
"Must start with 'v' or 'V'"
)
return super().parse(version[1:], optional_minor_and_patch=True)

def __str__(self) -> str:
# Reconstruct the tag
return "v" + super().__str__()

version = SemVerWithVPrefix.parse("v1.1.0")
dev_version = version.replace(prerelease="dev.0")

assert str(dev_version) == "v1.1.0-dev.0"