From f68d8222b0d82480735a51095e1dcd75a2c1cebc Mon Sep 17 00:00:00 2001 From: Elise Shanholtz <eshanholtz@twilio.com> Date: Thu, 31 Oct 2019 16:34:24 -0700 Subject: [PATCH 1/4] add custom decorator to send method deprecation warning --- tests/unit/base/test_deprecation.py | 27 +++++++++++++++++++++++++++ twilio/base/obsolete.py | 19 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 tests/unit/base/test_deprecation.py diff --git a/tests/unit/base/test_deprecation.py b/tests/unit/base/test_deprecation.py new file mode 100644 index 0000000000..8663d8a976 --- /dev/null +++ b/tests/unit/base/test_deprecation.py @@ -0,0 +1,27 @@ +import unittest +import warnings + +from twilio.base.obsolete import deprecated_method + + +class DeprecatedMethodTest(unittest.TestCase): + + def test_deprecation_decorator(self): + + @deprecated_method('new_method') + def old_method(): + pass + + with warnings.catch_warnings(record=True) as caught_warnings: + warnings.simplefilter("always") + + # Call function that should raise a warning + old_method() + + if len(caught_warnings): + self.assertEqual( + str(caught_warnings[0].message), + 'Function method .old_method() is being deprecated in favor of .new_method()' + ) + assert issubclass(caught_warnings[0].category, DeprecationWarning) + diff --git a/twilio/base/obsolete.py b/twilio/base/obsolete.py index 6c89ba86dd..2d90d5f76b 100644 --- a/twilio/base/obsolete.py +++ b/twilio/base/obsolete.py @@ -21,3 +21,22 @@ def new_func(*args, **kwargs): ) return new_func + + +def deprecated_method(new_func): + """ + This is a decorator which can be used to mark deprecated methods. + It will report in a DeprecationWarning being emitted to stderr when the deprecated method is used. + """ + + def deprecated_method_wrapper(func): + + @functools.wraps(func) + def wrapper(*args, **kwargs): + warnings.warn('Function method .{}() is being deprecated in favor of .{}()'.format(func.__name__, new_func), + DeprecationWarning) + return func(*args, **kwargs) + + return wrapper + + return deprecated_method_wrapper From f4e99639a2e681adf720785ec56ed00c8b1fbfa1 Mon Sep 17 00:00:00 2001 From: Elise Shanholtz <eshanholtz@twilio.com> Date: Thu, 31 Oct 2019 16:39:21 -0700 Subject: [PATCH 2/4] fixed style --- twilio/base/obsolete.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/twilio/base/obsolete.py b/twilio/base/obsolete.py index 2d90d5f76b..aec0e49699 100644 --- a/twilio/base/obsolete.py +++ b/twilio/base/obsolete.py @@ -33,8 +33,8 @@ def deprecated_method_wrapper(func): @functools.wraps(func) def wrapper(*args, **kwargs): - warnings.warn('Function method .{}() is being deprecated in favor of .{}()'.format(func.__name__, new_func), - DeprecationWarning) + msg = 'Function method .{}() is being deprecated in favor of .{}()'.format(func.__name__, new_func) + warnings.warn(msg, DeprecationWarning) return func(*args, **kwargs) return wrapper From 6e3b9f0fb4705d2737b67ad091c5c0bec89f51d7 Mon Sep 17 00:00:00 2001 From: Elise Shanholtz <eshanholtz@twilio.com> Date: Thu, 31 Oct 2019 16:46:03 -0700 Subject: [PATCH 3/4] improved decorator test --- tests/unit/base/test_deprecation.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/base/test_deprecation.py b/tests/unit/base/test_deprecation.py index 8663d8a976..08fe69f4c3 100644 --- a/tests/unit/base/test_deprecation.py +++ b/tests/unit/base/test_deprecation.py @@ -10,13 +10,13 @@ def test_deprecation_decorator(self): @deprecated_method('new_method') def old_method(): - pass + return True with warnings.catch_warnings(record=True) as caught_warnings: warnings.simplefilter("always") - # Call function that should raise a warning - old_method() + # Call function that should raise a warning, but still execute + self.assertTrue(old_method()) if len(caught_warnings): self.assertEqual( From 547b22ec68c3fde7b9e4290b662a7d14250de114 Mon Sep 17 00:00:00 2001 From: Elise Shanholtz <eshanholtz@twilio.com> Date: Thu, 31 Oct 2019 16:55:28 -0700 Subject: [PATCH 4/4] decorator improvements --- tests/unit/base/test_deprecation.py | 18 +++++++++++++++++- twilio/base/obsolete.py | 5 +++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/tests/unit/base/test_deprecation.py b/tests/unit/base/test_deprecation.py index 08fe69f4c3..a502696ea4 100644 --- a/tests/unit/base/test_deprecation.py +++ b/tests/unit/base/test_deprecation.py @@ -8,6 +8,23 @@ class DeprecatedMethodTest(unittest.TestCase): def test_deprecation_decorator(self): + @deprecated_method() + def old_method(): + return True + + with warnings.catch_warnings(record=True) as caught_warnings: + warnings.simplefilter("always") + # Call function that should raise a warning, but still execute + self.assertTrue(old_method()) + if len(caught_warnings): + self.assertEqual( + str(caught_warnings[0].message), + 'Function method .old_method() is being deprecated' + ) + assert issubclass(caught_warnings[0].category, DeprecationWarning) + + def test_deprecation_decorator_with_new_method(self): + @deprecated_method('new_method') def old_method(): return True @@ -24,4 +41,3 @@ def old_method(): 'Function method .old_method() is being deprecated in favor of .new_method()' ) assert issubclass(caught_warnings[0].category, DeprecationWarning) - diff --git a/twilio/base/obsolete.py b/twilio/base/obsolete.py index aec0e49699..2c1057b69a 100644 --- a/twilio/base/obsolete.py +++ b/twilio/base/obsolete.py @@ -23,7 +23,7 @@ def new_func(*args, **kwargs): return new_func -def deprecated_method(new_func): +def deprecated_method(new_func=None): """ This is a decorator which can be used to mark deprecated methods. It will report in a DeprecationWarning being emitted to stderr when the deprecated method is used. @@ -33,7 +33,8 @@ def deprecated_method_wrapper(func): @functools.wraps(func) def wrapper(*args, **kwargs): - msg = 'Function method .{}() is being deprecated in favor of .{}()'.format(func.__name__, new_func) + msg = 'Function method .{}() is being deprecated'.format(func.__name__) + msg += ' in favor of .{}()'.format(new_func) if new_func else '' warnings.warn(msg, DeprecationWarning) return func(*args, **kwargs)