Skip to content

Commit 1cd60ea

Browse files
farhanclaude
andcommitted
fix: replace hardcoded __version__ with importlib.metadata
A static string in __init__.py is a second source of truth that can drift from pyproject.toml. importlib.metadata.version() reads the installed package metadata, so there is only one authoritative place. Also updates docs/conf.py to use the same approach instead of reading the source file with a regex. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1504146 commit 1cd60ea

2 files changed

Lines changed: 11 additions & 23 deletions

File tree

docs/conf.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,15 @@
1212
serve to show the default.
1313
"""
1414
import os
15-
import re
1615
import sys
1716
from datetime import datetime, UTC
17+
from importlib.metadata import PackageNotFoundError, version
1818
from subprocess import check_call
1919

20-
21-
def get_version(*file_paths):
22-
"""
23-
Extract the version string from the file.
24-
25-
Input:
26-
- file_paths: relative path fragments to file with
27-
version string
28-
"""
29-
filename = os.path.join(os.path.dirname(__file__), *file_paths)
30-
version_file = open(filename, encoding="utf8").read()
31-
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M)
32-
if version_match:
33-
return version_match.group(1)
34-
raise RuntimeError("Unable to find version string.")
35-
36-
37-
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
38-
sys.path.append(REPO_ROOT)
39-
40-
VERSION = get_version("../xapi_db_load", "__init__.py")
20+
try:
21+
VERSION = version("xapi-db-load")
22+
except PackageNotFoundError:
23+
VERSION = "unknown"
4124

4225

4326
# If extensions (or modules to document with autodoc) are in another directory,

xapi_db_load/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22
Scripts to generate fake xAPI data against various backends.
33
"""
44

5-
__version__ = "3.1.0"
5+
from importlib.metadata import PackageNotFoundError, version
6+
7+
try:
8+
__version__ = version("xapi-db-load")
9+
except PackageNotFoundError:
10+
__version__ = "unknown"

0 commit comments

Comments
 (0)