From 9c5a39519e9faaf5d483633ffbdd1e94d9dd2a33 Mon Sep 17 00:00:00 2001 From: Pere Miquel Brull Date: Fri, 10 Nov 2023 09:03:36 +0100 Subject: [PATCH 1/2] Add deprecation warnings --- .../ingestion/ometa/mixins/glossary_mixin.py | 23 ++++++++++++ .../ometa/mixins/role_policy_mixin.py | 10 ++++++ ingestion/src/metadata/utils/deprecation.py | 35 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 ingestion/src/metadata/utils/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..3f277a4ec3b1 100644 --- a/ingestion/src/metadata/ingestion/ometa/mixins/role_policy_mixin.py +++ b/ingestion/src/metadata/ingestion/ometa/mixins/role_policy_mixin.py @@ -21,6 +21,8 @@ from metadata.generated.schema.entity.policies.policy import Policy from metadata.generated.schema.entity.teams.role import Role from metadata.generated.schema.type import basic +from metadata.utils.deprecation import deprecated + from metadata.ingestion.ometa.client import REST from metadata.ingestion.ometa.mixins.patch_mixin_utils import ( OMetaPatchMixinBase, @@ -129,6 +131,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 +267,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 From 9fbc09da4fe57002ff0a83d42af5cdeb7240ee96 Mon Sep 17 00:00:00 2001 From: Pere Miquel Brull Date: Fri, 10 Nov 2023 09:19:47 +0100 Subject: [PATCH 2/2] Test deprecation warning --- .../ometa/mixins/role_policy_mixin.py | 3 +- .../tests/unit/utils/test_deprecation.py | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 ingestion/tests/unit/utils/test_deprecation.py 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 3f277a4ec3b1..68bb58e74239 100644 --- a/ingestion/src/metadata/ingestion/ometa/mixins/role_policy_mixin.py +++ b/ingestion/src/metadata/ingestion/ometa/mixins/role_policy_mixin.py @@ -21,8 +21,6 @@ from metadata.generated.schema.entity.policies.policy import Policy from metadata.generated.schema.entity.teams.role import Role from metadata.generated.schema.type import basic -from metadata.utils.deprecation import deprecated - from metadata.ingestion.ometa.client import REST from metadata.ingestion.ometa.mixins.patch_mixin_utils import ( OMetaPatchMixinBase, @@ -32,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() 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))