Typing Stubs for Deprecated
This is a typeshed stub package that provides static type annotations for the 'Deprecated' Python library. It enables type checkers like Mypy and Pyright to analyze code using the `Deprecated` library, which offers a `@deprecated` decorator for marking functions, methods, or classes as deprecated and emitting runtime warnings. The `types-deprecated` package is part of the `typeshed` project and its release cadence is tied to typeshed updates, aiming for compatibility with `Deprecated~=1.3.1`.
Warnings
- gotcha This `types-deprecated` package provides *only* type stubs. For the actual `@deprecated` decorator functionality at runtime, you must also install the `Deprecated` library (`pip install Deprecated`).
- gotcha By default, `DeprecationWarning` messages are ignored in Python 2.7+ unless explicitly enabled (e.g., via `python -W always` or `warnings.simplefilter('always', DeprecationWarning)` in code). If you don't see warnings, they might be silently suppressed.
- gotcha When deprecating a class using `@deprecated`, the warning is typically emitted only during class instantiation (when `__new__` is called), not for every method call on an already-instantiated deprecated object.
- breaking The `types-deprecated` stubs aim to provide accurate annotations for `Deprecated~=1.3.1`. Future major versions of the runtime `Deprecated` library (e.g., 2.x) may introduce API changes that could break type checking, even if runtime code continues to function with older versions of the library.
Install
-
pip install types-deprecated -
pip install Deprecated
Imports
- deprecated
from deprecated import deprecated
- deprecated_params
from deprecated.params import deprecated_params
- DeprecatedMethod
from deprecated import DeprecatedMethod
-
Quickstart
import warnings
from deprecated import deprecated
# Enable DeprecationWarnings to be visible (they are often ignored by default)
warnings.simplefilter('always', DeprecationWarning)
@deprecated(reason="Use new_function instead")
def old_function(a: int, b: int) -> int:
"""This is an old function."""
return a + b
@deprecated(version='1.2.0', reason="Use NewClass instead")
class OldClass:
def __init__(self, value: str):
self.value = value
def get_value(self) -> str:
return self.value
def new_function(x: int, y: int) -> int:
"""This is the new function."""
return x * y
# Using the deprecated function and class
result = old_function(5, 3)
print(f"Result from old_function: {result}")
obj = OldClass("hello")
print(f"Value from OldClass: {obj.get_value()}")
# Expected usage of the new function
new_result = new_function(5, 3)
print(f"Result from new_function: {new_result}")
# Example of using a deprecated parameter (requires deprecated.params)
from deprecated.params import deprecated_params
@deprecated_params('old_arg', reason='Use new_arg instead')
def example_function(new_arg: str, old_arg: str = 'default') -> None:
print(f"Called with new_arg: {new_arg}, old_arg: {old_arg}")
example_function(new_arg='modern value', old_arg='legacy value')
# Restore default warning filter (optional)
warnings.simplefilter('default', DeprecationWarning)