Python標準ライブラリのデコレータ(特に@非推奨)


127

ルーチンに非推奨のマークを付ける必要がありますが、非推奨の標準ライブラリデコレータはありません。私はそれと警告モジュールのレシピを知っていますが、私の質問は次のとおりです:なぜこの(一般的な)タスクに標準のライブラリデコレータがないのですか?

追加の質問:標準ライブラリに標準デコレータはありますか?


13
現在、非推奨パッケージがあります
muon

11
私はそれを行う方法を理解していますが、なぜそれがstd libにないのか(私がOPのケースであると想定しているため)の洞察を得るためにここに来て、実際の質問に対する良い答えが表示されません
SwimBikeRun

4
質問が頻繁に発生して、質問に答えようとさえしない多数の回答を得て、「レシピを知っている」などのことを積極的に無視するのはなぜですか?めちゃくちゃです!
Catskul、

1
@Catskul偽のインターネットポイントのため。
Stefano

1
推奨のライブラリを使用できます。
Laurent LAPORTE

回答:


59

Leandroによって引用されたものから変更されたスニペットを以下に示します。

import warnings
import functools

def deprecated(func):
    """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."""
    @functools.wraps(func)
    def new_func(*args, **kwargs):
        warnings.simplefilter('always', DeprecationWarning)  # turn off filter
        warnings.warn("Call to deprecated function {}.".format(func.__name__),
                      category=DeprecationWarning,
                      stacklevel=2)
        warnings.simplefilter('default', DeprecationWarning)  # reset filter
        return func(*args, **kwargs)
    return new_func

# Examples

@deprecated
def some_old_function(x, y):
    return x + y

class SomeClass:
    @deprecated
    def some_old_method(self, x, y):
        return x + y

一部のインタープリターでは、最初に公開されたソリューション(フィルター処理なし)が警告の抑制につながる可能性があるためです。


14
functools.wrapsそのように名前とドキュメントを設定するのではなく、なぜ使用しないのですか?
Maximilian、

1
@Maximilian:これを追加するために編集され、このコードの将来のコピーペーストが誤って実行するのを防ぐ
Eric

17
副作用(フィルターのオン/オフ)は好きではありません。これを決めるのはデコレータの仕事ではありません。
Kentzo 2017

1
フィルターをオンまたはオフにすると、バグ
gerrit

4
実際の質問には答えません。
Catskul、

44

ここに別の解決策があります:

このデコレータ(実際にはデコレータファクトリ)を使用すると、理由メッセージを提供できます。また、ソースファイル名行番号を指定することにより、開発者が問題を診断するのに役立つことも役立ちます

編集:このコードはゼロの推奨事項を使用します。これは、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.10🎉


6
うまくいきます- 関数定義サイトではなく、関数呼び出しサイトを出力するwarn_explicit行を置き換えることを好みwarnings.warn(msg, category=DeprecationWarning, stacklevel=2)ます。デバッグが容易になります。
ゼロ

こんにちは、GPLv3ライセンスライブラリでコードスニペットを使用したいと思います。私が合法的にそうすることができるように、GPLv3 またはより寛容なライセンスの下でコードを再ライセンスしてもよろしいですか?
gerrit


1
@LaurentLAPORTE知っています。CC-BY-SOは、GPLv3内での使用を許可していません(共有のようなビットのため)。このため、GPL互換のライセンスの下でこのコードを追加でリリースしてもよいかどうかを尋ねています。それ以外の場合は問題ありません。コードは使用しません。
gerrit

2
実際の質問には答えません。
Catskul、

15

muonが提案したようにdeprecationこのためのパッケージをインストールできます。

deprecationライブラリが提供deprecatedデコレータとfail_if_not_removedあなたのテストのためにデコレータを。

取り付け

pip install deprecation

使用例

import deprecation

@deprecation.deprecated(deprecated_in="1.0", removed_in="2.0",
                        current_version=__version__,
                        details="Use the bar function instead")
def foo():
    """Do some stuff"""
    return 1

完全なドキュメントについては、http://deprecation.readthedocs.io/を参照してください。


4
実際の質問には答えません。
Catskul、

1
PyCharmはこれを認識しないことに注意してください
cz

11

その理由は、Pythonコードは静的に処理できないため(C ++コンパイラーの場合のように)、実際に使用する前に、いくつかの使用に関する警告を受け取ることができないためです。「警告:このスクリプトの開発者は非推奨のAPIを使用しています」という大量のメッセージでスクリプトのユーザーにスパムを送信することは良い考えではないと思います。

更新:ただし、元の機能を別の機能に変換するデコレータを作成できます。新しい関数はスイッチをマーク/チェックして、この関数がすでに呼び出されたことを通知し、スイッチをオン状態にしたときにのみメッセージを表示します。または終了時に、プログラムで使用されているすべての非推奨関数のリストを出力する場合があります。


3
そして、関数がモジュールからインポートされたときに非推奨を示すことができるはずです。デコレータはそのための適切なツールです。
Janusz Lenar 2013

@JanuszLenar、非推奨の関数を使用しなくても警告が表示されます。しかし、私はいくつかのヒントで私の答えを更新できると思います。
13

8

utilsファイルを作成できます

import warnings

def deprecated(message):
  def deprecated_decorator(func):
      def deprecated_func(*args, **kwargs):
          warnings.warn("{} is a deprecated function. {}".format(func.__name__, message),
                        category=DeprecationWarning,
                        stacklevel=2)
          warnings.simplefilter('default', DeprecationWarning)
          return func(*args, **kwargs)
      return deprecated_func
  return deprecated_decorator

次に、次のように非推奨のデコレータをインポートします。

from .utils import deprecated

@deprecated("Use method yyy instead")
def some_method()"
 pass

おかげで、廃止予定のメッセージを表示するだけでなく、これを使用してユーザーを適切な場所に誘導しています!
ドイツのアタナシオ2018

3
実際の質問には答えません。
Catskul、

2

更新:各コード行で初めて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の行に移動できます。


2
実際の質問には答えません。
Catskul、

0

Steven Vascellaroによるこの回答の拡張:

Anacondaを使用する場合は、最初にdeprecationパッケージをインストールします。

conda install -c conda-forge deprecation 

次に、ファイルの上部に以下を貼り付けます

import deprecation

@deprecation.deprecated(deprecated_in="1.0", removed_in="2.0",
                    current_version=__version__,
                    details="Use the bar function instead")
def foo():
    """Do some stuff"""
    return 1

完全なドキュメントについては、http://deprecation.readthedocs.io/を参照してください。


4
実際の質問には答えません。
Catskul、
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.