From e27baea3355eb1f07e6c9e4e0901113601f398e0 Mon Sep 17 00:00:00 2001 From: Pere Miquel Brull Date: Fri, 10 Nov 2023 10:47:07 +0100 Subject: [PATCH] Add deprecation warnings (#13927) --- .../ingestion/ometa/mixins/glossary_mixin.py | 23 +++++++++++ .../ometa/mixins/role_policy_mixin.py | 9 +++++ ingestion/src/metadata/utils/deprecation.py | 35 +++++++++++++++++ .../tests/unit/utils/test_deprecation.py | 39 +++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 ingestion/src/metadata/utils/deprecation.py create mode 100644 ingestion/tests/unit/utils/test_deprecation.py diff --git a/ingestion/src/metadata/ingestion/ometa/mixins/glossary_mixin.py b/ingestion/src/metadata/ingestion/ometa/mixins/glossary_mixin.py index 8dd47f50a860..d3afddfe58e2 100644 --- a/ingestion/src/metadata/ingestion/ometa/mixins/glossary_mixin.py +++ b/ingestion/src/metadata/ingestion/ometa/mixins/glossary_mixin.py @@ -30,6 +30,7 @@ PatchValue, ) from metadata.ingestion.ometa.utils import model_str +from metadata.utils.deprecation import deprecated from metadata.utils.logger import ometa_logger logger = ometa_logger() @@ -44,6 +45,7 @@ class GlossaryMixin(OMetaPatchMixinBase): To be inherited by OpenMetadata """ + @deprecated(message="Use metadata.create_or_update instead", release="1.3") def create_glossary(self, glossaries_body): """Method to create new Glossary Args: @@ -54,6 +56,7 @@ def create_glossary(self, glossaries_body): ) logger.info(f"Created a Glossary: {resp}") + @deprecated(message="Use metadata.create_or_update instead", release="1.3") def create_glossary_term(self, glossary_term_body): """Method to create new Glossary Term Args: @@ -64,6 +67,10 @@ def create_glossary_term(self, glossary_term_body): ) logger.info(f"Created a Glossary Term: {resp}") + @deprecated( + message="Use metadata.patch instead as the new standard method that will create the jsonpatch dynamically", + release="1.3", + ) def patch_glossary_term_parent( self, entity_id: Union[str, basic.Uuid], @@ -133,6 +140,10 @@ def patch_glossary_term_parent( return None + @deprecated( + message="Use metadata.patch instead as the new standard method that will create the jsonpatch dynamically", + release="1.3", + ) def patch_glossary_term_related_terms( self, entity_id: Union[str, basic.Uuid], @@ -221,6 +232,10 @@ def patch_glossary_term_related_terms( return None + @deprecated( + message="Use metadata.patch instead as the new standard method that will create the jsonpatch dynamically", + release="1.3", + ) def patch_reviewers( self, entity: Union[Type[Glossary], Type[GlossaryTerm]], @@ -307,6 +322,10 @@ def patch_reviewers( return None + @deprecated( + message="Use metadata.patch instead as the new standard method that will create the jsonpatch dynamically", + release="1.3", + ) def patch_glossary_term_synonyms( self, entity_id: Union[str, basic.Uuid], @@ -385,6 +404,10 @@ def patch_glossary_term_synonyms( return None + @deprecated( + message="Use metadata.patch instead as the new standard method that will create the jsonpatch dynamically", + release="1.3", + ) def patch_glossary_term_references( self, entity_id: Union[str, basic.Uuid], diff --git a/ingestion/src/metadata/ingestion/ometa/mixins/role_policy_mixin.py b/ingestion/src/metadata/ingestion/ometa/mixins/role_policy_mixin.py index a2556df4b462..68bb58e74239 100644 --- a/ingestion/src/metadata/ingestion/ometa/mixins/role_policy_mixin.py +++ b/ingestion/src/metadata/ingestion/ometa/mixins/role_policy_mixin.py @@ -30,6 +30,7 @@ PatchValue, ) from metadata.ingestion.ometa.utils import model_str +from metadata.utils.deprecation import deprecated from metadata.utils.logger import ometa_logger logger = ometa_logger() @@ -129,6 +130,10 @@ def _get_optional_rule_patch( ] return data + @deprecated( + message="Use metadata.patch instead as the new standard method that will create the jsonpatch dynamically", + release="1.3", + ) def patch_role_policy( self, entity_id: Union[str, basic.Uuid], @@ -261,6 +266,10 @@ def patch_role_policy( return None + @deprecated( + message="Use metadata.patch instead as the new standard method that will create the jsonpatch dynamically", + release="1.3", + ) def patch_policy_rule( self, entity_id: Union[str, basic.Uuid], diff --git a/ingestion/src/metadata/utils/deprecation.py b/ingestion/src/metadata/utils/deprecation.py new file mode 100644 index 000000000000..f1a8e3880f27 --- /dev/null +++ b/ingestion/src/metadata/utils/deprecation.py @@ -0,0 +1,35 @@ +# Copyright 2021 Collate +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Announce method deprecation +""" +import warnings +from functools import wraps + + +def deprecated(message: str, release: str): + """Decorator factory to accept specific messages for each function""" + + def _deprecated(fn): + @wraps(fn) + def inner(*args, **kwargs): + warnings.simplefilter("always", DeprecationWarning) + warnings.warn( + f"[{fn.__name__}] will be deprecated in the release [{release}]: {message}", + category=DeprecationWarning, + ) + warnings.simplefilter("default", DeprecationWarning) + + return fn(*args, **kwargs) + + return inner + + return _deprecated diff --git a/ingestion/tests/unit/utils/test_deprecation.py b/ingestion/tests/unit/utils/test_deprecation.py new file mode 100644 index 000000000000..19bcd769eab9 --- /dev/null +++ b/ingestion/tests/unit/utils/test_deprecation.py @@ -0,0 +1,39 @@ +# Copyright 2021 Collate +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Test deprecation warnings +""" + +import warnings +from unittest import TestCase + +from metadata.utils.deprecation import deprecated + + +class TestDeprecationWarning(TestCase): + """Test deprecation warnings are properly displayed""" + + @deprecated(message="This is a deprecation", release="x.y.z") + def deprecated_call(self) -> None: + """Sample method""" + + def test_deprecation_warning(self) -> None: + """Validate warning""" + + with warnings.catch_warnings(record=True) as warn: + # Trigger the warning + self.deprecated_call() + + # Verify the result + self.assertEquals(len(warn), 1) + self.assertTrue(issubclass(warn[0].category, DeprecationWarning)) + self.assertTrue("This is a deprecation" in str(warn[0].message)) + self.assertTrue("x.y.z" in str(warn[0].message))