Skip to content

Commit 620aa6f

Browse files
committed
fix: make the version attr of the package available without install
1 parent 5968991 commit 620aa6f

3 files changed

Lines changed: 17 additions & 17 deletions

File tree

docs/conf.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,18 @@
99
import re
1010
import sys
1111
from pathlib import Path
12-
import tomllib
12+
from importlib.metadata import version
1313

1414
DOCS_DIR = Path(__file__).resolve().parent
1515
ROOT_DIR = DOCS_DIR.parent
1616

1717
sys.path.append(str(ROOT_DIR))
1818
sys.path.append(str(ROOT_DIR / "src"))
1919

20-
21-
def _read_release() -> str:
22-
pyproject_file = ROOT_DIR / "pyproject.toml"
23-
data = tomllib.loads(pyproject_file.read_text(encoding="utf-8"))
24-
25-
try:
26-
return data["project"]["version"]
27-
except KeyError as exc:
28-
raise RuntimeError(
29-
f"Could not determine khisto version from {pyproject_file}"
30-
) from exc
31-
3220
project = 'khisto-python'
3321
copyright = '2026, The Khiops Team'
3422
author = 'The Khiops Team'
35-
release = _read_release()
23+
release = version("khisto")
3624

3725
# -- General configuration ---------------------------------------------------
3826
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ dev = [
8080
{include-group = "test"},
8181
{include-group = "lint"},
8282
{include-group = "docs"},
83+
"tomli>=2.0.0; python_version < '3.11'" # TODO : Remove on Python 3.10 EOL
8384
]
8485

8586
[build-system]

src/khisto/__init__.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,22 @@
1010
logging.basicConfig(level=logging.INFO)
1111
logger = logging.getLogger(__name__)
1212

13-
ROOT_DIR = Path(os.path.dirname(os.path.abspath(__file__))).parent.absolute()
14-
13+
ROOT_DIR = Path(__file__).resolve().parent.parent
1514
KHISTO_BIN_DIR = os.environ.get("KHISTO_BIN_DIR", "khisto")
1615

17-
__version__ = version("khisto")
16+
if (ROOT_DIR / "pyproject.toml").exists():
17+
# TODO : Remove on Python 3.10 EOL
18+
try:
19+
import tomllib as tomli
20+
except ModuleNotFoundError:
21+
import tomli
22+
23+
with open(ROOT_DIR / "pyproject.toml", "rt") as f:
24+
__version__ = tomli.load(f)["project"]["version"]
25+
else:
26+
from importlib.metadata import version # noqa: E402
27+
28+
__version__ = version("khisto")
1829

1930
from .array import histogram # noqa: E402
2031
from .core import HistogramResult # noqa: E402

0 commit comments

Comments
 (0)