Skip to main content

deprecate-kwargs

formatting pytest PyPI

A Tool for Deprecating (Keyword) Arguments for Backward Compatibility for Python Functions.

A decorator is implemented to deprecate old kwargs in a function, with signature and docstring modified accordingly. Instead of replacing the old kwargs with new ones, this decorator makes old and new kwargs both available, with warnings raised when old kwargs are passed.

Installation

Run

python -m pip install deprecate-kwargs

or install the latest version in GitHub using

python -m pip install git+https://github.com/DeepPSP/deprecate-kwargs.git

Usage Example

>>> from deprecate_kwargs import deprecate_kwargs
>>> @deprecate_kwargs([["new_arg_1", "old_arg_1"], ["new_arg_2", "old_arg_2"], ["new_kw", "old_kw"]])
>>> def some_func(old_arg_1: int, old_arg_2: int, old_kw: int = 3):
>>>     return (old_arg_1 + old_arg_2) * old_kw
>>> some_func.__signature__
<Signature (new_arg_1: int, new_arg_2: int, new_kw: int = 3)>
>>> some_func(10, 20, 3)
90
>>> some_func(new_arg_1=10, new_arg_2=20, new_kw=3)
90
>>> some_func(10, old_arg_2=20, old_kw=3)
PendingDeprecationWarning: (keyword) argument "old_arg_2" is deprecated, use "new_arg_2" instead
PendingDeprecationWarning: (keyword) argument "old_kw" is deprecated, use "new_kw" instead
90

Benefits

deprecate_kwargs is quite useful when one wants to change the name of an argument (or keyword argument) of some function, while keeping old codes using this function still working, hence is beneficial for backward compatibility. For example, say in version 0.1 of some package, there's a function

def some_deep_learning_model_trainer(learning_rate, ...):
    ...

And in version 0.2, someone wants to change learning_rate to lr. If it was replaced directly via

def some_deep_learning_model_trainer(lr, ...):
    ...

then old codes using this function bycalling some_deep_learning_model_trainer(learning_rate=1e-3, ...) would break. However, if the replacement is done using

@deprecate_kwargs([["lr", "learning_rate"]])
def some_deep_learning_model_trainer(learning_rate, ...):
    ...

then one can call this function using some_deep_learning_model_trainer(lr=1e-3), as well as some_deep_learning_model_trainer(learning_rate=1e-3, ...) only with a warning raised. In this way, old codes are rescued from breaking.

Metadata

Release files for deprecate-kwargs 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for deprecate-kwargs 0.0.2
File Size Uploaded Provenance Actions
deprecate_kwargs-0.0.2.tar.gz 3.8 kB Without provenance Details for deprecate_kwargs-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for deprecate-kwargs 0.0.2
File Interpreter ABI Platform Actions
deprecate_kwargs-0.0.2-py3-none-any.whl Python 3 none any Details for deprecate_kwargs-0.0.2-py3-none-any.whl

Total release size: 7.9 kB

Release files / deprecate_kwargs-0.0.2.tar.gz

Download URL deprecate_kwargs-0.0.2.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7132d15c568e4bb13f10b9d3c82e9f4f1a224b44d070cd588f68388f851bf736
BLAKE2b-256 checksum
How to use checksums
daf88f91ed2ab0bece25146226d1328e55f1324ce8cc159ab84ab5caa0b10189
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.6 tqdm/4.62.3 importlib-metadata/4.5.0 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.9

Release files / deprecate_kwargs-0.0.2-py3-none-any.whl

Download URL deprecate_kwargs-0.0.2-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
196efcc24d0024f792c3f8701a2020be169af2f05d7d3a9c44fe9c7bcb7c50e0
BLAKE2b-256 checksum
How to use checksums
786649d1e3b0b78058faa3a76f6555ccb23fa202ea47ff5835a345235f46439e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.6 tqdm/4.62.3 importlib-metadata/4.5.0 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.9

Security for deprecate-kwargs 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page