@@ -571,6 +571,8 @@ The "old way" with :func:`semver.max_ver` or :func:`semver.min_ver` is still ava
571571 ' 1.0.0'
572572
573573
574+ .. _sec_dealing_with_invalid_versions :
575+
574576Dealing with Invalid Versions
575577-----------------------------
576578
@@ -749,3 +751,39 @@ the following methods:
749751 For further details, see the section
750752 `Overriding the default filter <https://docs.python.org/3/library/warnings.html#overriding-the-default-filter >`_
751753 of the Python documentation.
754+
755+
756+ .. _sec_creating_subclasses_from_versioninfo :
757+
758+ Creating Subclasses from VersionInfo
759+ ------------------------------------
760+
761+ If you do not like creating functions to modify the behavior of semver
762+ (as shown in section :ref: `sec_dealing_with_invalid_versions `), you can
763+ also create a subclass of the :class: `VersionInfo ` class.
764+
765+ For example, if you want to output a "v" prefix before a version,
766+ but the other behavior is the same, use the following code:
767+
768+ .. literalinclude :: semverwithvprefix.py
769+ :language: python
770+ :lines: 4-
771+
772+
773+ The derived class :class: `SemVerWithVPrefix ` can be used like
774+ the original class:
775+
776+ .. code-block :: python
777+
778+ >> > v1 = SemVerWithVPrefix.parse(" v1.2.3" )
779+ >> > assert str (v1) == " v1.2.3"
780+ >> > print (v1)
781+ v1.2.3
782+ >> > v2 = SemVerWithVPrefix.parse(" v2.3.4" )
783+ >> > v2 > v1
784+ True
785+ >> > bad = SemVerWithVPrefix.parse(" 1.2.4" )
786+ Traceback (most recent call last):
787+ ...
788+ ValueError : ' 1.2.4' : not a valid semantic version tag. Must start with ' v' or ' V'
789+
0 commit comments