未验证 提交 79f12595 编写于 作者: L Leo Chen 提交者: GitHub

Refine deprecated decorator (#26238)

* show deprecation warning once

* add some notes

* remove unused module
上级 f2b6d8d5
...@@ -19,6 +19,14 @@ import warnings ...@@ -19,6 +19,14 @@ import warnings
import functools import functools
import paddle import paddle
# NOTE(zhiqiu): Since python 3.2, DeprecationWarning is ignored by default,
# and since python 3.7, it is once again shown by default when triggered directly by code in __main__.
# See details: https://docs.python.org/3/library/warnings.html#default-warning-filter
# The following line set DeprecationWarning to show once, which is expected to work in python 3.2 -> 3.6
# However, doing this could introduce one samll side effect, i.e., the DeprecationWarning which is not issued by @deprecated.
# The side effect is acceptable, and we will find better way to do this if we could.
warnings.simplefilter('default', DeprecationWarning)
def deprecated(update_to="", since="", reason=""): def deprecated(update_to="", since="", reason=""):
"""Decorate a function to signify its deprecation. """Decorate a function to signify its deprecation.
...@@ -54,7 +62,7 @@ def deprecated(update_to="", since="", reason=""): ...@@ -54,7 +62,7 @@ def deprecated(update_to="", since="", reason=""):
"paddle." "paddle."
), 'Argument update_to must start with "paddle.", your value is "{}"'.format( ), 'Argument update_to must start with "paddle.", your value is "{}"'.format(
update_to) update_to)
msg += ' Use "{}" instead.'.format(_update_to) msg += ' Please use "{}" instead.'.format(_update_to)
if len(_reason) > 0: if len(_reason) > 0:
msg += "\n reason: {}".format(_reason) msg += "\n reason: {}".format(_reason)
...@@ -70,11 +78,8 @@ def deprecated(update_to="", since="", reason=""): ...@@ -70,11 +78,8 @@ def deprecated(update_to="", since="", reason=""):
v_since = [int(i) for i in _since.split(".")] v_since = [int(i) for i in _since.split(".")]
v_since += [0] * (4 - len(v_since)) v_since += [0] * (4 - len(v_since))
if paddle.__version__ == "0.0.0" or _since == "" or v_current >= v_since: if paddle.__version__ == "0.0.0" or _since == "" or v_current >= v_since:
warnings.simplefilter('always',
DeprecationWarning) # turn off filter
warnings.warn(msg, category=DeprecationWarning, stacklevel=2) warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
warnings.simplefilter('default',
DeprecationWarning) # reset filter
return func(*args, **kwargs) return func(*args, **kwargs)
return wrapper return wrapper
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册