Подтвердить что ты не робот

Декораторы в стандартной библиотеке python (в частности, @deprecated)

Мне нужно пометить процедуры как устаревшие, но, видимо, нет стандартного декодера библиотеки для устаревания. Я знаю рецепты для него и модуль предупреждений, но мой вопрос: почему нет стандартного декодера библиотеки для этой (общей) задачи?

Дополнительный вопрос: существуют ли стандартные декораторы в стандартной библиотеке?

4b9b3361

Ответ 1

Вот фрагмент кода, модифицированный по сравнению с цитируемым Леандро:

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

Потому что в некоторых интерпретаторах первое раскрытое решение (без обработки фильтра) может привести к подавлению предупреждения.

Ответ 2

Вот еще одно решение:

Этот декоратор (фактически фабрика декораторов) позволяет вам отправлять причину сообщения. Также более полезно помочь разработчику диагностировать проблему, указав исходные имя файла и номер строки.

РЕДАКТИРОВАТЬ: этот код использует нулевую рекомендацию: заменить строку 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.6 🎉

Ответ 3

Я предполагаю, что причина в том, что код Python не может быть обработан статически (как это сделано для компиляторов С++), вы не можете получить предупреждение об использовании некоторых вещей, прежде чем использовать его. Я не думаю, что это хорошая идея спамить пользователя вашего script с кучей сообщений. "Предупреждение: этот разработчик этого script использует устаревший API".

Обновление:, но вы можете создать декоратор, который преобразует оригинальную функцию в другую. Новая функция будет отмечать/проверять переключатель, говорящий, что эта функция была вызвана уже и покажет сообщение только при включении переключателя в состояние. И/или при выходе он может распечатать список всех устаревших функций, используемых в программе.

Ответ 4

Как предложил мюон, для этого вы можете установить пакет 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/ для полной документации.

Ответ 5

classmethod(), staticmethod() и различные декораторы property являются важными. Там также один из contextlib и несколько других менее используемых.

Ответ 6

Вы можете создать файл утилит

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

Ответ 7

UPDATE: я думаю, что лучше, когда мы показываем 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.

Ответ 8

Дополнение этого ответа от Стивена Вацелларо:

Если вы используете 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/ для полной документации.