Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Document how to create subclass from VersionInfo #287

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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2020
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: 2 additions & 0 deletions 2 conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
sys.path.insert(0, "docs")

from coerce import coerce # noqa:E402
from semverwithvprefix import SemVerWithVPrefix


@pytest.fixture(autouse=True)
def add_semver(doctest_namespace):
doctest_namespace["semver"] = semver
doctest_namespace["coerce"] = coerce
doctest_namespace["SemVerWithVPrefix"] = SemVerWithVPrefix
31 changes: 31 additions & 0 deletions 31 docs/semverwithvprefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from semver import VersionInfo


class SemVerWithVPrefix(VersionInfo):
"""
A subclass of VersionInfo which allows a "v" prefix
"""

@classmethod
def parse(cls, version):
"""
Parse version string to a VersionInfo instance.

:param version: version string with "v" or "V" prefix
:type version: str
:raises ValueError: when version does not start with "v" or "V"
:return: a new instance
:rtype: :class:`SemVerWithVPrefix`
"""
if not version[0] in ("v", "V"):
raise ValueError(
"{v!r}: not a valid semantic version tag. Must start with 'v' or 'V'".format(
v=version
)
)
self = super(SemVerWithVPrefix, cls).parse(version[1:])
return self

def __str__(self):
# Reconstruct the tag
return "v" + super(SemVerWithVPrefix, self).__str__()
38 changes: 38 additions & 0 deletions 38 docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ The "old way" with :func:`semver.max_ver` or :func:`semver.min_ver` is still ava
'1.0.0'


.. _sec_dealing_with_invalid_versions:

Dealing with Invalid Versions
-----------------------------

Expand Down Expand Up @@ -708,3 +710,39 @@ the following methods:
For further details, see the section
`Overriding the default filter <https://docs.python.org/3/library/warnings.html#overriding-the-default-filter>`_
of the Python documentation.


.. _sec_creating_subclasses_from_versioninfo:

Creating Subclasses from VersionInfo
------------------------------------

If you do not like creating functions to modify the behavior of semver
(as shown in section :ref:`sec_dealing_with_invalid_versions`), you can
also create a subclass of the :class:`VersionInfo` class.

For example, if you want to output a "v" prefix before a version,
but the other behavior is the same, use the following code:

.. literalinclude:: semverwithvprefix.py
:language: python
:lines: 4-


The derived class :class:`SemVerWithVPrefix` can be used like
the original class:

.. code-block:: python

>>> v1 = SemVerWithVPrefix.parse("v1.2.3")
>>> assert str(v1) == "v1.2.3"
>>> print(v1)
v1.2.3
>>> v2 = SemVerWithVPrefix.parse("v2.3.4")
>>> v2 > v1
True
>>> bad = SemVerWithVPrefix.parse("1.2.4")
Traceback (most recent call last):
...
ValueError: '1.2.4': not a valid semantic version tag. Must start with 'v' or 'V'

2 changes: 2 additions & 0 deletions 2 setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ exclude =
__pycache__,
build,
dist
docs
conftest.py
4 changes: 2 additions & 2 deletions 4 test_semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,9 +1134,9 @@ def test_subclass_from_versioninfo():
class SemVerWithVPrefix(VersionInfo):
@classmethod
def parse(cls, version):
if not version.startswith("v"):
if not version[0] in ("v", "V"):
raise ValueError(
"{v}: not a valid semantic version tag".format(v=version)
"{v!r}: version must start with 'v' or 'V'".format(v=version)
)
return super(SemVerWithVPrefix, cls).parse(version[1:])

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.