From 8c67ca7d17da8f75f6ad68095984534d2f4760d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A6rkeren?= <164513459+Faerkeren@users.noreply.github.com> Date: Wed, 27 May 2026 10:37:17 -0100 Subject: [PATCH] fix: single-source __version__ from package metadata (#78) Previously haclient.__version__ was hardcoded to "0.2.0" while pyproject.toml declared 1.1.2, causing the two values to drift. Derive __version__ at import time via importlib.metadata.version("haclient") so pyproject.toml is the single source of truth. Falls back to "0.0.0+unknown" only when the package is not installed (e.g. running straight from a source tree without an install). Add tests/test_packaging.py asserting __version__ matches the installed package metadata, preventing future drift. --- src/haclient/__init__.py | 8 +++++++- tests/test_packaging.py | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/test_packaging.py 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__