Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions shapash/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
VERSION = (2, 9, 0)
from importlib.metadata import PackageNotFoundError, version

__version__ = ".".join(map(str, VERSION))
try:
__version__ = version("shapash")
except PackageNotFoundError: # running from a source tree without an install
__version__ = "0+unknown"

VERSION = tuple(int(p) for p in __version__.split(".")[:3] if p.isdigit())
32 changes: 32 additions & 0 deletions tests/unit_tests/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import importlib
import sys
from importlib.metadata import PackageNotFoundError
from unittest.mock import patch

import pytest

import shapash # noqa: F401

# ``shapash.__version__`` resolves to the version string, not the submodule.
version_module = sys.modules["shapash.__version__"]


@pytest.fixture
def reload_version_module():
"""Re-execute ``shapash.__version__`` under a test patch, then restore the real values."""
yield lambda: importlib.reload(version_module)
importlib.reload(version_module)


def test_version_reads_package_metadata(reload_version_module):
with patch("importlib.metadata.version", return_value="2.10.1"):
reload_version_module()
assert version_module.__version__ == "2.10.1"
assert version_module.VERSION == (2, 10, 1)


def test_version_falls_back_when_not_installed(reload_version_module):
with patch("importlib.metadata.version", side_effect=PackageNotFoundError("shapash")):
reload_version_module()
assert version_module.__version__ == "0+unknown"
assert version_module.VERSION == ()
7 changes: 4 additions & 3 deletions tests/unit_tests/utils/test_load_smartpredictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import unittest
import warnings
from os import path
from unittest.mock import patch

import catboost as cb
import numpy as np
Expand Down Expand Up @@ -117,6 +118,7 @@ def test_load_with_schema_fingerprint_mismatch_raises(self) -> None:
with pytest.raises(ValueError, match="schema fingerprint mismatch"):
load_smartpredictor(pkl)

@patch("shapash.utils.io.shapash_version", "2.9.0")
def test_load_with_major_version_mismatch_raises(self) -> None:
predictor = self._make_predictor()
with tempfile.TemporaryDirectory() as tmp:
Expand All @@ -131,17 +133,16 @@ def test_load_with_major_version_mismatch_raises(self) -> None:
with pytest.raises(ValueError, match="Major version mismatch"):
load_smartpredictor(pkl)

@patch("shapash.utils.io.shapash_version", "2.9.0")
def test_load_with_minor_version_mismatch_warns(self) -> None:
from shapash.__version__ import VERSION

predictor = self._make_predictor()
with tempfile.TemporaryDirectory() as tmp:
pkl = os.path.join(tmp, "predictor.pkl")
predictor.save(pkl)
manifest_path = pkl + MANIFEST_SUFFIX
with open(manifest_path, encoding="utf-8") as f:
manifest = json.load(f)
manifest["shapash_version"] = f"{VERSION[0]}.{VERSION[1] + 1}.0"
manifest["shapash_version"] = "2.10.0"
with open(manifest_path, "w", encoding="utf-8") as f:
json.dump(manifest, f)
with pytest.warns(UserWarning, match="Minor version skew"):
Expand Down
Loading