ルーチンを非推奨としてマークする必要がありますが、非推奨の標準ライブラリデコレータがないようです。私はそれと警告モジュールのレシピを知っていますが、私の質問は、この(一般的な)タスク用の標準ライブラリデコレータがないのはなぜですか?
追加の質問:標準ライブラリに標準のデコレータはありますか?
ルーチンを非推奨としてマークする必要がありますが、非推奨の標準ライブラリデコレータがないようです。私はそれと警告モジュールのレシピを知っていますが、私の質問は、この(一般的な)タスク用の標準ライブラリデコレータがないのはなぜですか?
追加の質問:標準ライブラリに標準のデコレータはありますか?
別の解決策は次のとおりです。
このデコレータ (実際にはデコレータ ファクトリ) を使用すると、理由メッセージを表示できます。また、ソースファイル名と行番号を提供することで、開発者が問題を診断できるようにすることもより便利です。
EDIT : このコードは、Zero の推奨事項を使用しています。warnings.warn_explicit
行をで置き換えます。これによりwarnings.warn(msg, category=DeprecationWarning, stacklevel=2)
、関数定義サイトではなく関数呼び出しサイトが出力されます。デバッグが容易になります。
EDIT2 : このバージョンでは、開発者はオプションの「理由」メッセージを指定できます。
import functools
import inspect
import warnings
string_types = (type(b''), type(u''))
def deprecated(reason):
"""
This is a decorator which can be used to mark functions
as deprecated. It will result in a warning being emitted
when the function is used.
"""
if isinstance(reason, string_types):
# The @deprecated is used with a 'reason'.
#
# .. code-block:: python
#
# @deprecated("please, use another function")
# def old_function(x, y):
# pass
def decorator(func1):
if inspect.isclass(func1):
fmt1 = "Call to deprecated class {name} ({reason})."
else:
fmt1 = "Call to deprecated function {name} ({reason})."
@functools.wraps(func1)
def new_func1(*args, **kwargs):
warnings.simplefilter('always', DeprecationWarning)
warnings.warn(
fmt1.format(name=func1.__name__, reason=reason),
category=DeprecationWarning,
stacklevel=2
)
warnings.simplefilter('default', DeprecationWarning)
return func1(*args, **kwargs)
return new_func1
return decorator
elif inspect.isclass(reason) or inspect.isfunction(reason):
# The @deprecated is used without any 'reason'.
#
# .. code-block:: python
#
# @deprecated
# def old_function(x, y):
# pass
func2 = reason
if inspect.isclass(func2):
fmt2 = "Call to deprecated class {name}."
else:
fmt2 = "Call to deprecated function {name}."
@functools.wraps(func2)
def new_func2(*args, **kwargs):
warnings.simplefilter('always', DeprecationWarning)
warnings.warn(
fmt2.format(name=func2.__name__),
category=DeprecationWarning,
stacklevel=2
)
warnings.simplefilter('default', DeprecationWarning)
return func2(*args, **kwargs)
return new_func2
else:
raise TypeError(repr(type(reason)))
このデコレーターは、関数、メソッド、およびクラスに使用できます。
簡単な例を次に示します。
@deprecated("use another function")
def some_old_function(x, y):
return x + y
class SomeClass(object):
@deprecated("use another method")
def some_old_method(self, x, y):
return x + y
@deprecated("use another class")
class SomeOldClass(object):
pass
some_old_function(5, 3)
SomeClass().some_old_method(8, 9)
SomeOldClass()
あなたは得るでしょう:
deprecated_example.py:59: DeprecationWarning: Call to deprecated function or method some_old_function (use another function).
some_old_function(5, 3)
deprecated_example.py:60: DeprecationWarning: Call to deprecated function or method some_old_method (use another method).
SomeClass().some_old_method(8, 9)
deprecated_example.py:61: DeprecationWarning: Call to deprecated class SomeOldClass (use another class).
SomeOldClass()
EDIT3:このデコレータは非推奨ライブラリの一部になりました:
新しい安定リリース v1.2.13
その理由は、Python コードを静的に処理できない (C++ コンパイラの場合のように) ためだと思います。実際に使用する前に、いくつかの使用に関する警告を受け取ることができません。スクリプトのユーザーに「警告: このスクリプトの開発者は非推奨の API を使用しています」というメッセージを大量に送信するのは良い考えではないと思います。
更新:ただし、元の関数を別の関数に変換するデコレータを作成できます。新しい関数は、この関数が既に呼び出されたことを示すスイッチをマーク/チェックし、スイッチをオン状態にしたときにのみメッセージを表示します。および/または終了時に、プログラムで使用されているすべての非推奨関数のリストを出力する場合があります。
更新:コード行ごとに DeprecationWarning を初めて表示し、メッセージを送信できる場合は、より良いと思います。
import inspect
import traceback
import warnings
import functools
import time
def deprecated(message: str = ''):
"""
This is a decorator which can be used to mark functions
as deprecated. It will result in a warning being emitted
when the function is used first time and filter is set for show DeprecationWarning.
"""
def decorator_wrapper(func):
@functools.wraps(func)
def function_wrapper(*args, **kwargs):
current_call_source = '|'.join(traceback.format_stack(inspect.currentframe()))
if current_call_source not in function_wrapper.last_call_source:
warnings.warn("Function {} is now deprecated! {}".format(func.__name__, message),
category=DeprecationWarning, stacklevel=2)
function_wrapper.last_call_source.add(current_call_source)
return func(*args, **kwargs)
function_wrapper.last_call_source = set()
return function_wrapper
return decorator_wrapper
@deprecated('You must use my_func2!')
def my_func():
time.sleep(.1)
print('aaa')
time.sleep(.1)
def my_func2():
print('bbb')
warnings.simplefilter('always', DeprecationWarning) # turn off filter
print('before cycle')
for i in range(5):
my_func()
print('after cycle')
my_func()
my_func()
my_func()
結果:
before cycle
C:/Users/adr-0/OneDrive/Projects/Python/test/unit1.py:45: DeprecationWarning: Function my_func is now deprecated! You must use my_func2!
aaa
aaa
aaa
aaa
aaa
after cycle
C:/Users/adr-0/OneDrive/Projects/Python/test/unit1.py:47: DeprecationWarning: Function my_func is now deprecated! You must use my_func2!
aaa
C:/Users/adr-0/OneDrive/Projects/Python/test/unit1.py:48: DeprecationWarning: Function my_func is now deprecated! You must use my_func2!
aaa
C:/Users/adr-0/OneDrive/Projects/Python/test/unit1.py:49: DeprecationWarning: Function my_func is now deprecated! You must use my_func2!
aaa
Process finished with exit code 0
警告パスをクリックするだけで、PyCharm の行に移動できます。