Skip to content

Commit

Permalink
(chore): Update version number inference in dev environments (#3441)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep authored Jan 17, 2025
1 parent 05ab694 commit 75c246d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

# Python build files
__pycache__/
/src/scanpy/_version.py
/ci/scanpy-min-deps.txt
/dist/
/*-env/
Expand Down
1 change: 1 addition & 0 deletions docs/release-notes/3441.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) {smaller}`P Angerer`
2 changes: 1 addition & 1 deletion hatch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
10 changes: 3 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Expand All @@ -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 = [
Expand Down
13 changes: 1 addition & 12 deletions src/scanpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions src/scanpy/_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("scanpy")

0 comments on commit 75c246d

Please sign in to comment.