diff --git a/src/haclient/__init__.py b/src/haclient/__init__.py index 6943747..d4b9853 100644 --- a/src/haclient/__init__.py +++ b/src/haclient/__init__.py @@ -12,6 +12,9 @@ from __future__ import annotations +from importlib.metadata import PackageNotFoundError +from importlib.metadata import version as _pkg_version + from haclient.api import HAClient from haclient.config import ConnectionConfig, ServicePolicy from haclient.core.connection import Connection @@ -67,4 +70,7 @@ "register_domain", ] -__version__ = "0.2.0" +try: + __version__ = _pkg_version("haclient") +except PackageNotFoundError: # pragma: no cover - only hit when package not installed + __version__ = "0.0.0+unknown" diff --git a/tests/test_packaging.py b/tests/test_packaging.py new file mode 100644 index 0000000..0fd888f --- /dev/null +++ b/tests/test_packaging.py @@ -0,0 +1,26 @@ +"""Packaging metadata tests. + +Ensures the package version is single-sourced from installed metadata +(see issue #78). +""" + +from __future__ import annotations + +from importlib.metadata import version as pkg_version + +import haclient + + +def test_version_matches_package_metadata() -> None: + """``haclient.__version__`` must match installed package metadata. + + This guards against the previous drift where ``pyproject.toml`` and + ``haclient/__init__.py`` declared different versions. + """ + assert haclient.__version__ == pkg_version("haclient") + + +def test_version_is_non_empty_string() -> None: + """``__version__`` must be a non-empty string.""" + assert isinstance(haclient.__version__, str) + assert haclient.__version__