From f6668c45b190e8296336198c89c540a9ab77297c Mon Sep 17 00:00:00 2001 From: Kiran Gopinathan Date: Fri, 22 May 2026 11:37:32 -0400 Subject: [PATCH] Read commit hash from pip metadata instead of guessing version tag When installed via pip from a git URL, pip records the exact commit hash in direct_url.json (PEP 610). The v{version} tag fallback fails when no such tag exists on the repo. Read the commit hash first, fall through to the version tag only for PyPI releases. --- lean_py/project.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lean_py/project.py b/lean_py/project.py index cfd0e99..696b260 100644 --- a/lean_py/project.py +++ b/lean_py/project.py @@ -14,9 +14,10 @@ from __future__ import annotations import hashlib +import json import os import shutil -from importlib.metadata import version as pkg_version +from importlib.metadata import distribution, version as pkg_version from pathlib import Path from lean_py.kernel import Kernel @@ -74,8 +75,25 @@ def _leanpy_version() -> str: def _leanpy_git_rev() -> str: - """Git rev for the LeanPy Lake dependency when pip-installed.""" - return os.environ.get("LEANPY_GIT_REV", f"v{_leanpy_version()}") + """Git rev for the LeanPy Lake dependency when pip-installed. + + Resolution order: LEANPY_GIT_REV env var, then the commit hash from + direct_url.json (set by pip for git installs), then v{version} tag. + """ + override = os.environ.get("LEANPY_GIT_REV") + if override: + return override + try: + dist = distribution("lean_py") + raw = dist.read_text("direct_url.json") + if raw: + info = json.loads(raw) + commit = info.get("vcs_info", {}).get("commit_id") + if commit: + return commit + except Exception: + pass + return f"v{_leanpy_version()}" def _cache_key(lean_version: str, deps: tuple[str, ...]) -> str: