Skip to content
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

open warning with paddle.utils.deprecated #60458

Merged
merged 15 commits into from
Jan 2, 2024
23 changes: 18 additions & 5 deletions python/paddle/utils/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""

import functools
import inspect
import sys
import warnings

Expand All @@ -24,6 +25,18 @@
__all__ = []


class VisibleDeprecationWarning(UserWarning):
"""Visible deprecation warning.

Since Python 3.7, Python only show the DeprecationWarning if the module
is __main__. So we use this warning to make the deprecation warning visible.

See more details from https://peps.python.org/pep-0565/
"""

...


def deprecated(update_to="", since="", reason="", level=0):
"""Decorate a function to signify its deprecation.

Expand All @@ -47,8 +60,6 @@ def deprecated(update_to="", since="", reason="", level=0):
"""

def decorator(func):
# TODO(zhiqiu): temporally disable the warnings
return func
"""construct warning message, and return a decorated function or class."""
assert isinstance(update_to, str), 'type of "update_to" must be str.'
assert isinstance(since, str), 'type of "since" must be str.'
Expand All @@ -75,9 +86,11 @@ def decorator(func):
)
msg += f' Please use "{_update_to}" instead.'
if len(_reason) > 0:
msg += f"\nreason: {_reason}"
msg += f"\n Reason: {_reason}"
if func.__doc__:
func.__doc__ = ('\n\nWarning: ' + msg + '\n') + func.__doc__
func.__doc__ = (
'\n\nWarning:\n ' + msg + '\n\n'
) + inspect.cleandoc(func.__doc__)

if level == 0:
return func
Expand Down Expand Up @@ -110,7 +123,7 @@ def wrapper(*args, **kwargs):
or v_current >= v_since
):
warnings.warn(
warningmsg, category=DeprecationWarning, stacklevel=2
warningmsg, category=VisibleDeprecationWarning, stacklevel=2
)

return func(*args, **kwargs)
Expand Down
2 changes: 0 additions & 2 deletions test/legacy_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ if(((NOT WITH_ROCM) AND (NOT WITH_GPU)) OR WIN32)
list(REMOVE_ITEM TEST_OPS test_fleet_executor_cond_interceptor)
endif()

list(REMOVE_ITEM TEST_OPS test_deprecated_decorator)

if(WIN32)
list(REMOVE_ITEM TEST_OPS test_multiprocess_reader_exception)
list(REMOVE_ITEM TEST_OPS test_trainer_desc)
Expand Down
59 changes: 24 additions & 35 deletions test/legacy_test/test_deprecated_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,20 @@
import numpy as np

import paddle
from paddle import _legacy_C_ops
from paddle.utils import deprecated

LOWEST_WARNING_POSTION = 3
ERROR_WARNING_POSTION = sys.maxsize

# custom paddle version
paddle.version.major = '1'
paddle.version.minor = '8'
paddle.version.major = '0'
paddle.version.minor = '0'
paddle.version.patch = '0'
paddle.version.rc = '0'
paddle.__version__ = '1.8.0'
paddle.version.full_version = '1.8.0'
paddle.__version__ = '0.0.0'
paddle.version.full_version = '0.0.0'
print("current paddle version: ", paddle.__version__)

paddle.disable_static()


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

doc_lst = api.__doc__.splitlines()
for idx, val in enumerate(doc_lst):
doc_list = api.__doc__.splitlines()
if len(doc_list) < 2:
return ERROR_WARNING_POSTION
for idx, (current_line, next_line) in enumerate(
zip(doc_list[:-1], doc_list[1:])
):
if (
val.startswith("Warning: ")
and val.endswith(" instead.")
and "and will be removed in future versions." in val
current_line == "Warning:"
and next_line.endswith(" instead.")
and "and will be removed in future versions." in next_line
):
return idx
return ERROR_WARNING_POSTION


class TestDeprecatedDocorator(unittest.TestCase):
class TestDeprecatedDecorator(unittest.TestCase):
"""
tests for paddle's Deprecated Docorator.
tests for paddle's deprecated decorator.
test_new_multiply: test for new api, which should not insert warning information.
test_ops_elementwise_mul: test for C++ elementwise_mul op, which should not insert warning information.
"""

def test_new_multiply(self):
Expand All @@ -87,26 +87,15 @@ def test_new_multiply(self):
# testting
self.assertLess(expected, captured)

def test_ops_elementwise_mul(self):
"""
Test for new C++ elementwise_op, expected result should be True,
because not matter what base.layers.elementwise_mul is deprecated.
"""

a = np.random.uniform(0.1, 1, [51, 76]).astype(np.float32)
b = np.random.uniform(0.1, 1, [51, 76]).astype(np.float32)
x = paddle.to_tensor(a)
y = paddle.to_tensor(b)
res = _legacy_C_ops.elementwise_mul(x, y)

# expected
expected = LOWEST_WARNING_POSTION

# captured
captured = get_warning_index(paddle.multiply)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是当时测试 fluid.layers.elementwise_mul 写的,2.0 之后就删除了api 映射表,这里的paddle.multipy 是当时清理 fluid #48748 的时候修改成这样的。当时的这个单测被暂时关闭,也就没有检测,这里就删除了


# testting
self.assertGreater(expected, captured)
def test_indent_level(self):
# test for different indent_level
dataset = paddle.base.DatasetFactory().create_dataset("InMemoryDataset")
with warnings.catch_warnings(record=True):
dataset.set_merge_by_lineid()
assert (
'\nSet merge by'
in paddle.base.InMemoryDataset.set_merge_by_lineid.__doc__
)

def test_tensor_gradient(self):
paddle.__version__ = '2.1.0'
Expand Down