From 41dc58f672e19b19d8d6e305529c81f5f2afe823 Mon Sep 17 00:00:00 2001 From: Romain Hugonnet Date: Mon, 15 Jan 2024 08:15:43 -0900 Subject: [PATCH] Fix `test_deprecate` to work with `setuptools_scm` versions (#429) --- geoutils/misc.py | 8 ++++++-- tests/test_misc.py | 27 ++++++++++++++++++--------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/geoutils/misc.py b/geoutils/misc.py index c82678a61..ed2b981fb 100644 --- a/geoutils/misc.py +++ b/geoutils/misc.py @@ -19,7 +19,7 @@ import geoutils -def deprecate(removal_version: str | None = None, details: str | None = None): # type: ignore +def deprecate(removal_version: Version | None = None, details: str | None = None): # type: ignore """ Trigger a DeprecationWarning for the decorated function. @@ -38,8 +38,12 @@ def deprecate(removal_version: str | None = None, details: str | None = None): def deprecator_func(func): # type: ignore @functools.wraps(func) def new_func(*args, **kwargs): # type: ignore + + # Get current base version (without dev changes) + current_version = Version(Version(geoutils.__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(geoutils.__version__) + should_warn = removal_version is None or removal_version > current_version # Add text depending on the given arguments and 'should_warn'. text = ( diff --git a/tests/test_misc.py b/tests/test_misc.py index cf9b461af..5773505d4 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -58,16 +58,25 @@ def test_deprecate(self, deprecation_increment: int | None, details: str | None) """ warnings.simplefilter("error") - current_version = geoutils.__version__ + current_version = Version(Version(geoutils.__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. @geoutils.misc.deprecate(removal_version, details=details) # type: ignore @@ -79,7 +88,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 = (