Skip to content

Commit

Permalink
Issue #642 Fix warning message of legacy aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Oct 9, 2024
1 parent f7f035f commit 8d5e22e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
7 changes: 5 additions & 2 deletions openeo/internal/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def test_warnings(stacklevel=1):
)


def legacy_alias(orig: Callable, name: str = "n/a", *, since: str, mode: str = "full"):
def legacy_alias(orig: Callable, name: str, *, since: str, mode: str = "full"):
"""
Create legacy alias of given function/method/classmethod/staticmethod
:param orig: function/method to create legacy alias for
:param name: name of the alias (unused)
:param name: original name of the alias
:param since: version since when this is alias is deprecated
:param mode:
- "full": raise warnings on calling, only have deprecation note as doc
Expand Down Expand Up @@ -69,6 +69,9 @@ def legacy_alias(orig: Callable, name: str = "n/a", *, since: str, mode: str = "
def wrapper(*args, **kwargs):
return orig(*args, **kwargs)

# Set deprecated name on the wrapper so that deprecation warnings use proper name.
wrapper.__name__ = name

ref = f":py:{'meth' if 'method' in kind else 'func'}:`.{orig.__name__}`"
message = f"Usage of this legacy {kind} is deprecated. Use {ref} instead."

Expand Down
10 changes: 5 additions & 5 deletions openeo/rest/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def describe(self) -> dict:
"""
return self.connection.get(f"/jobs/{self.job_id}", expected_status=200).json()

describe_job = legacy_alias(describe, since="0.20.0", mode="soft")
describe_job = legacy_alias(describe, name="describe_job", since="0.20.0", mode="soft")

def status(self) -> str:
"""
Expand All @@ -96,7 +96,7 @@ def delete(self):
"""
self.connection.delete(f"/jobs/{self.job_id}", expected_status=204)

delete_job = legacy_alias(delete, since="0.20.0", mode="soft")
delete_job = legacy_alias(delete, name="delete_job", since="0.20.0", mode="soft")

@openeo_endpoint("GET /jobs/{job_id}/estimate")
def estimate(self):
Expand All @@ -107,7 +107,7 @@ def estimate(self):
currency = self.connection.capabilities().currency()
return VisualDict('job-estimate', data=data, parameters={'currency': currency})

estimate_job = legacy_alias(estimate, since="0.20.0", mode="soft")
estimate_job = legacy_alias(estimate, name="estimate_job", since="0.20.0", mode="soft")

@openeo_endpoint("POST /jobs/{job_id}/results")
def start(self) -> BatchJob:
Expand All @@ -122,7 +122,7 @@ def start(self) -> BatchJob:
self.connection.post(f"/jobs/{self.job_id}/results", expected_status=202)
return self

start_job = legacy_alias(start, since="0.20.0", mode="soft")
start_job = legacy_alias(start, name="start_job", since="0.20.0", mode="soft")

@openeo_endpoint("DELETE /jobs/{job_id}/results")
def stop(self):
Expand All @@ -134,7 +134,7 @@ def stop(self):
"""
self.connection.delete(f"/jobs/{self.job_id}/results", expected_status=204)

stop_job = legacy_alias(stop, since="0.20.0", mode="soft")
stop_job = legacy_alias(stop, name="stop_job", since="0.20.0", mode="soft")

def get_results_metadata_url(self, *, full: bool = False) -> str:
"""Get results metadata URL"""
Expand Down
10 changes: 5 additions & 5 deletions tests/internal/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def add(x, y):
with pytest.warns(
UserDeprecationWarning,
match=re.escape(
"Call to deprecated function (or staticmethod) add."
"Call to deprecated function (or staticmethod) do_plus."
" (Usage of this legacy function is deprecated. Use `.add` instead.)"
" -- Deprecated since version v1.2."
),
Expand Down Expand Up @@ -71,7 +71,7 @@ def add(self, x, y):
with pytest.warns(
UserDeprecationWarning,
match=re.escape(
"Call to deprecated method add."
"Call to deprecated method do_plus."
" (Usage of this legacy method is deprecated. Use `.add` instead.)"
" -- Deprecated since version v1.2."
),
Expand Down Expand Up @@ -103,7 +103,7 @@ def add(cls, x, y):

expected_warning = re.escape(
# Workaround for bug in classmethod detection before Python 3.9 (see https://wrapt.readthedocs.io/en/latest/decorators.html#decorating-class-methods
f"Call to deprecated {'class method' if sys.version_info >= (3, 9) else 'function (or staticmethod)'} add."
f"Call to deprecated {'class method' if sys.version_info >= (3, 9) else 'function (or staticmethod)'} do_plus."
" (Usage of this legacy class method is deprecated. Use `.add` instead.)"
" -- Deprecated since version v1.2."
)
Expand Down Expand Up @@ -138,7 +138,7 @@ def add(x, y):
assert len(recwarn) == 0

expected_warning = re.escape(
"Call to deprecated function (or staticmethod) add."
"Call to deprecated function (or staticmethod) do_plus."
" (Usage of this legacy static method is deprecated. Use `.add` instead.)"
" -- Deprecated since version v1.2."
)
Expand All @@ -157,7 +157,7 @@ def add(self, x, y):
"""Add x and y."""
return x + y

do_plus = legacy_alias(add, since="v1.2", mode="soft")
do_plus = legacy_alias(add, name="do_plus", since="v1.2", mode="soft")

assert Foo.add.__doc__ == "Add x and y."
assert Foo.do_plus.__doc__ == (
Expand Down
4 changes: 2 additions & 2 deletions tests/rest/datacube/test_datacube100.py
Original file line number Diff line number Diff line change
Expand Up @@ -3394,10 +3394,10 @@ def test_create_job_invalid_header(self, con100, requests_mock, add_header, job_
_ = cube.create_job(out_format="GTiff")

def test_legacy_send_job(self, con100, requests_mock):
"""Legacy `DataCube.send_job` alis for `create_job"""
"""Legacy `DataCube.send_job` alias for `create_job"""
requests_mock.post(API_URL + "/jobs", json=self._get_handler_post_jobs())
cube = con100.load_collection("S2")
expected_warning = "Call to deprecated method create_job. (Usage of this legacy method is deprecated. Use `.create_job` instead.) -- Deprecated since version 0.10.0."
expected_warning = "Call to deprecated method send_job. (Usage of this legacy method is deprecated. Use `.create_job` instead.) -- Deprecated since version 0.10.0."
with pytest.warns(UserDeprecationWarning, match=re.escape(expected_warning)):
job = cube.send_job(out_format="GTiff")
assert job.job_id == "myj0b1"
Expand Down

0 comments on commit 8d5e22e

Please sign in to comment.