From c6e6d4dceb493dd49414ddbc9b518696a16c75f2 Mon Sep 17 00:00:00 2001 From: Denny Biasiolli Date: Thu, 8 Sep 2022 20:55:08 +0200 Subject: [PATCH] mixins: displaying deprecation notice only when needed when deprecated classes are subclassed --- src/reversion_rest_framework/mixins.py | 82 +++++++++++++++----------- 1 file changed, 46 insertions(+), 36 deletions(-) diff --git a/src/reversion_rest_framework/mixins.py b/src/reversion_rest_framework/mixins.py index 3b4edbd..1371ac0 100644 --- a/src/reversion_rest_framework/mixins.py +++ b/src/reversion_rest_framework/mixins.py @@ -21,13 +21,15 @@ class BaseHistoryMixin: class BaseHistoryModelMixin(BaseHistoryMixin): """Deprecated! Use BaseHistoryMixin instead""" - warnings.warn( - 'Deprecation notice: the "BaseHistoryModelMixin" has been renamed to ' - '"BaseHistoryMixin" and will be removed in the next version of ' - "django-reversion-rest-framework", - category=FutureWarning, - stacklevel=2, - ) + def __init_subclass__(cls): + warnings.warn( + 'Deprecation notice: the "BaseHistoryModelMixin" has been renamed to ' + '"BaseHistoryMixin" and will be removed in the next version of ' + "django-reversion-rest-framework", + category=FutureWarning, + stacklevel=2, + ) + return super().__init_subclass__() class HistoryMixin(BaseHistoryMixin): @@ -96,13 +98,15 @@ def version(self, request, pk=None, version_pk=None): class HistoryOnlyMixin(HistoryMixin): """Deprecated! Use HistoryMixin instead""" - warnings.warn( - 'Deprecation notice: the "HistoryOnlyMixin" has been renamed to ' - '"HistoryMixin" and will be removed in the next version of ' - "django-reversion-rest-framework", - category=FutureWarning, - stacklevel=2, - ) + def __init_subclass__(cls): + warnings.warn( + 'Deprecation notice: the "HistoryOnlyMixin" has been renamed to ' + '"HistoryMixin" and will be removed in the next version of ' + "django-reversion-rest-framework", + category=FutureWarning, + stacklevel=2, + ) + return super().__init_subclass__() class DeletedMixin(BaseHistoryMixin): @@ -131,26 +135,30 @@ def deleted(self, request): class DeletedOnlyMixin(DeletedMixin): """Deprecated! Use DeletedMixin instead""" - warnings.warn( - 'Deprecation notice: the "DeletedOnlyMixin" has been renamed to ' - '"DeletedMixin" and will be removed in the next version of ' - "django-reversion-rest-framework", - category=FutureWarning, - stacklevel=2, - ) + def __init_subclass__(cls): + warnings.warn( + 'Deprecation notice: the "DeletedOnlyMixin" has been renamed to ' + '"DeletedMixin" and will be removed in the next version of ' + "django-reversion-rest-framework", + category=FutureWarning, + stacklevel=2, + ) + return super().__init_subclass__() class ReadOnlyHistoryModel(HistoryMixin, DeletedMixin): """Deprecated! Use (HistoryMixin, DeletedMixin)instead""" - warnings.warn( - 'Deprecation notice: the "ReadOnlyHistoryModel" ' - "will be removed in the next version of " - "django-reversion-rest-framework. " - " Please use `HistoryMixin` and `DeletedMixin` for the same behaviour", - category=FutureWarning, - stacklevel=2, - ) + def __init_subclass__(cls): + warnings.warn( + 'Deprecation notice: the "ReadOnlyHistoryModel" ' + "will be removed in the next version of " + "django-reversion-rest-framework. " + " Please use `HistoryMixin` and `DeletedMixin` for the same behaviour", + category=FutureWarning, + stacklevel=2, + ) + return super().__init_subclass__() class RevertMixin(HistoryMixin): @@ -193,10 +201,12 @@ def revert(self, request, pk=None, version_pk=None, *args, **kwargs): class HistoryModelMixin(DeletedMixin, RevertMixin): """Deprecated! Use `(RevertMixin, DeletedMixin)` instead""" - warnings.warn( - 'Deprecation notice: the "ReadOnlyHistoryModel" ' - "will be removed in the next version of " - "django-reversion-rest-framework", - category=FutureWarning, - stacklevel=2, - ) + def __init_subclass__(cls): + warnings.warn( + 'Deprecation notice: the "ReadOnlyHistoryModel" ' + "will be removed in the next version of " + "django-reversion-rest-framework", + category=FutureWarning, + stacklevel=2, + ) + return super().__init_subclass__()