Deprecator provides a decorator to easily deprecate old code.
Project description
Deprecator
A framework for managing deprecation warnings with version-aware categorization.
Overview
The deprecator package provides a structured approach to handling deprecation warnings in Python packages, automatically categorizing warnings based on version comparisons and providing flexible management of deprecation policies across different packages and frameworks.
Requirements
- Python 3.10 or higher
Quick Start
-
Install deprecator with CLI support:
pip install 'deprecator[cli]'
-
Initialize deprecator in your project:
deprecator initThis creates a
_deprecations.pymodule and configures yourpyproject.toml. -
Define your deprecations:
# In _deprecations.py from deprecator import for_package deprecator = for_package(__package__) OLD_API_DEPRECATION = deprecator.define( "old_api is deprecated, use new_api instead", warn_in="2.0.0", gone_in="3.0.0" )
-
Use deprecations in your code:
from ._deprecations import OLD_API_DEPRECATION @OLD_API_DEPRECATION.apply def old_api(): pass
Architecture
Core Components
-
Deprecator(deprecator/_deprecator.py): The main deprecation management class that:- Tracks package version and creates version-specific warning categories
- Provides
define()method to create deprecation warnings based ongone_in/warn_inversions - Automatically categorizes warnings as pending, active, or expired based on current version
- Tracks all defined deprecations internally
-
DeprecatorRegistry(deprecator/_registry.py): Manages multiple deprecator instances:- Caches deprecators by package name to avoid duplication
- Provides
for_package()method to get or create deprecators - Framework-specific registries allow different libraries to manage their own deprecation scopes
- Each registry has a
frameworkattribute identifying the framework/library it serves
-
Warning Categories (
deprecator/_deprecator.py): Version-aware warning types:PerPackagePendingDeprecationWarning: For future deprecations (current_version < warn_in)PerPackageDeprecationWarning: For active deprecations (warn_in <= current_version < gone_in)PerPackageExpiredDeprecationWarning: For expired deprecations (gone_in <= current_version)
Public API
deprecate(): Legacy decorator function for simple deprecation warningsfor_package(package_name: PackageName | str): Get a deprecator bound to a specific packageregistry_for_package(package_name: PackageName | str): Get a registry bound to a specific framework
Version Logic
The deprecator automatically determines warning categories based on version comparison:
- If current version >= gone_in version → ExpiredDeprecationWarning (should cause errors)
- If current version >= warn_in version → DeprecationWarning (active warnings)
- Otherwise → PendingDeprecationWarning (future warnings, typically for tests)
Usage
from deprecator import for_package
# Get a deprecator for your package (accepts str or PackageName)
deprecator = for_package("mypackage")
# Define a deprecation (using UPPER_CASE for constants)
OLD_FUNCTION_DEPRECATION = deprecator.define(
"old_function is deprecated, use new_function instead",
warn_in="2.0.0",
gone_in="3.0.0"
)
# Method 1: Use as a decorator
@OLD_FUNCTION_DEPRECATION.apply
def old_function():
# The decorator automatically emits the warning
# ... existing functionality
pass
# Method 2: Emit warning manually
def another_old_function():
# Standard warning (uses stacklevel=2 by default)
OLD_FUNCTION_DEPRECATION.warn()
# ... existing functionality
# Method 3: Custom stacklevel for wrapper functions
def wrapper_function():
# Custom stacklevel for wrapper functions
OLD_FUNCTION_DEPRECATION.warn(stacklevel=3)
# Method 4: Explicit file/line for tools and linters
def linter_integration():
# Explicit file/line for tools and linters
OLD_FUNCTION_DEPRECATION.warn_explicit("myfile.py", 42)
Warning Methods
Each deprecation warning instance provides three methods for using deprecations:
apply: Use as a decorator to automatically emit warnings when the decorated function/class is usedwarn(stacklevel=2): Emit warning using the standard warnings systemwarn_explicit(filename, lineno, module=None): Emit warning with explicit location
The apply decorator uses typing_extensions.deprecated under the hood, while warn() and warn_explicit() mirror the stdlib warnings.warn() and warnings.warn_explicit() functions.
Cookbook
For practical examples and common patterns, see the Cookbook.
Testing Structure
Tests are organized by functionality:
test_deprecator.py: Core deprecator functionalitytest_registry.py: Registry management and cachingtest_exposed_api.py: Public API surfacetest_legacy_deprecator.py: Legacy decorator compatibility
Project details
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 deprecator-26.1.12.0.tar.gz.
File metadata
- Download URL: deprecator-26.1.12.0.tar.gz
- Upload date:
- Size: 114.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95fd8df5605af9ea746a9985ebfcd16b794e09ed79ab703d1a298964998044b2
|
|
| MD5 |
bf135823f38c0635723e98d247660500
|
|
| BLAKE2b-256 |
621609c20f0fd0bad01adc7fae31149a4cfe29cfcdee1f29efea6924577137ef
|
Provenance
The following attestation bundles were made for deprecator-26.1.12.0.tar.gz:
Publisher:
ci.yml on RonnyPfannschmidt/deprecator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
deprecator-26.1.12.0.tar.gz -
Subject digest:
95fd8df5605af9ea746a9985ebfcd16b794e09ed79ab703d1a298964998044b2 - Sigstore transparency entry: 814276545
- Sigstore integration time:
-
Permalink:
RonnyPfannschmidt/deprecator@e42318bf55c4447c721a9028e36dbbcaf32fb89c -
Branch / Tag:
refs/tags/v26.1.12.0 - Owner: https://github.com/RonnyPfannschmidt
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@e42318bf55c4447c721a9028e36dbbcaf32fb89c -
Trigger Event:
push
-
Statement type:
File details
Details for the file deprecator-26.1.12.0-py3-none-any.whl.
File metadata
- Download URL: deprecator-26.1.12.0-py3-none-any.whl
- Upload date:
- Size: 22.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd73d36e69919e2416fce4af9b8561c2e8c4eaeb449e69623aa8d4840b3a108a
|
|
| MD5 |
06c5bf9c3ef6f6fe0936305a17f55f04
|
|
| BLAKE2b-256 |
52c1db499f5fb1cac4dab18fbcc2b2f8376c5c17f81089a40fc7a4275bdf35e2
|
Provenance
The following attestation bundles were made for deprecator-26.1.12.0-py3-none-any.whl:
Publisher:
ci.yml on RonnyPfannschmidt/deprecator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
deprecator-26.1.12.0-py3-none-any.whl -
Subject digest:
dd73d36e69919e2416fce4af9b8561c2e8c4eaeb449e69623aa8d4840b3a108a - Sigstore transparency entry: 814276548
- Sigstore integration time:
-
Permalink:
RonnyPfannschmidt/deprecator@e42318bf55c4447c721a9028e36dbbcaf32fb89c -
Branch / Tag:
refs/tags/v26.1.12.0 - Owner: https://github.com/RonnyPfannschmidt
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@e42318bf55c4447c721a9028e36dbbcaf32fb89c -
Trigger Event:
push
-
Statement type: