Skip to content

Commit

Permalink
Allow developers to specify when a deprecated setting will be removed (
Browse files Browse the repository at this point in the history
  • Loading branch information
ababic committed Aug 23, 2018
1 parent 1eb3916 commit 70bc813
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
16 changes: 11 additions & 5 deletions cogwheels/helpers/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,22 @@ class DeprecatedAppSetting:
app setting, and helps to raise warnings related with that deprecation.
"""
def __init__(
self, setting_name, renamed_to=None, replaced_by=None,
self, setting_name, renamed_to=None, replaced_by=None, removing_in=None,
warning_category=None, additional_guidance=None
):
self.setting_name = setting_name
self.replacement_name = renamed_to or replaced_by
self.is_renamed = renamed_to is not None
self.removing_in = removing_in
self.warning_category = warning_category or DeprecationWarning
self.additional_guidance = additional_guidance
self._prefix = ''
self.is_imminent = not issubclass(
self.warning_category, PendingDeprecationWarning)

@property
def is_imminent(self):
# TODO: Replace this with something that sniffs out the app's current
# version and compares it with self.removing_in.
return issubclass(self.warning_category, DeprecationWarning)

@property
def prefix(self):
Expand All @@ -72,6 +77,9 @@ def prefix(self, value):
self._prefix = value

def get_removed_in_version_text(self):
# To be removed once 'removed_in' is required.
if self.removing_in is not None:
return self.removing_in
if self.is_imminent:
return 'the next version'
return 'two versions time'
Expand All @@ -87,8 +95,6 @@ def _make_warning_message(self, message_format):
)

def warn_if_overridden(self, stacklevel=2):
# TODO: Figure out a clean way to raise this warning so that it appears
# to come from the override setting definition in the user's Django settings.
warnings.warn(
self._make_warning_message(DEPRECATED_SETTING_OVERRIDDEN_WARNING_FORMAT),
category=self.warning_category,
Expand Down
8 changes: 4 additions & 4 deletions cogwheels/helpers/tests/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_referencing_deprecated_setting_returns_a_value_but_raises_a_warning(sel
"The RENAMED_SETTING_OLD app setting has been renamed to RENAMED_SETTING_NEW. "
"Please update your code to reference the new setting, as continuing to reference "
"RENAMED_SETTING_OLD will cause an exception to be raised once support is removed "
"in the next version.",
"in 1.7.",
str(w[0])
)

Expand All @@ -148,7 +148,7 @@ def test_user_defined_setting_with_old_name_still_used_when_new_setting_referenc
"The COGWHEELS_TESTS_RENAMED_SETTING_OLD setting has been renamed to "
"COGWHEELS_TESTS_RENAMED_SETTING_NEW. Please update your Django settings to use the "
"new setting, otherwise the app will revert to it's default behaviour once support for "
"COGWHEELS_TESTS_RENAMED_SETTING_OLD is removed in the next version.",
"COGWHEELS_TESTS_RENAMED_SETTING_OLD is removed in 1.7.",
str(cm.warning)
)

Expand All @@ -167,7 +167,7 @@ def test_referencing_deprecated_setting_returns_a_value_but_raises_a_warning(sel
"The REPLACED_SETTING app setting is deprecated in favour of using "
"REPLACEMENT_SETTING. Please update your code to reference the new setting, as "
"continuing to reference REPLACED_SETTING will cause an exception to be raised "
"once support is removed in two versions time.",
"once support is removed in 1.8.",
str(w[0])
)
# The additional guidance should be present also
Expand All @@ -194,7 +194,7 @@ def test_user_defined_setting_with_old_name_still_used_when_new_setting_referenc
"The COGWHEELS_TESTS_REPLACED_SETTING setting is deprecated in favour of using "
"COGWHEELS_TESTS_REPLACEMENT_SETTING. Please update your Django settings to use the "
"new setting, otherwise the app will revert to it's default behaviour once support for "
"COGWHEELS_TESTS_REPLACED_SETTING is removed in two versions time.",
"COGWHEELS_TESTS_REPLACED_SETTING is removed in 1.8.",
str(cm.warning)
)
# The additional guidance should be present also
Expand Down
4 changes: 3 additions & 1 deletion cogwheels/tests/conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ class TestAppSettingsHelper(BaseAppSettingsHelper):
'RENAMED_SETTING_OLD',
renamed_to='RENAMED_SETTING_NEW',
warning_category=DeprecationWarning,
removing_in='1.7',
),
DeprecatedAppSetting(
'REPLACED_SETTING',
replaced_by='REPLACEMENT_SETTING',
additional_guidance=COMPLEX_REPLACEMENT_GUIDANCE,
warning_category=PendingDeprecationWarning,
additional_guidance=COMPLEX_REPLACEMENT_GUIDANCE
removing_in='1.8',
),
DeprecatedAppSetting(
'REPLACED_MODEL_SETTING',
Expand Down

0 comments on commit 70bc813

Please sign in to comment.