Skip to content

Commit

Permalink
(chore): Update version number inference in dev environments (#1831)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep authored Jan 17, 2025
1 parent faec0f8 commit 7b8d873
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ __pycache__/
# Distribution / packaging
/dist/
/ci/min-deps.txt
/src/anndata/_version.py
/requirements*.lock
/.python-version

Expand Down
1 change: 1 addition & 0 deletions docs/release-notes/1831.dev.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix version number inference in development environments (CI and local) {user}`flying-sheep`
8 changes: 3 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ dependencies = [
"h5py>=3.7",
"exceptiongroup; python_version<'3.11'",
"natsort",
"packaging>=20.0",
"packaging>=24.2",
# array-api-compat 1.5 has https://github.com/scverse/anndata/issues/1410
"array_api_compat>1.4,!=1.5",
"legacy-api-wrap",
Expand All @@ -60,8 +60,8 @@ Home-page = "https://github.com/scverse/anndata"

[project.optional-dependencies]
dev = [
# dev version generation
"setuptools-scm",
# runtime dev version generation
"hatch-vcs",
"anndata[dev-doc,dev-test]",
]
doc = [
Expand Down Expand Up @@ -112,8 +112,6 @@ dask = ["dask[array]>=2022.09.2,!=2024.8.*,!=2024.9.*"]

[tool.hatch.version]
source = "vcs"
[tool.hatch.build.hooks.vcs]
version-file = "src/anndata/_version.py"
raw-options.version_scheme = "release-branch-semver"
[tool.hatch.build.targets.wheel]
packages = ["src/anndata", "src/testing"]
Expand Down
13 changes: 2 additions & 11 deletions src/anndata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,16 @@

from __future__ import annotations

import sys
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Any

try: # See https://github.com/maresb/hatch-vcs-footgun-example
from setuptools_scm import get_version

__version__ = get_version(root="../..", relative_to=__file__)
except (ImportError, LookupError):
try:
from ._version import __version__
except ModuleNotFoundError:
msg = "anndata is not correctly installed. Please install it, e.g. with pip."
raise RuntimeError(msg)
from ._version import __version__

# Allowing notes to be added to exceptions. See: https://github.com/scverse/anndata/issues/868
import sys

if sys.version_info < (3, 11):
# Backport package for exception groups
import exceptiongroup # noqa: F401
Expand Down
37 changes: 37 additions & 0 deletions src/anndata/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Get version from VCS in a dev environment or from package metadata in production.
See <https://github.com/maresb/hatch-vcs-footgun-example>.
"""

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("anndata")

0 comments on commit 7b8d873

Please sign in to comment.