Skip to content

Commit 8fcf35b

Browse files
open warning with paddle.utils.deprecated (#60458)
* open_warning * update unittest * update * fix typos * fix warning in test runner * uncomment * cleanup todo * using VisibleDeprecationWarning * update comment * fix typo * fix indentation * fix * fix * fix indent level and test * update --------- Co-authored-by: SigureMo <[email protected]>
1 parent 77d7638 commit 8fcf35b

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

python/paddle/utils/deprecated.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""
1717

1818
import functools
19+
import inspect
1920
import sys
2021
import warnings
2122

@@ -24,6 +25,18 @@
2425
__all__ = []
2526

2627

28+
class VisibleDeprecationWarning(UserWarning):
29+
"""Visible deprecation warning.
30+
31+
Since Python 3.7, Python only show the DeprecationWarning if the module
32+
is __main__. So we use this warning to make the deprecation warning visible.
33+
34+
See more details from https://peps.python.org/pep-0565/
35+
"""
36+
37+
...
38+
39+
2740
def deprecated(update_to="", since="", reason="", level=0):
2841
"""Decorate a function to signify its deprecation.
2942
@@ -47,8 +60,6 @@ def deprecated(update_to="", since="", reason="", level=0):
4760
"""
4861

4962
def decorator(func):
50-
# TODO(zhiqiu): temporally disable the warnings
51-
return func
5263
"""construct warning message, and return a decorated function or class."""
5364
assert isinstance(update_to, str), 'type of "update_to" must be str.'
5465
assert isinstance(since, str), 'type of "since" must be str.'
@@ -75,9 +86,11 @@ def decorator(func):
7586
)
7687
msg += f' Please use "{_update_to}" instead.'
7788
if len(_reason) > 0:
78-
msg += f"\nreason: {_reason}"
89+
msg += f"\n Reason: {_reason}"
7990
if func.__doc__:
80-
func.__doc__ = ('\n\nWarning: ' + msg + '\n') + func.__doc__
91+
func.__doc__ = (
92+
'\n\nWarning:\n ' + msg + '\n\n'
93+
) + inspect.cleandoc(func.__doc__)
8194

8295
if level == 0:
8396
return func
@@ -110,7 +123,7 @@ def wrapper(*args, **kwargs):
110123
or v_current >= v_since
111124
):
112125
warnings.warn(
113-
warningmsg, category=DeprecationWarning, stacklevel=2
126+
warningmsg, category=VisibleDeprecationWarning, stacklevel=2
114127
)
115128

116129
return func(*args, **kwargs)

test/legacy_test/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ if(((NOT WITH_ROCM) AND (NOT WITH_GPU)) OR WIN32)
118118
list(REMOVE_ITEM TEST_OPS test_fleet_executor_cond_interceptor)
119119
endif()
120120

121-
list(REMOVE_ITEM TEST_OPS test_deprecated_decorator)
122-
123121
if(WIN32)
124122
list(REMOVE_ITEM TEST_OPS test_multiprocess_reader_exception)
125123
list(REMOVE_ITEM TEST_OPS test_trainer_desc)

test/legacy_test/test_deprecated_decorator.py

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,20 @@
1919
import numpy as np
2020

2121
import paddle
22-
from paddle import _legacy_C_ops
2322
from paddle.utils import deprecated
2423

2524
LOWEST_WARNING_POSTION = 3
2625
ERROR_WARNING_POSTION = sys.maxsize
2726

2827
# custom paddle version
29-
paddle.version.major = '1'
30-
paddle.version.minor = '8'
28+
paddle.version.major = '0'
29+
paddle.version.minor = '0'
3130
paddle.version.patch = '0'
3231
paddle.version.rc = '0'
33-
paddle.__version__ = '1.8.0'
34-
paddle.version.full_version = '1.8.0'
32+
paddle.__version__ = '0.0.0'
33+
paddle.version.full_version = '0.0.0'
3534
print("current paddle version: ", paddle.__version__)
3635

37-
paddle.disable_static()
38-
3936

4037
def get_warning_index(api):
4138
"""
@@ -49,22 +46,25 @@ def get_warning_index(api):
4946
index (int): the index of the Warinng information in its doc string if exists.
5047
"""
5148

52-
doc_lst = api.__doc__.splitlines()
53-
for idx, val in enumerate(doc_lst):
49+
doc_list = api.__doc__.splitlines()
50+
if len(doc_list) < 2:
51+
return ERROR_WARNING_POSTION
52+
for idx, (current_line, next_line) in enumerate(
53+
zip(doc_list[:-1], doc_list[1:])
54+
):
5455
if (
55-
val.startswith("Warning: ")
56-
and val.endswith(" instead.")
57-
and "and will be removed in future versions." in val
56+
current_line == "Warning:"
57+
and next_line.endswith(" instead.")
58+
and "and will be removed in future versions." in next_line
5859
):
5960
return idx
6061
return ERROR_WARNING_POSTION
6162

6263

63-
class TestDeprecatedDocorator(unittest.TestCase):
64+
class TestDeprecatedDecorator(unittest.TestCase):
6465
"""
65-
tests for paddle's Deprecated Docorator.
66+
tests for paddle's deprecated decorator.
6667
test_new_multiply: test for new api, which should not insert warning information.
67-
test_ops_elementwise_mul: test for C++ elementwise_mul op, which should not insert warning information.
6868
"""
6969

7070
def test_new_multiply(self):
@@ -87,26 +87,15 @@ def test_new_multiply(self):
8787
# testting
8888
self.assertLess(expected, captured)
8989

90-
def test_ops_elementwise_mul(self):
91-
"""
92-
Test for new C++ elementwise_op, expected result should be True,
93-
because not matter what base.layers.elementwise_mul is deprecated.
94-
"""
95-
96-
a = np.random.uniform(0.1, 1, [51, 76]).astype(np.float32)
97-
b = np.random.uniform(0.1, 1, [51, 76]).astype(np.float32)
98-
x = paddle.to_tensor(a)
99-
y = paddle.to_tensor(b)
100-
res = _legacy_C_ops.elementwise_mul(x, y)
101-
102-
# expected
103-
expected = LOWEST_WARNING_POSTION
104-
105-
# captured
106-
captured = get_warning_index(paddle.multiply)
107-
108-
# testting
109-
self.assertGreater(expected, captured)
90+
def test_indent_level(self):
91+
# test for different indent_level
92+
dataset = paddle.base.DatasetFactory().create_dataset("InMemoryDataset")
93+
with warnings.catch_warnings(record=True):
94+
dataset.set_merge_by_lineid()
95+
assert (
96+
'\nSet merge by'
97+
in paddle.base.InMemoryDataset.set_merge_by_lineid.__doc__
98+
)
11099

111100
def test_tensor_gradient(self):
112101
paddle.__version__ = '2.1.0'

0 commit comments

Comments
 (0)