Deprecate Python APIs without breaking existing callers.
Use pyDeprecate when you rename a function, move a class, or retire an argument while existing user code must keep working. A single decorator warns callers, forwards calls, and remaps arguments during the migration window. CI audit tools expose deprecations that have reached their removal deadline.
The documentation site owns the full guides and API reference. This README covers installation, a quick start, the everyday patterns, warning controls, audits, testing and docstring notices; each section links to the guide with the details.
📖 Overview
Keep the old entry point temporarily, tell users what changed, and forward calls to its replacement. Remove the deprecated entry point after callers have migrated.
The usual migration has four steps:
Publish the replacement API.
Deprecate the old entry point with introduction and removal versions.
Audit deprecated wrappers in tests, pre-commit, or CI.
Remove the old entry point when its migration window closes.
The CLI itself uses only the standard library; [audit] adds packaging for the version-aware commands. See installation details for the full package and extras reference.
🚀 Quick Start
Forward an existing function to its replacement with @deprecated(target=...):
from deprecate import deprecated, void
# NEW API — the replacement computes the sum
def compute_sum(a: int, b: int) -> int:
return a + b
# DEPRECATED API — `addition` now forwards to `compute_sum`; its own body never runs
@deprecated(target=compute_sum, deprecated_in="1.0", remove_in="2.0")
def addition(a: int, b: int) -> int:
return void(a, b)
print(addition(1, 2)) # warns: FutureWarning
Output: addition(1, 2)
3
The result comes from compute_sum, because the deprecated body only returns None. Existing callers see this warning once, and new callers should call compute_sumdirectly:
FutureWarning: The `addition` was deprecated since v1.0 in favor of `__main__.compute_sum`. It will be removed in v2.0.
Warning — forwarding skips the old body. With a callable target, the deprecated function body does not run. If skip_if evaluates to True, the source body runs instead, so keep a working fallback when combining these options.
A hand-written warnings.warn shim covers the first row only. pyDeprecate covers the rest of the migration from the same decorator:
Migration need
Hand-written warnings.warn
pyDeprecate
Warn old callers
Write the message and stacklevel by hand
Generated message with both versions
Forward calls to the replacement
Write a shim per function
target=new_function
Rename or drop arguments
Translate keyword arguments by hand
args_mapping
Keep old class, constant, and module names
Subclass or write a module __getattr__
Transparent proxies
Warn once instead of on every call
Track call state yourself
num_warns budget per wrapper
Fail CI once the removal version is reached
Not available
pydeprecate expiry
Use warnings.deprecated (PEP 702, Python 3.13+) or typing_extensions.deprecated when you only need a type-checker signal; stack it above @deprecated with category=None, so callers get the type-checker signal and only one runtime warning. See the Python deprecation tools comparison for deprecation, Deprecated and other libraries.
📚 Use-cases and Applications
Each example below is a complete basic setup you can paste and run. The use-case guide helps you choose a pattern for other cases.
🔄 Rename an argument
Use TargetMode.ARGS_REMAP when the function stays in place but one of its arguments has a new name:
from deprecate import TargetMode, deprecated
# NEW API — `factor` is the current argument name
# DEPRECATED API — callers passing `scale` are remapped to `factor`
@deprecated(target=TargetMode.ARGS_REMAP, args_mapping={"scale": "factor"}, deprecated_in="0.2", remove_in="0.4")
def resize_width(width: int, scale: float = 1.0, factor: float = 1.0) -> int:
return int(width * factor)
print(resize_width(100, scale=2)) # warns: FutureWarning
Output: resize_width(100, scale=2)
200
FutureWarning: The `resize_width` uses deprecated arguments: `scale` -> `factor`. They were deprecated since v0.2 and will be removed in v0.4.
🔀 Move a function and rename its arguments
Combine a callable target with args_mapping when the replacement also renamed or dropped arguments. Map an old name to None to drop it before forwarding:
from deprecate import deprecated, void
# NEW API — `max_rows` replaces `limit`, and the `verbose` flag is gone
def fetch_rows(table: str, max_rows: int = 10) -> list[str]:
return [f"{table}-{i}" for i in range(max_rows)]
# DEPRECATED API — `load_rows` forwards to `fetch_rows`, renaming `limit` and dropping `verbose`
@deprecated(
target=fetch_rows,
args_mapping={"limit": "max_rows", "verbose": None},
deprecated_in="1.3",
remove_in="2.0",
)
def load_rows(table: str, limit: int = 10, verbose: bool = False) -> list[str]:
return void(table, limit, verbose)
print(load_rows("users", limit=2, verbose=True)) # warns: FutureWarning
Output: load_rows("users", limit=2, verbose=True)
['users-0', 'users-1']
FutureWarning: The `load_rows` was deprecated since v1.3 in favor of `__main__.fetch_rows`. It will be removed in v2.0.
⚠ Warn without changing behavior
Use TargetMode.NOTIFY when the function keeps its body and only needs a removal notice:
from deprecate import TargetMode, deprecated
# DEPRECATED API — `order_total` still runs its own body until v0.5
@deprecated(target=TargetMode.NOTIFY, deprecated_in="0.1", remove_in="0.5")
def order_total(prices: list[float]) -> float:
return sum(prices)
print(order_total([1.5, 2.5])) # warns: FutureWarning
Output: order_total([1.5, 2.5])
4.0
FutureWarning: The `order_total` was deprecated since v0.1. It will be removed in v0.5.
🧰 Deprecate a method or a property
The same decorator works on methods, class methods, static methods and properties. Put @deprecated above @property (the outer order); the property warns on read. The inner order @property @deprecated fails pydeprecate check:
from deprecate import TargetMode, deprecated
class Invoice:
def __init__(self, amount: float) -> None:
self.amount = amount
# NEW API — the total including tax
def total(self) -> float:
return self.amount * 1.2
# DEPRECATED API — `gross` still runs its body, which calls `total`
@deprecated(target=TargetMode.NOTIFY, deprecated_in="1.1", remove_in="2.0")
def gross(self) -> float:
return self.total()
# DEPRECATED API — read `amount` instead
@deprecated(target=TargetMode.NOTIFY, deprecated_in="1.1", remove_in="2.0")
@property
def value(self) -> float:
return self.amount
invoice = Invoice(10.0)
print(invoice.gross()) # warns: FutureWarning
print(invoice.value) # warns: FutureWarning
Output: invoice.gross(); invoice.value
12.0
10.0
FutureWarning: The `gross` was deprecated since v1.1. It will be removed in v2.0.
FutureWarning: The `value` was deprecated since v1.1. It will be removed in v2.0.
A property with a setter or deleter needs @deprecatedabove@property so that every accessor warns. See properties and class and static methods.
⏳ Deprecate an async function
async def functions take the same decorator. The warning fires when the coroutine runs, and the call forwards to an async replacement:
import asyncio
from deprecate import deprecated, void
# NEW API — the renamed coroutine
async def fetch_invoice(invoice_id: int) -> str:
return f"invoice-{invoice_id}"
# DEPRECATED API — `get_invoice` forwards to `fetch_invoice`
@deprecated(target=fetch_invoice, deprecated_in="1.4", remove_in="2.0")
async def get_invoice(invoice_id: int) -> str:
return void(invoice_id)
print(asyncio.run(get_invoice(7))) # warns: FutureWarning
Output: asyncio.run(get_invoice(7))
invoice-7
Generators and async generators work the same way: keep yield in the deprecated stub, and the warning fires on the first iteration. See async functions and generators.
🏗 Rename a class
Use deprecated_class to keep an old class name working while construction goes to the new class:
from deprecate import deprecated_class
# NEW API — the renamed writer
class ReportWriter:
def __init__(self, path: str = "") -> None:
self.path = path
# DEPRECATED API — `CsvReportWriter` forwards to `ReportWriter`
@deprecated_class(target=ReportWriter, deprecated_in="1.2", remove_in="2.0")
class CsvReportWriter:
def __init__(self, path: str = "") -> None:
self.path = path
writer = CsvReportWriter("out.csv") # warns: FutureWarning
print(type(writer).__name__, writer.path)
Output: type(writer).__name__, writer.path
ReportWriter out.csv
FutureWarning: The `CsvReportWriter` was deprecated since v1.2 in favor of `__main__.ReportWriter`. It will be removed in v2.0.
🏷 Rename a class attribute
Use attrs_mapping when the class keeps its name but an attribute is renamed. Only the listed names warn; the others pass through silently:
from deprecate import deprecated_class
# NEW API — `colour` is the canonical spelling
# DEPRECATED API — `color` redirects to `colour`
@deprecated_class(attrs_mapping={"color": "colour"}, deprecated_in="2.0", remove_in="3.0")
class Palette:
colour: str = "red"
size: int = 10
print(Palette.color) # warns: FutureWarning
print(Palette.size) # no warning
Output: Palette.color; Palette.size
red
10
FutureWarning: The `color` was deprecated since v2.0 in favor of `Palette.colour`. It will be removed in v3.0.
Reads, writes and deletes all redirect. Map a name to None to warn without renaming. See selective attributes for Enum members and dataclass fields.
📦 Rename a constant
Use deprecated_instance to keep an old module-level name for a dict, list, constant, or object:
from deprecate import deprecated_instance
# NEW API — renamed to say what it configures
TRAINING_CONFIG = {"lr": 0.001, "batch_size": 32}
# DEPRECATED API — `DEFAULTS` was the original name; `read_only=True` refuses writes through the old name
DEFAULTS = deprecated_instance(TRAINING_CONFIG, name="DEFAULTS", deprecated_in="1.2", remove_in="2.0", read_only=True)
print(DEFAULTS["lr"]) # warns: FutureWarning
Output: DEFAULTS["lr"]
0.001
FutureWarning: The `DEFAULTS` was deprecated since v1.2. It will be removed in v2.0.
📁 Deprecate a module
Call deprecated_moduleonce at the bottom of the old module file. Every public attribute access on the old module then warns, on each access by default:
# phmdoctest:skip — template: the last lines of old_calculator.py; new_calculator is your replacement module
# DEPRECATED API — old_calculator.py; its functions now live in new_calculator.py
import new_calculator
from deprecate import deprecated_module
deprecated_module(__name__, target=new_calculator, deprecated_in="2.0", remove_in="3.0")
Names missing from old_calculator are forwarded to new_calculator, so old_calculator.add(1, 2) keeps working with a FutureWarning. Leave out target to keep the module in place and only warn. See module deprecation for attribute renames during the redirect.
➕ More patterns
Stacked decorators — rename arguments across several releases (guide).
Fatal deprecations — raise DeprecatedError instead of warning, with as_exception=True on one wrapper, with deprecate.as_exceptions(): for one block, or DEPRECATE_AS_EXCEPTIONS=1 for the whole process (guide).
Strict callable-only deprecation — reject class sources with deprecated_callable (guide).
Enums and dataclasses — forward class access and construction (guide).
New required arguments — inject fixed forwarded kwargs (guide).
The void() helper — write deprecated stubs (guide).
For existing installations, read the migration guide before upgrading.
🎛 Control the Warnings
Every decorator and proxy takes the same options for where its notice goes, how often it fires, what it says, and when it applies.
📣 Output, budget and message
Send the notice to a logger with stream, repeat it on every call with num_warns=-1, and replace the default text with message_template:
import logging
from deprecate import deprecated, void
# NEW API — the renamed helper
def normalize(text: str) -> str:
return text.strip().lower()
# DEPRECATED API — `clean` forwards to `normalize` and logs each call
@deprecated(
target=normalize,
deprecated_in="1.0",
remove_in="2.0",
stream=logging.warning,
num_warns=-1,
message_template="`%(source_name)s` is deprecated, call `%(target_name)s` instead.",
)
def clean(text: str) -> str:
return void(text)
print(clean(" Hello ")) # logs: WARNING
print(clean(" World")) # logs: WARNING again
Output: clean(" Hello "); clean(" World")
hello
world
WARNING:root:`clean` is deprecated, call `normalize` instead.
WARNING:root:`clean` is deprecated, call `normalize` instead.
stream accepts any callable that takes the message, such as print or your own function. stream=None silences the notice; deprecated_module still warns with it until v1.0.
num_warns defaults to 1, so each wrapper warns once per process. A positive number sets the budget and -1 warns on every call.
message_template always accepts %(source_name)s, %(deprecated_in)s and %(remove_in)s. With a callable target it also accepts %(target_name)s and %(target_path)s.
See customization for every placeholder and stream recipe.
🔀 Apply a deprecation conditionally
skip_if takes a boolean or a zero-argument callable. While it returns True, the wrapper neither warns nor forwards, and the source body runs:
from deprecate import TargetMode, deprecated
LEGACY_EXPORT = True
# DEPRECATED API — `export_csv` warns only once the legacy export flag is off
@deprecated(target=TargetMode.NOTIFY, deprecated_in="1.0", remove_in="2.0", skip_if=lambda: LEGACY_EXPORT)
def export_csv(rows: list[int]) -> int:
return len(rows)
print(export_csv([1, 2])) # no warning while LEGACY_EXPORT is True
LEGACY_EXPORT = False
print(export_csv([1, 2])) # warns: FutureWarning
Output: export_csv([1, 2]) before and after the switch
Use deprecated_in and remove_in to define the migration window. A callable target identifies the replacement in the default warning message. See the parameter matrix and the complete API reference for every option.
🎯 Target modes
Choose the behavior explicitly when configuring a wrapper:
Target
Behavior
A replacement callable
Warn and forward calls to the replacement.
TargetMode.NOTIFY
Warn and keep running the source body.
TargetMode.ARGS_REMAP
Rename arguments within the same function.
TargetMode.ATTRS_REMAP
Warn on selected attribute aliases (deprecated_class; deprecated_instance since v0.16).
The default TargetMode.AUTO infers the mode at decoration time. See mode differences for warning triggers and mapping rules.
🔍 Audit
Audit wrappers before release to catch invalid configuration and wrappers that outlived their removal version.
⏰ Check from the command line
pip install 'pyDeprecate[audit]'
pydeprecate check path/to/your/package # invalid wrapper configuration
pydeprecate expiry path/to/your/package --version 2.0.0 # wrappers at or past their remove_in version
pydeprecate status path/to/your/package # deprecation status table
pydeprecate all path/to/your/package --version 2.0.0 # every check in one scan, plus the table
check exits 1 on configuration errors and only reports advisory findings such as identity mappings. expiry exits 1 on any expired wrapper, and statusonly reports. See CLI exit codes before making one a required CI gate.
The hook imports the scanned package, so its imports must resolve without your project's runtime dependencies. The pydeprecate-expiry, pydeprecate-policy, pydeprecate-chains and pydeprecate-all hooks run the other checks. See pre-commit integration for options.
⚙ Run in GitHub Actions
Add a step after checking out the repository and installing your package:
The step returns the CLI exit code. See the GitHub Action recipe for every input.
🐍 Check from Python
The same checks run from a test, which needs no extra CI setup. Removal-deadline checks need the [audit] extra:
# phmdoctest:skip — CI template: replace my_package with your actual package
import my_package
from deprecate import validate_deprecation_expiry
def test_no_expired_deprecations():
expired = validate_deprecation_expiry(my_package, current_version=my_package.__version__)
assert not expired, "\n".join(expired)
find_deprecation_wrappers(my_package) lists every wrapper and its configuration, and generate_deprecation_table(my_package) renders the release-planning table.
Pair pytest.warns with assert_no_warnings to check both that old callers warn and that new callers stay silent:
import pytest
from deprecate import assert_no_warnings, deprecated, void
# NEW API — doubles the input value
def score_predictions(x: int) -> int:
return x * 2
# DEPRECATED API — `score` replaced by `score_predictions`
@deprecated(target=score_predictions, deprecated_in="1.0", remove_in="2.0")
def score(x: int) -> int:
return void(x)
def test_deprecated_function_shows_warning():
with pytest.warns(FutureWarning, match="score.*deprecated"):
assert score(42) == 84
def test_new_function_no_warning():
with assert_no_warnings(FutureWarning):
assert score_predictions(42) == 84
# README demo only: pytest collects the test_* functions itself, so leave these two calls out of a test module
test_deprecated_function_shows_warning()
test_new_function_no_warning()
A wrapper warns once by default (num_warns=1). A pytest.warns check must therefore be the first call of that deprecated function in the test process, or the wrapper needs num_warns=-1. See testing deprecated code for more patterns.
📝 Documentation tools
Set update_docstring=True to write the deprecation notice into the wrapper's docstring, so your rendered API reference shows it next to the signature:
from deprecate import TargetMode, deprecated
# DEPRECATED API — `tidy` keeps working until v2.0 and says so in its docstring
@deprecated(
target=TargetMode.NOTIFY, deprecated_in="1.0", remove_in="2.0", update_docstring=True, docstring_style="rst"
)
def tidy(text: str) -> str:
"""Strip and lowercase the text."""
return text.strip().lower()
print(tidy.__doc__)
Output: tidy.__doc__
Strip and lowercase the text.
.. deprecated:: 1.0
Will be removed in 2.0.
With a callable target, the notice adds a line naming the replacement, such as Use :func:`mypackage.new_name` instead.
The notice is RST/Sphinx or MkDocs/Markdown. Set docstring_style to pick one; otherwise the style is detected from the environment. Bundled extensions keep the notice visible on deprecated classes in documentation builds (beta):
Sphinx — add "deprecate.docstring.sphinx_ext" to extensions in conf.py.
MkDocs — add deprecate.docstring.griffe_ext:RuntimeDocstrings to the mkdocstrings Python handler's options.extensions.
See limitations for the few corners that behave differently than you might expect, each with a workaround. If one blocks a real use case, open an issue: we are happy to discuss it and integrate a fix when it fits.
🏷️ Show that you use pyDeprecate
Add this badge to show that your project provides an API migration path:
[](https://github.com/Borda/pyDeprecate)
🤝 Contributing
Contributions, bug reports, and migration stories are welcome.