Deprecat Decorator
The `deprecat` library provides a Python `@deprecat` decorator for marking classes, functions, and methods as deprecated. It also includes functionality to deprecate specific arguments within a function using `@deprecat.deprecated_args`. Maintained on GitHub, it's currently at version 2.1.3 and has a moderate release cadence, with the latest significant updates focusing on bug fixes and new features like `remove_version`.
Common errors
-
ModuleNotFoundError: No module named 'deprecated'
cause Attempting to import `deprecated` when `deprecat` is intended, or the wrong library is installed.fixEnsure you have installed `deprecat` (`pip install deprecat`) and are importing it correctly: `from deprecat import deprecat`. -
TypeError: 'NoneType' object is not callable
cause Attempting to use `@deprecat` without calling it (i.e., omitting `()` when no arguments are passed).fixAlways call the decorator, even if with empty parentheses: `@deprecat()`.
Warnings
- gotcha The `deprecat` library is distinct from the similarly named `deprecated` library. Ensure you import `from deprecat import deprecat` to use this library's specific features, particularly `deprecated_args`.
- gotcha When using `@deprecat` without any arguments (e.g., just marking something as deprecated without specifying a reason or version), you must still include the parentheses: `@deprecat()`. Omitting them (`@deprecat`) will cause the decorator to not apply correctly or raise a `TypeError`.
- gotcha The `deprecated_args` functionality is available as an attribute of the `deprecat` decorator (i.e., `deprecat.deprecated_args`). It is not directly importable as `from deprecat import deprecated_args`.
Install
-
pip install deprecat
Imports
- deprecat
from deprecated import deprecated
from deprecat import deprecat
Quickstart
from deprecat import deprecat
import warnings
@deprecat(reason="This function is old, use new_sum instead", version="2.0.0")
def old_sum(a, b):
return a + b
class MyClass:
@deprecat(reason="This method is outdated", version="2.1.0")
def old_method(self):
return "Hello from old method"
@deprecat(reason="This function's 'b' argument is deprecated", version='2.1.1')
@deprecat.deprecated_args(version='2.1.1', deprecated_argument='b')
def my_func(a, b):
return a * b
# Suppress deprecation warnings for cleaner output in quickstart
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
print(f"Old sum: {old_sum(1, 2)}")
obj = MyClass()
print(f"Old method output: {obj.old_method()}")
print(f"Function with deprecated arg: {my_func(3, b=4)}")