From 75c246d191aaabb02e6cce9d926de9fb40dbc77e Mon Sep 17 00:00:00 2001 From: "Philipp A." Date: Fri, 17 Jan 2025 14:10:35 +0100 Subject: [PATCH] (chore): Update version number inference in dev environments (#3441) --- .gitignore | 1 - docs/release-notes/3441.dev.md | 1 + hatch.toml | 2 +- pyproject.toml | 10 +++------ src/scanpy/__init__.py | 13 +----------- src/scanpy/_version.py | 37 ++++++++++++++++++++++++++++++++++ 6 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 docs/release-notes/3441.dev.md create mode 100644 src/scanpy/_version.py diff --git a/.gitignore b/.gitignore index 65f9de7e0a..de85b8a6b7 100644 --- a/.gitignore +++ b/.gitignore @@ -27,7 +27,6 @@ # Python build files __pycache__/ -/src/scanpy/_version.py /ci/scanpy-min-deps.txt /dist/ /*-env/ diff --git a/docs/release-notes/3441.dev.md b/docs/release-notes/3441.dev.md new file mode 100644 index 0000000000..5b95e1ab22 --- /dev/null +++ b/docs/release-notes/3441.dev.md @@ -0,0 +1 @@ +Fix version number inference in development environments (CI and local) {smaller}`P Angerer` diff --git a/hatch.toml b/hatch.toml index b0a1084c61..d61f91f577 100644 --- a/hatch.toml +++ b/hatch.toml @@ -15,7 +15,7 @@ scripts.clean = "git restore --source=HEAD --staged --worktree -- docs/release-n [envs.hatch-test] default-args = [ ] -features = [ "test", "dask-ml" ] +features = [ "dev", "test", "dask-ml" ] extra-dependencies = [ "ipykernel" ] overrides.matrix.deps.env-vars = [ { if = [ "pre" ], key = "UV_PRERELEASE", value = "allow" }, diff --git a/pyproject.toml b/pyproject.toml index 71f7f1c482..379f51d8c6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -132,11 +132,9 @@ doc = [ "sam-algorithm", ] dev = [ - # getting the dev version - "setuptools_scm", - # static checking - "pre-commit", - "towncrier", + "hatch-vcs", # runtime dev version generation + "pre-commit", # static checking + "towncrier", # release note management ] # Algorithms paga = [ "igraph" ] @@ -158,8 +156,6 @@ packages = [ "src/testing", "src/scanpy" ] [tool.hatch.version] source = "vcs" raw-options.version_scheme = "release-branch-semver" -[tool.hatch.build.hooks.vcs] -version-file = "src/scanpy/_version.py" [tool.pytest.ini_options] addopts = [ diff --git a/src/scanpy/__init__.py b/src/scanpy/__init__.py index b844372d1e..d6bfc4ac4b 100644 --- a/src/scanpy/__init__.py +++ b/src/scanpy/__init__.py @@ -6,19 +6,8 @@ from packaging.version import Version -try: # See https://github.com/maresb/hatch-vcs-footgun-example - from setuptools_scm import get_version - - __version__ = get_version(root="../..", relative_to=__file__) - del get_version -except (ImportError, LookupError): - try: - from ._version import __version__ - except ModuleNotFoundError: - msg = "scanpy is not correctly installed. Please install it, e.g. with pip." - raise RuntimeError(msg) - from ._utils import check_versions +from ._version import __version__ check_versions() del check_versions diff --git a/src/scanpy/_version.py b/src/scanpy/_version.py new file mode 100644 index 0000000000..db1dda9329 --- /dev/null +++ b/src/scanpy/_version.py @@ -0,0 +1,37 @@ +"""Get version from VCS in a dev environment or from package metadata in production. + +See . +""" + +from __future__ import annotations + +from pathlib import Path + +__all__ = ["__version__"] + + +def _get_version_from_vcs() -> str: # pragma: no cover + from hatchling.metadata.core import ProjectMetadata + from hatchling.plugin.exceptions import UnknownPluginError + from hatchling.plugin.manager import PluginManager + from hatchling.utils.fs import locate_file + + if (pyproject_toml := locate_file(__file__, "pyproject.toml")) is None: + msg = "pyproject.toml not found although hatchling is installed" + raise LookupError(msg) + root = Path(pyproject_toml).parent + metadata = ProjectMetadata(root=str(root), plugin_manager=PluginManager()) + try: + # Version can be either statically set in pyproject.toml or computed dynamically: + return metadata.core.version or metadata.hatch.version.cached + except UnknownPluginError: + msg = "Unable to import hatch plugin." + raise ImportError(msg) + + +try: + __version__ = _get_version_from_vcs() +except (ImportError, LookupError): + import importlib.metadata + + __version__ = importlib.metadata.version("scanpy")