def _deprecated_func( func: Callable[[Any], Any]) -> Callable[[Any], Any]: """Decorates a function as deprecated. This decorator is supposed to be applied to the deprecated function. """ if func.__doc__ is None: func.__doc__ = "" note = _DEPRECATION_NOTE_TEMPLATE.format(d_ver=deprecated_version, r_ver=removed_version) if text is not None: note += _format_text(text) indent = _get_docstring_indent(func.__doc__) func.__doc__ = func.__doc__.strip() + textwrap.indent( note, indent) + indent # TODO(mamu): Annotate this correctly. @functools.wraps(func) def new_func(*args: Any, **kwargs: Any) -> Any: warnings.warn( "{} has been deprecated in v{}. " "This feature will be removed in v{}.".format( name if name is not None else func.__name__, deprecated_version, removed_version, ), FutureWarning, stacklevel=2, ) return func(*args, **kwargs) # type: ignore return new_func
def decorator(func: Callable[FP, FT]) -> Callable[FP, FT]: if func.__doc__ is None: func.__doc__ = "" note = _DEPRECATION_NOTE_TEMPLATE.format(d_ver=deprecated_version, r_ver=removed_version) if text is not None: note += _format_text(text) indent = _get_docstring_indent(func.__doc__) func.__doc__ = func.__doc__.strip() + textwrap.indent(note, indent) + indent @functools.wraps(func) def wrapper(*args: Any, **kwargs: Any) -> FT: """Decorates a function as deprecated. This decorator is supposed to be applied to the deprecated function. """ message = _DEPRECATION_WARNING_TEMPLATE.format( name=(name if name is not None else func.__name__), d_ver=deprecated_version, r_ver=removed_version, ) if text is not None: message += " " + text warnings.warn(message, FutureWarning, stacklevel=2) return func(*args, **kwargs) return wrapper
def wrapper(cls: CT) -> CT: """Decorates a class as deprecated. This decorator is supposed to be applied to the deprecated class. """ _original_init = getattr(cls, "__init__") _original_name = getattr(cls, "__name__") @functools.wraps(_original_init) def wrapped_init(self, *args, **kwargs) -> None: # type: ignore message = _DEPRECATION_WARNING_TEMPLATE.format( name=(name if name is not None else _original_name), d_ver=deprecated_version, r_ver=removed_version, ) if text is not None: message += " " + text warnings.warn( message, FutureWarning, stacklevel=2, ) _original_init(self, *args, **kwargs) setattr(cls, "__init__", wrapped_init) if cls.__doc__ is None: cls.__doc__ = "" note = _DEPRECATION_NOTE_TEMPLATE.format(d_ver=deprecated_version, r_ver=removed_version) if text is not None: note += _format_text(text) indent = _get_docstring_indent(cls.__doc__) cls.__doc__ = cls.__doc__.strip() + textwrap.indent( note, indent) + indent return cls
def _deprecated_class(cls: Any) -> Any: """Decorates a class as deprecated. This decorator is supposed to be applied to the deprecated class. """ _original_init = cls.__init__ @functools.wraps(_original_init) def wrapped_init(self, *args, **kwargs) -> None: # type: ignore warnings.warn( "{0} has been deprecated in v{1}. " "This feature will be removed in v{2}. " "See https://github.com/optuna/optuna/releases/tag/v{1}.". format( name if name is not None else cls.__name__, deprecated_version, removed_version, ), FutureWarning, stacklevel=2, ) _original_init(self, *args, **kwargs) cls.__init__ = wrapped_init if cls.__doc__ is None: cls.__doc__ = "" note = _DEPRECATION_NOTE_TEMPLATE.format(d_ver=deprecated_version, r_ver=removed_version) if text is not None: note += _format_text(text) indent = _get_docstring_indent(cls.__doc__) cls.__doc__ = cls.__doc__.strip() + textwrap.indent( note, indent) + indent return cls