Skip to main content

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

Project description

deprecate-kwargs

formatting pytest codecov PyPI downloads license GitHub Release Date - Published_At GitHub commits since latest release (by SemVer including pre-releases)

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([["kw_new_again", "new_kw"]], version="0.3.0")
... @deprecate_kwargs([["new_arg_1", "old_arg_1"], ["new_arg_2", "old_arg_2"], ["new_kw", "old_kw"]], version="0.1.0")
... 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
  warnings.warn(
PendingDeprecationWarning: (keyword) argument "old_kw" is deprecated, use "new_kw" instead
  warnings.warn(
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"]], version="0.2")
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.

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

deprecate_kwargs-0.1.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

deprecate_kwargs-0.1.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file deprecate_kwargs-0.1.1.tar.gz.

File metadata

  • Download URL: deprecate_kwargs-0.1.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for deprecate_kwargs-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8013676c077c23e5d32c592409c743ccc0013c2206a3b05d1c35f0e83dcb6360
MD5 032f15d38f42dccfcdef34e5687af513
BLAKE2b-256 5189afbaec0a091897eeee0a8f2984533fe302906759df7455ad2624b4b796af

See more details on using hashes here.

File details

Details for the file deprecate_kwargs-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for deprecate_kwargs-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 740bac42366f519f967cc13d3fb455c17784dab32c8ec6edf2d8c075b076756a
MD5 c313dca022ef4b8a51746378e8914a0e
BLAKE2b-256 d766701a5a6ceb12068bb505225fce36fd2510f11dc09fe1afbf96153bc5ea83

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page