Skip to content

fix: single-source __version__ from package metadata (#78)#87

Merged
Faerkeren merged 1 commit into
mainfrom
fix/single-source-version
May 27, 2026
Merged

fix: single-source __version__ from package metadata (#78)#87
Faerkeren merged 1 commit into
mainfrom
fix/single-source-version

Conversation

@Faerkeren
Copy link
Copy Markdown
Contributor

Closes #78.

Issue validity

Valid. Confirmed the drift firsthand:

  • pyproject.toml declared version 1.1.2
  • src/haclient/__init__.py exposed __version__ = "0.2.0"

This is exactly the inconsistency the issue called out.

Fix

Took option 1 from the issue's suggested fixes: keep haclient.__version__ (no breaking change for users) but derive it at import time from installed package metadata, so pyproject.toml is the single source of truth.

from importlib.metadata import PackageNotFoundError
from importlib.metadata import version as _pkg_version

try:
    __version__ = _pkg_version("haclient")
except PackageNotFoundError:  # pragma: no cover
    __version__ = "0.0.0+unknown"

Fallback only triggers if the package is not installed (e.g. running from a raw source tree without pip install).

Tests

Added tests/test_packaging.py:

  • test_version_matches_package_metadata — asserts haclient.__version__ == importlib.metadata.version("haclient"), preventing future drift.
  • test_version_is_non_empty_string — sanity check.

Checks run

  • ruff check src tests — pass
  • ruff format --check src tests — pass
  • mypy src (strict) — pass, no issues in 38 source files
  • pytest tests/ --cov=haclient --cov-fail-under=95318 passed, coverage 97.18%

Verified locally:

pkg meta:    1.1.2
__version__: 1.1.2

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.
@Faerkeren Faerkeren merged commit 88da5ff into main May 27, 2026
12 checks passed
@Faerkeren Faerkeren deleted the fix/single-source-version branch May 27, 2026 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Single-source the package version

1 participant