neuralqx.utils.deprecation module¶
This file contains logic for different decorators responsible for different deprecation warnings
- NOTE: part(s) of, or the entire content, of this file is obtained from NetKet’s source code
the original copyright mentioned above applies.
- deprecated(reason=None, func_name=None)¶
Decorator to mark a function or class as deprecated.
When applied, calling the decorated object will emit a
FutureWarningindicating that the function/class is deprecated and will be removed in a future release. An optionalreasoncan be included to provide migration notes, and an optionalfunc_namecan be supplied to override the name shown in the warning (useful for aliases).- Parameters:
- Returns:
A decorator that wraps the target callable/class and emits a deprecation warning on use.
- deprecation_warning(depr_message)¶
Emit a
FutureWarningwith a provided deprecation message.The message is passed through
textwrap.dedent()and emitted withstacklevel=2so the warning points to the user’s call site.
- old_name_deprecation(function_name, reason='')¶
Decorator factory to deprecate an old function name in favour of a new one.
When applied, calling the decorated function emits a
FutureWarninginforming the user thatfunc.__name__has been renamed tofunction_nameand that the old name will be removed in a future release. An optionalreasonmay be appended with additional context.
- parameter_name_deprecation(old_param, new_param, reason='')¶
Decorator factory to deprecate a keyword parameter name in favour of a new one.
When applied, calling the decorated function will emit a
FutureWarningif the user suppliesold_paramas a keyword argument, indicating that it will be renamed tonew_paramin a future release. The function call proceeds unchanged (the keyword is not automatically renamed).- Parameters:
- Returns:
A decorator that wraps the function and emits a parameter rename warning when
old_paramis used.
- deprecated_class(reason=None, class_name=None, *, category=<class 'FutureWarning'>, warn_on_subclasses=False, stacklevel=2)¶
Decorator to mark a class as deprecated.
Unlike function-style deprecation decorators, this decorator preserves the class object (so
isinstance, subclassing, and introspection continue to work) and emits a warning when the class is instantiated.The warning is emitted from a wrapped
__init__method.- Parameters:
reason (
str|None) – Optional additional migration notes (e.g. replacement class or API changes).class_name (
str|None) – Optional name to display in the warning instead ofcls.__name__. Useful when deprecating an alias/shim class.category (
type[Warning]) – Warning category to emit. Defaults toFutureWarning.warn_on_subclasses (
bool) – IfFalse(default), warn only when the deprecated class itself is instantiated. IfTrue, also warn when subclasses (that inherit the wrapped__init__) are instantiated.stacklevel (
int) – Stacklevel passed towarnings.warn(). Default2is usually correct.
- Return type:
Callable[[TypeVar(T, bound=type)],TypeVar(T, bound=type)]- Returns:
Callable[[type], type] A class decorator that wraps
__init__and returns the original class object.
Notes
This decorator modifies
cls.__init__in place.It is safe for normal classes and dataclasses.
It is not intended for deprecating a plain alias assignment like
OldClass = NewClassbecause aliases provide no call hook. For aliases, create a shim class and decorate that shim.
- deprecate_public_api(namespace, exports=None, *, module_name=None, reason=None, warn_on_module_import=False, internal_module_prefixes=(), module_warning_stacklevel=5)¶
Deprecates an entire public API namespace, typically a package or module
__init__.This utility wraps exported functions and classes with deprecation warnings. It can also optionally emit a module-level deprecation warning at import time.
- Parameters:
namespace (
MutableMapping[str,Any]) – Module globals dictionary, usuallyglobals().exports (
Optional[Iterable[str]]) – Public names to deprecate. Defaults tonamespace["__all__"].module_name (
str|None) – Fully qualified module name used in warning labels. Defaults tonamespace["__name__"].reason (
Union[str,Callable[[str],str],None]) – Additional migration guidance. Can be either a static string or a callable that receives the fully qualified target name and returns a message.warn_on_module_import (
bool) – Whether to emit a module-level deprecation warning at import time.internal_module_prefixes (
tuple[str,...]) – Optional module-name prefixes for internal call sites where class instantiation should not emit deprecation warnings.module_warning_stacklevel (
int) – Stack level used for module-level import warnings. Defaults to5so warnings typically point to the import call site rather than inside deprecation internals.
- Return type: