Decorator and utilities for deprecating functions, parameters, and classes with zero boilerplate
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_deprecate-0.3.0.tar.gz.
File metadata
- Download URL: philiprehberger_deprecate-0.3.0.tar.gz
- Upload date:
- Size: 193.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4b3306c2b66ab3e26e5e3fe83b628d49e76eb3160e2ce38cddf72867419a4d0
|
|
| MD5 |
34e13a372e091248db98bd4cded85637
|
|
| BLAKE2b-256 |
280e7b4876c6ec4ed4b14f5b45556d3e7ac77ae17a06345d47b01162e92eefa8
|
File details
Details for the file philiprehberger_deprecate-0.3.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_deprecate-0.3.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e3889432b53a418cf43b46317a33a574721d6831517feb44de68fb00084c65b
|
|
| MD5 |
0f7f80047dbdb1a302163f0773bf5e6c
|
|
| BLAKE2b-256 |
b6162abb80948596090da42eaad6efe9227d4c35ac7c335a6cea713310087263
|