Skip to content

Commit abfc8c3

Browse files
rgoyaflying-sheep
authored andcommitted
Backport PR #1972: Catch all exceptions getting version from VCS, fallback to importlib
1 parent 7271eb1 commit abfc8c3

1 file changed

Lines changed: 32 additions & 7 deletions

File tree

src/anndata/_version.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55

66
from __future__ import annotations
77

8+
import warnings
89
from pathlib import Path
910

1011
__all__ = ["__version__"]
1112

13+
_PROJECT_NAME = "anndata"
1214

13-
def _get_version_from_vcs() -> str: # pragma: no cover
15+
16+
class GetVersionError(Exception):
17+
pass
18+
19+
20+
def _get_version_from_vcs(project_name: str) -> str: # pragma: no cover
1421
from hatchling.metadata.core import ProjectMetadata
1522
from hatchling.plugin.exceptions import UnknownPluginError
1623
from hatchling.plugin.manager import PluginManager
@@ -23,15 +30,33 @@ def _get_version_from_vcs() -> str: # pragma: no cover
2330
metadata = ProjectMetadata(root=str(root), plugin_manager=PluginManager())
2431
try:
2532
# Version can be either statically set in pyproject.toml or computed dynamically:
26-
return metadata.core.version or metadata.hatch.version.cached
27-
except UnknownPluginError:
33+
version = metadata.core.version or metadata.hatch.version.cached
34+
except UnknownPluginError as e:
2835
msg = "Unable to import hatch plugin."
29-
raise ImportError(msg)
36+
raise ImportError(msg) from e
37+
except ValueError as e:
38+
msg = f"Could not find hatchling project data in TOML file, {pyproject_toml}"
39+
raise GetVersionError(msg) from e
40+
except TypeError as e:
41+
msg = "Could not parse build configuration."
42+
raise GetVersionError(msg) from e
43+
except Exception as e:
44+
msg = (
45+
f"Unknown error getting version from hatchling config for '{project_name}'."
46+
)
47+
warnings.warn(f"{msg}: {e}", stacklevel=1)
48+
raise GetVersionError(msg) from e
49+
50+
# We found a hatchling environment, but is it ours?
51+
if metadata.core.name != project_name:
52+
msg = f"Data in pyproject.toml is not related to {project_name}."
53+
raise GetVersionError(msg)
54+
return version
3055

3156

3257
try:
33-
__version__ = _get_version_from_vcs()
34-
except (ImportError, LookupError):
58+
__version__ = _get_version_from_vcs(_PROJECT_NAME)
59+
except (ImportError, LookupError, GetVersionError):
3560
import importlib.metadata
3661

37-
__version__ = importlib.metadata.version("anndata")
62+
__version__ = importlib.metadata.version(_PROJECT_NAME)

0 commit comments

Comments
 (0)