Skip to content

Commit 118d5e3

Browse files
malteosclaude
andauthored
fix: derive __version__ from package metadata (#2)
The publish workflow's `uv version --bump` step only edits `pyproject.toml`, but `__version__` was a hand-kept constant in `__init__.py`. After the v0.2.0 release the wheel's metadata said `0.2.0` while `commonlid.__version__` and every `summary.json`'s `commonlid_version` field stayed at `0.1.0`. Make `pyproject.toml` the single source of truth: read the version from `importlib.metadata.version("commonlid")` at import time, with a sentinel fallback for un-installed source-tree imports. Lives in a new `_version.py` to keep `__init__.py` lint-clean (RUF067 forbids runtime statements there). Add a regression test asserting `commonlid.__version__` equals `importlib.metadata.version("commonlid")` so any future drift is caught by the standard gate run instead of in production. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent a8656dd commit 118d5e3

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/commonlid/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# model's ``load()``), so this stays cheap.
77
from commonlid import datasets as _tasks # noqa: F401
88
from commonlid import models as _models # noqa: F401
9+
from commonlid._version import __version__
910
from commonlid.core.lid_dataset import LIDDataset, PrivateDatasetAccessError
1011
from commonlid.core.lid_model import LIDModel, LIDPrediction
1112
from commonlid.core.registry import (
@@ -19,8 +20,6 @@
1920
from commonlid.evaluation.evaluator import Evaluator
2021
from commonlid.evaluation.results import Result
2122

22-
__version__ = "0.1.0"
23-
2423
__all__ = [
2524
"Evaluator",
2625
"LIDDataset",

src/commonlid/_version.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Single source of truth for the package version.
2+
3+
Read from the wheel metadata so it can never drift from ``pyproject.toml``.
4+
The fallback only fires for source-tree imports that were never installed
5+
(e.g. ``PYTHONPATH=src python …``).
6+
"""
7+
8+
from __future__ import annotations
9+
10+
from importlib.metadata import PackageNotFoundError
11+
from importlib.metadata import version as _pkg_version
12+
13+
try:
14+
__version__ = _pkg_version("commonlid")
15+
except PackageNotFoundError: # pragma: no cover
16+
__version__ = "0.0.0+unknown"

tests/unit/test_cli_stub.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import json
4+
from importlib.metadata import version as _pkg_version
45

56
from typer.testing import CliRunner
67

@@ -16,6 +17,16 @@ def test_version() -> None:
1617
assert __version__ in result.stdout
1718

1819

20+
def test_version_matches_package_metadata() -> None:
21+
"""``commonlid.__version__`` must track the wheel's installed metadata.
22+
23+
Catches the failure mode where the publish workflow bumps
24+
``pyproject.toml`` but a hand-kept version constant inside the package
25+
drifts. The two should never diverge at runtime.
26+
"""
27+
assert __version__ == _pkg_version("commonlid")
28+
29+
1930
def test_list_models_json_contains_known_models() -> None:
2031
result = runner.invoke(app, ["list-models", "--json"])
2132
assert result.exit_code == 0

0 commit comments

Comments
 (0)