@@ -45,37 +45,53 @@ <h1 class="title">Module <code>pedantic.decorators.fn_deco_deprecated</code></h1
4545< h2 class ="section-title " id ="header-functions "> Functions</ h2 >
4646< dl >
4747< dt id ="pedantic.decorators.fn_deco_deprecated.deprecated "> < code class ="name flex ">
48- < span > def < span class ="ident "> deprecated</ span > </ span > (< span > func: Callable[..., ~ReturnType]) ‑> Callable[..., ~ReturnType]</ span >
48+ < span > def < span class ="ident "> deprecated</ span > </ span > (< span > func: Callable[..., ~ReturnType] | None = None, message: str = '' ) ‑> Callable[..., ~ReturnType] | Callable[[Callable[..., ~ReturnType]], Callable[..., ~ReturnType] ]</ span >
4949</ code > </ dt >
5050< dd >
5151< details class ="source ">
5252< summary >
5353< span > Expand source code</ span >
5454</ summary >
55- < pre > < code class ="python "> def deprecated(func: F) -> F:
55+ < pre > < code class ="python "> def deprecated(func: F | None = None, message: str = '' ) -> F | Callable[[F], F] :
5656 """
5757 Use this decorator to mark a function as deprecated. It will raise a warning when the function is called.
58+ You can specify an optional reason or message to display with the warning.
5859
5960 Example:
60-
6161 >>> @deprecated
6262 ... def my_function(a, b, c):
6363 ... pass
64- >>> my_function(5, 4, 3)
64+ >>> my_function(5, 4, 3) # doctest: +SKIP
65+ >>> @deprecated(message='Will be removed soon. Please use my_function_new_instead.')
66+ ... def my_function(a, b, c):
67+ ... pass
68+ >>> my_function(5, 4, 3) # doctest: +SKIP
6569 """
6670
67- @wraps(func)
68- def wrapper(*args: Any, **kwargs: Any) -> ReturnType:
69- _raise_warning(msg=f'Call to deprecated function {func.__qualname__}.', category=DeprecationWarning)
70- return func(*args, **kwargs)
71- return wrapper</ code > </ pre >
71+ def decorator(fun: F) -> F:
72+ @wraps(fun)
73+ def wrapper(*args: Any, **kwargs: Any) -> ReturnType:
74+ msg = f'Call to deprecated function {fun.__qualname__}.'
75+
76+ if message:
77+ msg += f'\nReason: {message}'
78+
79+ warnings.warn(message=msg, category=DeprecationWarning, stacklevel=2)
80+ return fun(*args, **kwargs)
81+ return wrapper
82+ return decorator if func is None else decorator(func)</ code > </ pre >
7283</ details >
73- < div class ="desc "> < p > Use this decorator to mark a function as deprecated. It will raise a warning when the function is called.</ p >
84+ < div class ="desc "> < p > Use this decorator to mark a function as deprecated. It will raise a warning when the function is called.
85+ You can specify an optional reason or message to display with the warning.</ p >
7486< p > Example:</ p >
7587< pre > < code class ="language-python-repl "> >>> @deprecated
7688... def my_function(a, b, c):
7789... pass
78- >>> my_function(5, 4, 3)
90+ >>> my_function(5, 4, 3) # doctest: +SKIP
91+ >>> @deprecated(message='Will be removed soon. Please use my_function_new_instead.')
92+ ... def my_function(a, b, c):
93+ ... pass
94+ >>> my_function(5, 4, 3) # doctest: +SKIP
7995</ code > </ pre > </ div >
8096</ dd >
8197</ dl >
0 commit comments