Skip to content

Commit

Permalink
Use Version objects for more robust and modular code
Browse files Browse the repository at this point in the history
  • Loading branch information
rhugonnet committed Jan 11, 2024
1 parent 90460bf commit 39e1179
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 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
25 changes: 15 additions & 10 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import re
import warnings
from copy import copy

import pytest
import yaml # type: ignore
Expand Down Expand Up @@ -58,17 +59,21 @@ 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)
# Splitting code rc in case it is a pre-release
removal_version = (
".".join(current_version.split(".")[0:2])
+ "."
+ str(int(current_version.split(".")[2].split("rc")[0]) + 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 @@ -80,7 +85,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 39e1179

Please sign in to comment.