8000 add method deprecation system warning decorator by eshanholtz · Pull Request #491 · twilio/twilio-python · GitHub
[go: up one dir, main page]

Skip to content

add method deprecation system warning decorator #491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
43 changes: 43 additions & 0 deletions tests/unit/base/test_deprecation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import unittest
import warnings

from twilio.base.obsolete import deprecated_method


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

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 in favor of .new_method()'
)
assert issubclass(caught_warnings[0].category, DeprecationWarning)
8000 20 changes: 20 additions & 0 deletions twilio/base/obsolete.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,23 @@ def new_func(*args, **kwargs):
)

return 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.
"""

def deprecated_method_wrapper(func):

@functools.wraps(func)
def wrapper(*args, **kwargs):
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)

return wrapper

return deprecated_method_wrapper
0