Skip to content

Commit 37ce07b

Browse files
committed
Document how to create subclass from VersionInfo
Related to issue #276
1 parent db870f2 commit 37ce07b

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
sys.path.insert(0, "docs")
66

77
from coerce import coerce # noqa:E402
8+
from semverwithvprefix import SemVerWithVPrefix
89

910

1011
@pytest.fixture(autouse=True)
1112
def add_semver(doctest_namespace):
1213
doctest_namespace["semver"] = semver
1314
doctest_namespace["coerce"] = coerce
15+
doctest_namespace["SemVerWithVPrefix"] = SemVerWithVPrefix

docs/semverwithvprefix.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from semver import VersionInfo
2+
3+
4+
class SemVerWithVPrefix(VersionInfo):
5+
"""
6+
A subclass of VersionInfo which allows a "v" prefix
7+
"""
8+
9+
@classmethod
10+
def parse(cls, version):
11+
"""
12+
Parse version string to a VersionInfo instance.
13+
14+
:param version: version string with "v" or "V" prefix
15+
:type version: str
16+
:raises ValueError: when version does not start with "v" or "V"
17+
:return: a new instance
18+
:rtype: :class:`SemVerWithVPrefix`
19+
"""
20+
if not version[0] in ("v", "V"):
21+
raise ValueError(
22+
"{v!r}: not a valid semantic version tag. Must start with 'v' or 'V'".format(
23+
v=version
24+
)
25+
)
26+
self = super(SemVerWithVPrefix, cls).parse(version[1:])
27+
return self
28+
29+
def __str__(self):
30+
# Reconstruct the tag
31+
return "v" + super(SemVerWithVPrefix, self).__str__()

docs/usage.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,8 @@ The "old way" with :func:`semver.max_ver` or :func:`semver.min_ver` is still ava
530530
'1.0.0'
531531
532532
533+
.. _sec_dealing_with_invalid_versions:
534+
533535
Dealing with Invalid Versions
534536
-----------------------------
535537

@@ -708,3 +710,39 @@ the following methods:
708710
For further details, see the section
709711
`Overriding the default filter <https://docs.python.org/3/library/warnings.html#overriding-the-default-filter>`_
710712
of the Python documentation.
713+
714+
715+
.. _sec_creating_subclasses_from_versioninfo:
716+
717+
Creating Subclasses from VersionInfo
718+
------------------------------------
719+
720+
If you do not like creating functions to modify the behavior of semver
721+
(as shown in section :ref:`sec_dealing_with_invalid_versions`), you can
722+
also create a subclass of the :class:`VersionInfo` class.
723+
724+
For example, if you want to output a "v" prefix before a version,
725+
but the other behavior is the same, use the following code:
726+
727+
.. literalinclude:: semverwithvprefix.py
728+
:language: python
729+
:lines: 4-
730+
731+
732+
The derived class :class:`SemVerWithVPrefix` can be used like
733+
the original class:
734+
735+
.. code-block:: python
736+
737+
>>> v1 = SemVerWithVPrefix.parse("v1.2.3")
738+
>>> assert str(v1) == "v1.2.3"
739+
>>> print(v1)
740+
v1.2.3
741+
>>> v2 = SemVerWithVPrefix.parse("v2.3.4")
742+
>>> v2 > v1
743+
True
744+
>>> bad = SemVerWithVPrefix.parse("1.2.4")
745+
Traceback (most recent call last):
746+
...
747+
ValueError: '1.2.4': not a valid semantic version tag. Must start with 'v' or 'V'
748+

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ exclude =
2121
__pycache__,
2222
build,
2323
dist
24+
docs
25+
conftest.py

0 commit comments

Comments
 (0)