Skip to content

Commit

Permalink
Fix test_deprecate to work with setuptools_scm versions (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhugonnet committed Jan 15, 2024
1 parent 25deaf2 commit 41dc58f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
8 changes: 6 additions & 2 deletions geoutils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = (
Expand Down
27 changes: 18 additions & 9 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = (
Expand Down

0 comments on commit 41dc58f

Please sign in to comment.