Skip to content

Commit

Permalink
Improve getting __version__
Browse files Browse the repository at this point in the history
  • Loading branch information
ericpre committed Feb 3, 2024
1 parent 34f1872 commit a07478e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: get repository name
shell: bash
run: echo "REPOSITORY_NAME=${GITHUB_REPOSITORY#*/}" >> $GITHUB_ENV

- name: Fetch tags upstream
if: ${{ github.repository_owner != 'hyperspy' }}
# Needs to fetch the tags from upstream to get the
# correct version with setuptools_scm
run: |
git remote add upstream https://github.com/hyperspy/hyperspy_gui_ipywidgets.git
git fetch --prune --unshallow
git remote add upstream https://github.com/hyperspy/${{ env.REPOSITORY_NAME }}.git
git fetch upstream --tags
- uses: actions/setup-python@v4
Expand Down
42 changes: 28 additions & 14 deletions hyperspy_gui_ipywidgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,33 @@


import importlib
from importlib.metadata import version
from pathlib import Path


if Path(__file__).parent.parent.name == "site-packages": # pragma: no cover
# Tested in the "Package & Test" workflow on GitHub CI
from importlib.metadata import version
__version__ = version("rosettasciio")

__version__ = version("hyperspy_gui_ipywidgets")
else:
# Editable install
from setuptools_scm import get_version
# For development version, `setuptools_scm` will be used at build time
# to get the dev version, in case of missing vcs information (git archive,
# shallow repository), the fallback version defined in pyproject.toml will
# be used

__version__ = get_version(Path(__file__).parent.parent)
# if we have a editable install from a git repository try to use
# `setuptools_scm` to find a more accurate version:
# `importlib.metadata` will provide the version at installation
# time and for editable version this may be different

# we only do that if we have enough git history, e.g. not shallow checkout
_root = Path(__file__).resolve().parents[1]
if (_root / ".git").exists() and not (_root / ".git/shallow").exists():
try:
# setuptools_scm may not be installed
from setuptools_scm import get_version

__version__ = get_version(_root)
except ImportError: # pragma: no cover
# setuptools_scm not install, we keep the existing __version__
pass


__all__ = [
Expand All @@ -49,11 +63,11 @@ def __dir__():


def __getattr__(name):
# lazy loading of module: this is only call when the attribute "name" is not found
# in the module
# See https://peps.python.org/pep-0562/
if name in __all__:
if name == "__version__":
return __version__
else:
return importlib.import_module(
"." + name, 'hyperspy_gui_ipywidgets'
)
return importlib.import_module(
"." + name, 'hyperspy_gui_ipywidgets'
)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,5 @@ include = ["hyperspy_gui_ipywidgets*"]

[tool.setuptools_scm]
# Presence enables setuptools_scm, the version will be determine at build time from git
# The version will be updated by the `prepare_release.py` script
fallback_version = "2.1.dev0"

0 comments on commit a07478e

Please sign in to comment.