Skip to content

Commit

Permalink
Merge pull request #114 from statisticsnorway/add_version_to_init
Browse files Browse the repository at this point in the history
Add version to init
  • Loading branch information
aecorn authored Jan 31, 2024
2 parents 27d7a90 + 82ca4ef commit 0889456
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ipython = ">=8.10.0"
jupyterhub = ">=3.0.0"
google-cloud-storage = ">=2.7.0"
pyjwt = ">=2.6.0"
tomli = ">=1.1.0"
google-cloud-pubsub = ">=2.14.1"
pyarrow-stubs = ">=10.0.1.7"
google-auth-stubs = ">=0.2.0" # Not maintained by Google, should change if Google releases their own stubs
Expand Down Expand Up @@ -85,6 +86,7 @@ module = [
"jupyterhub.*",
"fsspec.*",
"responses.*",
"tomli.*",
]
ignore_missing_imports = true

Expand Down
31 changes: 31 additions & 0 deletions src/dapla/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
"""Dapla Toolbelt collection."""

import importlib.metadata
import warnings

import tomli

from .auth import AuthClient
from .backports import details
from .backports import show
Expand Down Expand Up @@ -29,3 +34,29 @@
"write_pandas",
"trigger_source_data_processing",
]


# Register function for second try: getting from pyproject directly (when running under pytest for example)
def _try_getting_pyproject_toml(e: Exception | None = None) -> str:
"""Look for version in pyproject.toml, if not found, set to 0.0.0 ."""
if e is None:
passed_excep: Exception = Exception("")
else:
passed_excep = e
try:
with open("../pyproject.toml", "rb") as f:
toml_dict = tomli.load(f)
version: str = toml_dict["tool"]["poetry"]["version"]
return version
except Exception as e:
version_missing: str = "0.0.0"
warn_msg = f"Error from dapla-toolbelt __init__, not able to get version-number, setting it to {version_missing}: {e}, passed in from importlib: {passed_excep}"
warnings.warn(warn_msg, category=Warning, stacklevel=2)
return version_missing


# First try is setting __version__ attribute programatically from installed package
try:
__version__ = importlib.metadata.version("dapla-toolbelt")
except importlib.metadata.PackageNotFoundError as e:
__version__ = _try_getting_pyproject_toml(e)
5 changes: 5 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import dapla


def test_version_attribute() -> None:
assert dapla.__version__ != "0.0.0"

0 comments on commit 0889456

Please sign in to comment.