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 b8ee74b
Showing 1 changed file with 28 additions and 14 deletions.
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:

Check warning on line 40 in hyperspy_gui_ipywidgets/__init__.py

View check run for this annotation

Codecov / codecov/patch

hyperspy_gui_ipywidgets/__init__.py#L40

Added line #L40 was not covered by tests
# setuptools_scm may not be installed
from setuptools_scm import get_version

Check warning on line 42 in hyperspy_gui_ipywidgets/__init__.py

View check run for this annotation

Codecov / codecov/patch

hyperspy_gui_ipywidgets/__init__.py#L42

Added line #L42 was not covered by tests

__version__ = get_version(_root)

Check warning on line 44 in hyperspy_gui_ipywidgets/__init__.py

View check run for this annotation

Codecov / codecov/patch

hyperspy_gui_ipywidgets/__init__.py#L44

Added line #L44 was not covered by tests
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}")

0 comments on commit b8ee74b

Please sign in to comment.