Skip to content

Commit

Permalink
Merge pull request apache#3 from apache/master
Browse files Browse the repository at this point in the history
Sync beam repository.
  • Loading branch information
BingfengShu authored Oct 25, 2018
2 parents bc0d0b3 + 98533ad commit a9cbb14
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 88 deletions.
6 changes: 0 additions & 6 deletions sdks/python/apache_beam/utils/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ def exp_multiply(arg1, arg2):
from functools import partial
from functools import wraps

# Produce only the first occurrence of matching warnings regardless of
# location per line of execution. Since the number of lines of execution
# depends on the concrete runner, the number of warnings produced will
# vary depending on the runner.
warnings.simplefilter("once")


def annotate(label, since, current, extra_message, custom_message=None):
"""Decorates an API with a deprecated or experimental annotation.
Expand Down
119 changes: 37 additions & 82 deletions sdks/python/apache_beam/utils/annotations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ def test_deprecated_with_since_current_message(self):
@deprecated(since='v.1', current='multiply', extra_message='Do this')
def fnc_test_deprecated_with_since_current_message():
return 'lol'
# Deprecation warnings are ignored by default, turn them on
# to enable testing.
warnings.simplefilter("always", DeprecationWarning)
fnc_test_deprecated_with_since_current_message()
self.check_annotation(
warning=w, warning_size=1,
warning=w,
warning_type=DeprecationWarning,
obj_name='fnc_test_deprecated_with_since_current_message',
annotation_type='deprecated',
Expand All @@ -46,8 +49,11 @@ def test_deprecated_with_since_current(self):
@deprecated(since='v.1', current='multiply')
def fnc_test_deprecated_with_since_current():
return 'lol'
# Deprecation warnings are ignored by default, turn them on
# to enable testing.
warnings.simplefilter("once", DeprecationWarning)
fnc_test_deprecated_with_since_current()
self.check_annotation(warning=w, warning_size=1,
self.check_annotation(warning=w,
warning_type=DeprecationWarning,
obj_name='fnc_test_deprecated_with_since_current',
annotation_type='deprecated',
Expand All @@ -59,8 +65,11 @@ def test_deprecated_without_current(self):
@deprecated(since='v.1')
def fnc_test_deprecated_without_current():
return 'lol'
# Deprecation warnings are ignored by default, turn them on
# to enable testing.
warnings.simplefilter("once", DeprecationWarning)
fnc_test_deprecated_without_current()
self.check_annotation(warning=w, warning_size=1,
self.check_annotation(warning=w,
warning_type=DeprecationWarning,
obj_name='fnc_test_deprecated_without_current',
annotation_type='deprecated',
Expand Down Expand Up @@ -93,7 +102,7 @@ def fnc_test_experimental_with_current_message():
return 'lol'
fnc_test_experimental_with_current_message()
self.check_annotation(
warning=w, warning_size=1,
warning=w,
warning_type=FutureWarning,
obj_name='fnc_test_experimental_with_current_message',
annotation_type='experimental',
Expand All @@ -106,7 +115,7 @@ def test_experimental_with_current(self):
def fnc_test_experimental_with_current():
return 'lol'
fnc_test_experimental_with_current()
self.check_annotation(warning=w, warning_size=1,
self.check_annotation(warning=w,
warning_type=FutureWarning,
obj_name='fnc_test_experimental_with_current',
annotation_type='experimental',
Expand All @@ -118,78 +127,12 @@ def test_experimental_without_current(self):
def fnc_test_experimental_without_current():
return 'lol'
fnc_test_experimental_without_current()
self.check_annotation(warning=w, warning_size=1,
self.check_annotation(warning=w,
warning_type=FutureWarning,
obj_name='fnc_test_experimental_without_current',
annotation_type='experimental',
label_check_list=[('instead', False)])

def test_frequency(self):
"""Tests that the filter 'once' is sufficient to print once per
warning independently of location."""
with warnings.catch_warnings(record=True) as w:
@experimental()
def fnc_test_annotate_frequency():
return 'lol'

@experimental()
def fnc2_test_annotate_frequency():
return 'lol'
fnc_test_annotate_frequency()
fnc_test_annotate_frequency()
fnc2_test_annotate_frequency()
self.check_annotation(warning=[w[0]], warning_size=1,
warning_type=FutureWarning,
obj_name='fnc_test_annotate_frequency',
annotation_type='experimental',
label_check_list=[])
self.check_annotation(warning=[w[1]], warning_size=1,
warning_type=FutureWarning,
obj_name='fnc2_test_annotate_frequency',
annotation_type='experimental',
label_check_list=[])

def test_frequency_class(self):
"""Tests that the filter 'once' is sufficient to print once per
warning independently of location."""
with warnings.catch_warnings(record=True) as w:
@experimental()
class Class_test_annotate_frequency(object):
fooo = 'lol'

def __init__(self):
pass

def foo(self):
return 'lol'

@experimental()
class Class2_test_annotate_frequency(object):
fooo = 'lol'

def __init__(self):
pass

def foo(self):
return 'lol'

foo = Class_test_annotate_frequency()
foo.foo()
foo1 = Class_test_annotate_frequency()
foo1.foo()
foo2 = Class2_test_annotate_frequency()
foo2.foo()
self.check_annotation(warning=[w[0]], warning_size=1,
warning_type=FutureWarning,
obj_name='Class_test_annotate_frequency',
annotation_type='experimental',
label_check_list=[])
self.check_annotation(warning=[w[1]], warning_size=1,
warning_type=FutureWarning,
obj_name='Class2_test_annotate_frequency',
annotation_type='experimental',
label_check_list=[])

def test_decapreted_custom_no_replacements(self):
"""Tests if custom message prints an empty string
for each replacement token when only the
Expand All @@ -201,8 +144,11 @@ def test_decapreted_custom_no_replacements(self):
@deprecated(since=strSince, custom_message=strCustom)
def fnc_test_experimental_custom_no_replacements():
return 'lol'
# Deprecation warnings are ignored by default, turn them on
# to enable testing.
warnings.simplefilter("always", DeprecationWarning)
fnc_test_experimental_custom_no_replacements()
self.check_custom_annotation(warning=w, warning_size=1,
self.check_custom_annotation(warning=w,
warning_type=DeprecationWarning,
obj_name='fnc_test_experimental_custom_no_\
replacements',
Expand Down Expand Up @@ -239,9 +185,12 @@ def test_deprecated_with_since_current_message_custom(self):
custom_message=strCustom)
def fnc_test_deprecated_with_since_current_message_custom():
return 'lol'
# Deprecation warnings are ignored by default, turn them
# on to enable testing.
warnings.simplefilter("always", DeprecationWarning)
strName = fnc_test_deprecated_with_since_current_message_custom .__name__
fnc_test_deprecated_with_since_current_message_custom()
self.check_custom_annotation(warning=w, warning_size=1,
self.check_custom_annotation(warning=w,
warning_type=DeprecationWarning,
obj_name='fnc_test_deprecated_with_since_\
current_message_custom',
Expand All @@ -264,10 +213,14 @@ def __init__(self):
def foo(self):
return 'lol'

# Deprecation warnings are ignored by default, turn them
# on to enable testing.
warnings.simplefilter("always", DeprecationWarning)

foo = Class_test_deprecated_with_since_current_message()
strName = Class_test_deprecated_with_since_current_message.__name__
foo.foo()
self.check_annotation(warning=w, warning_size=1,
self.check_annotation(warning=w,
warning_type=DeprecationWarning,
obj_name=strName,
annotation_type='deprecated',
Expand All @@ -286,9 +239,14 @@ def test_experimental_with_current_message_custom(self):
custom_message=strCustom)
def fnc_test_experimental_with_current_message_custom():
return 'lol'

# Deprecation warnings are ignored by default, turn them
# on to enable testing.
warnings.simplefilter("always", DeprecationWarning)

strName = fnc_test_experimental_with_current_message_custom.__name__
fnc_test_experimental_with_current_message_custom()
self.check_custom_annotation(warning=w, warning_size=1,
self.check_custom_annotation(warning=w,
warning_type=FutureWarning,
obj_name='fnc_test_experimental\
_with_current_message_custom',
Expand All @@ -313,17 +271,16 @@ def foo(self):
foo = Class_test_experimental_with_current_message()
strName = Class_test_experimental_with_current_message.__name__
foo.foo()
self.check_annotation(warning=w, warning_size=1,
self.check_annotation(warning=w,
warning_type=FutureWarning,
obj_name=strName,
annotation_type='experimental',
label_check_list=[('instead', True),
('Do this', True)])

# helper function
def check_annotation(self, warning, warning_size, warning_type, obj_name,
def check_annotation(self, warning, warning_type, obj_name,
annotation_type, label_check_list):
self.assertEqual(1, warning_size)
self.assertTrue(issubclass(warning[-1].category, warning_type))
self.assertIn(obj_name + ' is ' + annotation_type, str(warning[-1].message))
for label in label_check_list:
Expand All @@ -333,10 +290,8 @@ def check_annotation(self, warning, warning_size, warning_type, obj_name,
self.assertNotIn(label[0], str(warning[-1].message))

# Helper function for custom messages
def check_custom_annotation(self, warning, warning_size, warning_type,
obj_name,
def check_custom_annotation(self, warning, warning_type, obj_name,
annotation_type, intended_message):
self.assertEqual(1, warning_size)
self.assertTrue(issubclass(warning[-1].category, warning_type))
self.assertIn(intended_message, str(warning[-1].message))

Expand Down

0 comments on commit a9cbb14

Please sign in to comment.