Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deprecation decorators #1683

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 53 additions & 0 deletions parcels/util/_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Internal helpers for Parcels."""

import functools
import warnings
from typing import Callable

PACKAGE = "Parcels"


def deprecated(msg: str = "") -> Callable:
"""Decorator marking a function as being deprecated

Parameters
----------
msg : str, optional
Custom message to append to the deprecation warning.

Examples
--------
```
@deprecated("Please use `another_function` instead")
def some_old_function(x, y):
return x + y

@deprecated()
def some_other_old_function(x, y):
return x + y
```
"""
if msg:
msg = " " + msg
erikvansebille marked this conversation as resolved.
Show resolved Hide resolved

def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
msg_formatted = (
f"`{func.__qualname__}` is deprecated and will be removed in a future release of {PACKAGE}.{msg}"

Check warning on line 37 in parcels/util/_helpers.py

View check run for this annotation

Codecov / codecov/patch

parcels/util/_helpers.py#L37

Added line #L37 was not covered by tests
)

warnings.warn(msg_formatted, category=DeprecationWarning, stacklevel=2)
return func(*args, **kwargs)

return wrapper

return decorator


def deprecated_made_private(func: Callable) -> Callable:
return deprecated(
"It has moved to the internal API as it is not expected to be directly used by "
"the end-user. If you feel that you use this code directly in your scripts, please "
"comment on our tracking issue at <>." # TODO: Add tracking issue
)(func)
Empty file added tests/util/__init__.py
Empty file.
56 changes: 56 additions & 0 deletions tests/util/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import pytest

from parcels.util._helpers import deprecated, deprecated_made_private

# Write me tests to test deprecated and depreacted_made_private decorators
# They should raise a DeprecationWarning, and also have the function name (or in the case of a method, {class_name}.{method_name}) in the warning message
VeckoTheGecko marked this conversation as resolved.
Show resolved Hide resolved


def test_deprecated():
class SomeClass:
@deprecated()
def some_method(self, x, y):
return x + y

@staticmethod
@deprecated()
def some_static_method(x, y):
return x + y

@property
@deprecated()
def some_property(self):
return 2

@deprecated()
def some_function(x, y):
return x + y

with pytest.warns(DeprecationWarning) as record:
SomeClass().some_method(1, 2)
assert "SomeClass.some_method" in record[0].message.args[0]

with pytest.warns(DeprecationWarning) as record:
SomeClass.some_static_method(1, 2)
assert "SomeClass.some_static_method" in record[0].message.args[0]

with pytest.warns(DeprecationWarning) as record:
_ = SomeClass().some_property
assert "SomeClass.some_property" in record[0].message.args[0]

with pytest.warns(DeprecationWarning) as record:
some_function(1, 2)
assert "some_function" in record[0].message.args[0]

with pytest.warns(DeprecationWarning) as record:
some_function(1, 2)
assert "some_function" in record[0].message.args[0]


def test_deprecated_made_private():
@deprecated_made_private
def some_function(x, y):
return x + y

with pytest.warns(DeprecationWarning):
some_function(1, 2)
Loading