Repository: beam Updated Branches: refs/heads/master 730b14e11 -> 42e3a6f85
Add extra message to deprecated annotation Project: http://git-wip-us.apache.org/repos/asf/beam/repo Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/d9595d35 Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/d9595d35 Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/d9595d35 Branch: refs/heads/master Commit: d9595d35176d2572423f6be069982b05eea204dd Parents: 730b14e Author: Maria Garcia Herrero <[email protected]> Authored: Wed Apr 26 16:22:09 2017 -0700 Committer: Ahmet Altay <[email protected]> Committed: Thu Apr 27 11:25:14 2017 -0700 ---------------------------------------------------------------------- sdks/python/apache_beam/utils/annotations.py | 13 ++++++--- .../apache_beam/utils/annotations_test.py | 29 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/beam/blob/d9595d35/sdks/python/apache_beam/utils/annotations.py ---------------------------------------------------------------------- diff --git a/sdks/python/apache_beam/utils/annotations.py b/sdks/python/apache_beam/utils/annotations.py index 263eb94..92318b1 100644 --- a/sdks/python/apache_beam/utils/annotations.py +++ b/sdks/python/apache_beam/utils/annotations.py @@ -69,13 +69,14 @@ from functools import wraps warnings.simplefilter("once") -def annotate(label, since, current): +def annotate(label, since, current, extra_message): """Decorates a function with a deprecated or experimental annotation. Args: label: the kind of annotation ('deprecated' or 'experimental'). since: the version that causes the annotation. current: the suggested replacement function. + extra_message: an optional additional message. Returns: The decorator for the function. @@ -90,7 +91,9 @@ def annotate(label, since, current): message = '%s is %s' % (fnc.__name__, label) if label == 'deprecated': message += ' since %s' % since - message += '. Use %s instead.'% current if current else '.' + message += '. Use %s instead.' % current if current else '.' + if extra_message: + message += '. ' + extra_message warnings.warn(message, warning_type) return fnc(*args, **kwargs) return inner @@ -100,5 +103,7 @@ def annotate(label, since, current): # Use partial application to customize each annotation. # 'current' will be optional in both deprecated and experimental # while 'since' will be mandatory for deprecated. -deprecated = partial(annotate, label='deprecated', current=None) -experimental = partial(annotate, label='experimental', current=None, since=None) +deprecated = partial(annotate, label='deprecated', + current=None, extra_message=None) +experimental = partial(annotate, label='experimental', + current=None, since=None, extra_message=None) http://git-wip-us.apache.org/repos/asf/beam/blob/d9595d35/sdks/python/apache_beam/utils/annotations_test.py ---------------------------------------------------------------------- diff --git a/sdks/python/apache_beam/utils/annotations_test.py b/sdks/python/apache_beam/utils/annotations_test.py index 64a24ee..32af8a9 100644 --- a/sdks/python/apache_beam/utils/annotations_test.py +++ b/sdks/python/apache_beam/utils/annotations_test.py @@ -24,6 +24,21 @@ from apache_beam.utils.annotations import experimental class AnnotationTests(unittest.TestCase): # Note: use different names for each of the the functions decorated # so that a warning is produced for each of them. + def test_deprecated_with_since_current_message(self): + with warnings.catch_warnings(record=True) as w: + @deprecated(since='v.1', current='multiply', extra_message='Do this') + def fnc_test_deprecated_with_since_current_message(): + return 'lol' + fnc_test_deprecated_with_since_current_message() + self.check_annotation( + warning=w, warning_size=1, + warning_type=DeprecationWarning, + fnc_name='fnc_test_deprecated_with_since_current_message', + annotation_type='deprecated', + label_check_list=[('since', True), + ('instead', True), + ('Do this', True)]) + def test_deprecated_with_since_current(self): with warnings.catch_warnings(record=True) as w: @deprecated(since='v.1', current='multiply') @@ -60,6 +75,20 @@ class AnnotationTests(unittest.TestCase): fnc_test_deprecated_without_since_should_fail() assert not w + def test_experimental_with_current_message(self): + with warnings.catch_warnings(record=True) as w: + @experimental(current='multiply', extra_message='Do this') + def fnc_test_experimental_with_current_message(): + return 'lol' + fnc_test_experimental_with_current_message() + self.check_annotation( + warning=w, warning_size=1, + warning_type=FutureWarning, + fnc_name='fnc_test_experimental_with_current_message', + annotation_type='experimental', + label_check_list=[('instead', True), + ('Do this', True)]) + def test_experimental_with_current(self): with warnings.catch_warnings(record=True) as w: @experimental(current='multiply')
