philiprehberger-deprecate
Decorator and utilities for deprecating functions, parameters, and classes with zero boilerplate.
Installation
pip install philiprehberger-deprecate
Usage
from philiprehberger_deprecate import (
deprecated,
deprecated_attribute,
deprecated_class,
deprecated_module,
deprecated_param,
silenced,
)
Deprecate a function
@deprecated(remove_in="2.0.0", alternative="new_fetch")
def old_fetch(url: str) -> str:
...
Calling old_fetch() emits:
DeprecationWarning: Function 'old_fetch' is deprecated. Will be removed in 2.0.0. Use 'new_fetch' instead.
Deprecate a parameter
@deprecated_param("color", renamed_to="colour", remove_in="2.0.0")
def draw(shape: str, *, colour: str = "red") -> None:
...
draw(shape="circle", color="blue") # warns and forwards color -> colour
Deprecate a class
@deprecated_class(remove_in="3.0.0", alternative="NewClient")
class OldClient:
def __init__(self, host: str) -> None:
self.host = host
Instantiating OldClient() emits a DeprecationWarning.
Deprecate a module
# in mypkg/legacy/__init__.py
from philiprehberger_deprecate import deprecated_module
deprecated_module(__name__, remove_in="2.0.0", alternative="mypkg.modern")
Importing mypkg.legacy emits:
DeprecationWarning: Module 'mypkg.legacy' is deprecated. Will be removed in 2.0.0. Use 'mypkg.modern' instead.
Deprecating attributes
class Config:
old_value = deprecated_attribute(
"old_value",
since="1.0",
removed_in="2.0",
replacement="new_value",
)
def __init__(self) -> None:
self.new_value = 42
cfg = Config()
cfg.old_value # emits DeprecationWarning and returns 42 (from new_value)
Silencing during tests
from philiprehberger_deprecate import silenced
with silenced():
old_function() # no DeprecationWarning emitted inside the block
API
| Function | Description |
|---|---|
deprecated(remove_in, *, alternative, message) |
Function decorator that emits DeprecationWarning on each call |
deprecated_param(param, *, renamed_to, remove_in) |
Function decorator that warns when a deprecated parameter is passed and optionally maps it to its replacement |
deprecated_class(remove_in, *, alternative, message) |
Class decorator that emits DeprecationWarning on instantiation |
deprecated_module(name, *, remove_in, alternative, message) |
Emits DeprecationWarning once when a deprecated module is imported |
deprecated_attribute(name, since=None, removed_in=None, replacement=None) |
Descriptor that emits DeprecationWarning on attribute access; transparently returns the replacement attribute's value when set |
silenced() |
Context manager that suppresses DeprecationWarning from this package (useful in tests) |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
Release files for philiprehberger-deprecate 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| philiprehberger_deprecate-0.3.0.tar.gz | 193.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| philiprehberger_deprecate-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 199.0 kB
Release files / philiprehberger_deprecate-0.3.0.tar.gz
| Download URL | philiprehberger_deprecate-0.3.0.tar.gz |
|---|---|
| Size | 193.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d4b3306c2b66ab3e26e5e3fe83b628d49e76eb3160e2ce38cddf72867419a4d0
|
|
BLAKE2b-256 checksum How to use checksums |
280e7b4876c6ec4ed4b14f5b45556d3e7ac77ae17a06345d47b01162e92eefa8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / philiprehberger_deprecate-0.3.0-py3-none-any.whl
| Download URL | philiprehberger_deprecate-0.3.0-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3e3889432b53a418cf43b46317a33a574721d6831517feb44de68fb00084c65b
|
|
BLAKE2b-256 checksum How to use checksums |
b6162abb80948596090da42eaad6efe9227d4c35ac7c335a6cea713310087263
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|