Skip to content
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
1 change: 1 addition & 0 deletions changelog.d/340.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Describe how to get version from a file
1 change: 1 addition & 0 deletions docs/advanced/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Advanced topics
display-deprecation-warnings
combine-pydantic-and-semver
convert-pypi-to-semver
version-from-file
23 changes: 23 additions & 0 deletions docs/advanced/version-from-file.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.. _sec_reading_versions_from_file:

Reading versions from file
==========================

In cases where a version is stored inside a file, one possible solution
is to use the following function:

.. code-block:: python

from semver.version import Version

def get_version(path: str = "version") -> Version:
"""
Construct a Version from a file

:param path: A text file only containing the semantic version
:return: A :class:`Version` object containing the semantic
version from the file.
"""
with open(path,"r") as fh:
version = fh.read().strip()
return Version.parse(version)