diff --git a/openeo/internal/warnings.py b/openeo/internal/warnings.py index 2c5335bc4..fe9d5489a 100644 --- a/openeo/internal/warnings.py +++ b/openeo/internal/warnings.py @@ -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 @@ -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." diff --git a/openeo/rest/job.py b/openeo/rest/job.py index 0dd3ff271..2248869b3 100644 --- a/openeo/rest/job.py +++ b/openeo/rest/job.py @@ -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: """ @@ -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): @@ -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: @@ -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): @@ -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""" diff --git a/tests/internal/test_warnings.py b/tests/internal/test_warnings.py index 4e7f6da8d..30c1e0138 100644 --- a/tests/internal/test_warnings.py +++ b/tests/internal/test_warnings.py @@ -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." ), @@ -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." ), @@ -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." ) @@ -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." ) @@ -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__ == ( diff --git a/tests/rest/datacube/test_datacube100.py b/tests/rest/datacube/test_datacube100.py index 2dba03ebf..97e6bd46e 100644 --- a/tests/rest/datacube/test_datacube100.py +++ b/tests/rest/datacube/test_datacube100.py @@ -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"