Skip to content

Commit

Permalink
Simplify subclassing Version
Browse files Browse the repository at this point in the history
Fixes: #112
  • Loading branch information
rbarrois committed Feb 28, 2023
1 parent 9acf55a commit 2cbbee3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ ChangeLog
2.10.1 (unreleased)
-------------------

*Minor:*

* `112 <https://github.com/rbarrois/python-semanticversion/issues/112>`_:
Functions returning a new ``Version`` instance reuse the current class,
helping with subclassing.

*Bugfix:*

* `141 <https://github.com/rbarrois/python-semanticversion/issues/141>`_:
Expand Down
24 changes: 12 additions & 12 deletions semantic_version/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ def _coerce(cls, value, allow_none=False):

def next_major(self):
if self.prerelease and self.minor == self.patch == 0:
return Version(
return self.__class__(
major=self.major,
minor=0,
patch=0,
partial=self.partial,
)
else:
return Version(
return self.__class__(
major=self.major + 1,
minor=0,
patch=0,
Expand All @@ -148,14 +148,14 @@ def next_major(self):

def next_minor(self):
if self.prerelease and self.patch == 0:
return Version(
return self.__class__(
major=self.major,
minor=self.minor,
patch=0,
partial=self.partial,
)
else:
return Version(
return self.__class__(
major=self.major,
minor=self.minor + 1,
patch=0,
Expand All @@ -164,14 +164,14 @@ def next_minor(self):

def next_patch(self):
if self.prerelease:
return Version(
return self.__class__(
major=self.major,
minor=self.minor,
patch=self.patch,
partial=self.partial,
)
else:
return Version(
return self.__class__(
major=self.major,
minor=self.minor,
patch=self.patch + 1,
Expand All @@ -181,7 +181,7 @@ def next_patch(self):
def truncate(self, level='patch'):
"""Return a new Version object, truncated up to the selected level."""
if level == 'build':
return Version(
return self.__class__(
major=self.major,
minor=self.minor,
patch=self.patch,
Expand All @@ -190,29 +190,29 @@ def truncate(self, level='patch'):
partial=self.partial,
)
elif level == 'prerelease':
return Version(
return self.__class__(
major=self.major,
minor=self.minor,
patch=self.patch,
prerelease=self.prerelease,
partial=self.partial,
)
elif level == 'patch':
return Version(
return self.__class__(
major=self.major,
minor=self.minor,
patch=self.patch,
partial=self.partial,
)
elif level == 'minor':
return Version(
return self.__class__(
major=self.major,
minor=self.minor,
patch=None if self.partial else 0,
partial=self.partial,
)
elif level == 'major':
return Version(
return self.__class__(
major=self.major,
minor=None if self.partial else 0,
patch=None if self.partial else 0,
Expand Down Expand Up @@ -266,7 +266,7 @@ def coerce(cls, version_string, partial=False):
)

if match.end() == len(version_string):
return Version(version, partial=partial)
return cls(version, partial=partial)

rest = version_string[match.end():]

Expand Down
9 changes: 9 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,15 @@ def test_truncate(self):
self.assertEqual(v.truncate("minor"), base.Version("3.2.0"))
self.assertEqual(v.truncate("major"), base.Version("3.0.0"))

def test_subclass(self):
"""Custom subclasses of Version returns instances of themselves."""
class MyVersion(base.Version):
pass

v = MyVersion("3.2.1-pre")
subv = v.truncate()
self.assertEqual(type(subv), MyVersion)


class SpecItemTestCase(unittest.TestCase):
if sys.version_info[0] <= 2:
Expand Down

0 comments on commit 2cbbee3

Please sign in to comment.