From f2431d1e04ccbce22e59d2fff8c3ea7ed3cea5d9 Mon Sep 17 00:00:00 2001 From: Romain Hugonnet Date: Mon, 15 Jan 2024 08:28:01 -0900 Subject: [PATCH] Fix `test_deprecate` to work with `setuptools_scm` versions (#461) --- tests/test_misc.py | 27 ++++++++++++++++++--------- xdem/misc.py | 8 ++++++-- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/tests/test_misc.py b/tests/test_misc.py index 2e3a92d0..77379308 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -60,16 +60,25 @@ def test_deprecate(self, deprecation_increment: int | None, details: str | None) warnings.simplefilter("error") - current_version = xdem.__version__ + current_version = Version(Version(xdem.__version__).base_version) # Set the removal version to be the current version plus the increment (e.g. 0.0.5 + 1 -> 0.0.6) - removal_version = ( - current_version.rsplit(".", 2)[0] - + "." - + str(int(current_version.rsplit(".", 2)[1]) + deprecation_increment) - if deprecation_increment is not None - else None - ) + if deprecation_increment is not None: + # If the micro version is already 0 and it is a decrement, decrement minor version instead + if current_version.micro == 0 and deprecation_increment == -1: + removal_version_tuple = (current_version.major, current_version.minor + deprecation_increment, 0) + # Otherwise, increment micro version + else: + removal_version_tuple = ( + current_version.major, + current_version.minor, + current_version.micro + deprecation_increment, + ) + + # Convert to version + removal_version = Version(".".join([str(v) for v in removal_version_tuple])) + else: + removal_version = None # Define a function with no use that is marked as deprecated. @xdem.misc.deprecate(removal_version, details=details) # type: ignore @@ -81,7 +90,7 @@ def useless_func() -> int: assert Version("0.0.10") > Version("0.0.8") # If True, a warning is expected. If False, a ValueError is expected. - should_warn = removal_version is None or Version(removal_version) > Version(current_version) + should_warn = removal_version is None or removal_version > current_version # Add the expected text depending on the parametrization. text = ( diff --git a/xdem/misc.py b/xdem/misc.py index 3a84bae2..7b0eb0b8 100644 --- a/xdem/misc.py +++ b/xdem/misc.py @@ -69,7 +69,7 @@ def generate_random_field(shape: tuple[int, int], corr_size: int) -> NDArrayf: return field -def deprecate(removal_version: str = None, details: str = None) -> Callable[[Any], Any]: +def deprecate(removal_version: Version = None, details: str = None) -> Callable[[Any], Any]: """ Trigger a DeprecationWarning for the decorated function. @@ -88,8 +88,12 @@ def deprecate(removal_version: str = None, details: str = None) -> Callable[[Any def deprecator_func(func: Callable[[Any], Any]) -> Callable[[Any], Any]: @functools.wraps(func) # type: ignore def new_func(*args: Any, **kwargs: Any) -> Any: + + # Get current base version (without dev changes) + current_version = Version(Version(xdem.__version__).base_version) + # True if it should warn, False if it should raise an error - should_warn = removal_version is None or Version(removal_version) > Version(xdem.__version__) + should_warn = removal_version is None or removal_version > current_version # Add text depending on the given arguments and 'should_warn'. text = (