Skip to content

Commit cd73ae1

Browse files
committed
Improve package version calculation and test
br3ndonland/inboard#8 - Add version calculation using `importlib.metadata` https://docs.python.org/3/library/importlib.metadata.html - Avoid throwing error if project is not installed (such as in Docker)
1 parent 3c18de1 commit cd73ae1

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

templatepython/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
__version__ = "0.1.0"
1+
"""
2+
templatepython
3+
---
4+
"""
5+
from importlib.metadata import version
6+
7+
8+
def package_version(package: str = __package__) -> str:
9+
"""Calculate version number based on pyproject.toml"""
10+
try:
11+
return version(package)
12+
except Exception:
13+
return "Package not found."
14+
15+
16+
__version__ = package_version()

tests/test_version.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
from templatepython import __version__ # type: ignore
1+
from templatepython import __version__, package_version
2+
3+
current_version = "0.1.0"
4+
5+
6+
def test_package_version() -> None:
7+
"""Test package version calculation."""
8+
assert package_version() == current_version
9+
10+
11+
def test_package_version_not_found() -> None:
12+
"""Test package version calculation when package is not installed."""
13+
assert package_version(package="incorrect") == "Package not found."
214

315

416
def test_version() -> None:
5-
assert __version__ == "0.1.0"
17+
"""Test package version number."""
18+
assert __version__ == current_version

0 commit comments

Comments
 (0)