# pyDeprecate
**Simple tooling for marking deprecated functions or classes and re-routing to the new successors' instance.**
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pyDeprecate)](https://pypi.org/project/pyDeprecate/)
[![PyPI Status](https://badge.fury.io/py/pyDeprecate.svg)](https://badge.fury.io/py/pyDeprecate)
[![PyPI Status](https://pepy.tech/badge/pyDeprecate)](https://pepy.tech/project/pyDeprecate)
[![Conda](https://img.shields.io/conda/v/conda-forge/pyDeprecate?label=conda&color=success)](https://anaconda.org/conda-forge/pyDeprecate)
![Conda](https://img.shields.io/conda/dn/conda-forge/pyDeprecate)
[![license](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/Borda/pyDeprecate/blob/master/LICENSE)
[![CI testing](https://github.com/Borda/pyDeprecate/actions/workflows/ci_testing.yml/badge.svg?tag=0.3.2)](https://github.com/Borda/pyDeprecate/actions/workflows/ci_testing.yml)
[![Code formatting](https://github.com/Borda/pyDeprecate/actions/workflows/code-format.yml/badge.svg?tag=0.3.2)](https://github.com/Borda/pyDeprecate/actions/workflows/code-format.yml)
[![codecov](https://codecov.io/gh/Borda/pyDeprecate/release/0.3.2/graph/badge.svg?token=BG7RQ86UJA)](https://codecov.io/gh/Borda/pyDeprecate)
[![CodeFactor](https://www.codefactor.io/repository/github/borda/pydeprecate/badge)](https://www.codefactor.io/repository/github/borda/pydeprecate)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/Borda/pyDeprecate/main.svg)](https://results.pre-commit.ci/latest/github/Borda/pyDeprecate/main)
<!--
[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/Borda/pyDeprecate.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Borda/pyDeprecate/context:python)
-->
---
The common use-case is moving your functions across codebase or outsourcing some functionalities to new packages.
For most of these cases, you want to hold some compatibility, so you cannot simply remove past function, and also for some time you want to warn users that functionality they have been using is moved and not it is deprecated in favor of another function (which shall be used instead) and soon it will be removed completely.
Another good aspect is to do not overwhelm a user with too many warnings, so per function/class, this warning is raised only N times in the preferable stream (warning, logger, etc.).
## Installation
Simple installation from PyPI:
```bash
pip install pyDeprecate
```
<details>
<summary>Other installations</summary>
Simply install with pip from source:
```bash
pip install https://github.com/Borda/pyDeprecate/archive/main.zip
```
</details>
## Use-cases
The functionality is kept simple and all default shall be reasonable, but still you can do extra customization such as:
* define user warning message and preferable stream
* extended argument mapping to target function/method
* define deprecation logic for self arguments
* specify warning count per:
- called function (for func deprecation)
- used arguments (for argument deprecation)
* define conditional skip (e.g. depending on some package version)
In particular the target values (cases):
- _None_ - raise only warning message (ignore all argument mapping)
- _True_ - deprecation some argument of itself (argument mapping shall be specified)
- _Callable_ - forward call to new methods (optional also argument mapping or extras)
### Simple function forwarding
It is very straight forward, you forward your function call to new function and all arguments are mapped:
```python
def base_sum(a: int = 0, b: int = 3) -> int:
"""My new function anywhere in codebase or even other package."""
return a + b
# ---------------------------
from deprecate import deprecated
@deprecated(target=base_sum, deprecated_in="0.1", remove_in="0.5")
def depr_sum(a: int, b: int = 5) -> int:
"""
My deprecated function which now has empty body
as all calls are routed to the new function.
"""
pass # or you can just place docstring as one above
# call this function will raise deprecation warning:
# The `depr_sum` was deprecated since v0.1 in favor of `__main__.base_sum`.
# It will be removed in v0.5.
print(depr_sum(1, 2))
```
<details>
<summary>sample output:</summary>
```
3
```
</details>
### Advanced target argument mapping
Another more complex example is using argument mapping is:
<details>
<summary>Advanced example</summary>
```python
import logging
from sklearn.metrics import accuracy_score
from deprecate import deprecated, void
@deprecated(
# use standard sklearn accuracy implementation
target=accuracy_score,
# custom warning stream
stream=logging.warning,
# number or warnings per lifetime (with -1 for always_
num_warns=5,
# custom message template
template_mgs="`%(source_name)s` was deprecated, use `%(target_path)s`",
# as target args are different, define mapping from source to target func
args_mapping={'preds': 'y_pred', 'target': 'y_true', 'blabla': None}
)
def depr_accuracy(preds: list, target: list, blabla: float) -> float:
"""My deprecated function which is mapping to sklearn accuracy."""
# to stop complain your IDE about unused argument you can use void/empty function
return void(preds, target, blabla)
# call this function will raise deprecation warning:
# WARNING:root:`depr_accuracy` was deprecated, use `sklearn.metrics.accuracy_score`
print(depr_accuracy([1, 0, 1, 2], [0, 1, 1, 2], 1.23))
```
sample output:
```
0.5
```
</details>
### Deprecation warning only
Base use-case with no forwarding and just raising warning :
```python
from deprecate import deprecated
@deprecated(target=None, deprecated_in="0.1", remove_in="0.5")
def my_sum(a: int, b: int = 5) -> int:
"""My deprecated function which still has to have implementation."""
return a + b
# call this function will raise deprecation warning:
# The `my_sum` was deprecated since v0.1. It will be removed in v0.5.
print(my_sum(1, 2))
```
<details>
<summary>sample output:</summary>
```
3
```
</details>
### Self argument mapping
We also support deprecation and argument mapping for the function itself:
```python
from deprecate import deprecated
@deprecated(
# define as depreaction some self argument - mapping
target=True, args_mapping={'coef': 'new_coef'},
# common version info
deprecated_in="0.2", remove_in="0.4",
)
def any_pow(base: float, coef: float = 0, new_coef: float = 0) -> float:
"""My function with deprecated argument `coef` mapped to `new_coef`."""
return base ** new_coef
# call this function will raise deprecation warning:
# The `any_pow` uses deprecated arguments: `coef` -> `new_coef`.
# They were deprecated since v0.2 and will be removed in v0.4.
print(any_pow(2, 3))
```
<details>
<summary>sample output:</summary>
```
8
```
</details>
### Multiple deprecation levels
Eventually you can set multiple deprecation levels via chaining deprecation arguments as each could be deprecated in another version:
<details>
<summary>Multiple deprecation levels</summary>
```python
from deprecate import deprecated
@deprecated(
True, "0.3", "0.6", args_mapping=dict(c1='nc1'),
template_mgs="Depr: v%(deprecated_in)s rm v%(remove_in)s for args: %(argument_map)s."
)
@deprecated(
True, "0.4", "0.7", args_mapping=dict(nc1='nc2'),
template_mgs="Depr: v%(deprecated_in)s rm v%(remove_in)s for args: %(argument_map)s."
)
def any_pow(base, c1: float = 0, nc1: float = 0, nc2: float = 2) -> float:
return base**nc2
# call this function will raise deprecation warning:
# DeprecationWarning('Depr: v0.3 rm v0.6 for args: `c1` -> `nc1`.')
# DeprecationWarning('Depr: v0.4 rm v0.7 for args: `nc1` -> `nc2`.')
print(any_pow(2, 3))
```
sample output:
```
8
```
</details>
### Conditional skip
Conditional skip of which can be used for mapping between different target functions depending on additional input such as package version
```python
from deprecate import deprecated
FAKE_VERSION = 1
def version_greater_1():
return FAKE_VERSION > 1
@deprecated(
True, "0.3", "0.6", args_mapping=dict(c1='nc1'), skip_if=version_greater_1
)
def skip_pow(base, c1: float = 1, nc1: float = 1) -> float:
return base**(c1 - nc1)
# call this function will raise deprecation warning
print(skip_pow(2, 3))
# change the fake versions
FAKE_VERSION = 2
# Will not raise any warning
print(skip_pow(2, 3))
```
<details>
<summary>sample output:</summary>
```
0.25
4
```
</details>
This can be beneficial with multiple deprecation levels shown above...
### Class deprecation
This case can be quite complex as you may deprecate just some methods, here we show full class deprecation:
```python
class NewCls:
"""My new class anywhere in the codebase or other package."""
def __init__(self, c: float, d: str = "abc"):
self.my_c = c
self.my_d = d
# ---------------------------
from deprecate import deprecated, void
class PastCls(NewCls):
"""
The deprecated class shall be inherited from the successor class
to hold all methods.
"""
@deprecated(target=NewCls, deprecated_in="0.2", remove_in="0.4")
def __init__(self, c: int, d: str = "efg"):
"""
You place the decorator around __init__ as you want
to warn user just at the time of creating object.
"""
return void(c, d)
# call this function will raise deprecation warning:
# The `PastCls` was deprecated since v0.2 in favor of `__main__.NewCls`.
# It will be removed in v0.4.
inst = PastCls(7)
print(inst.my_c) # returns: 7
print(inst.my_d) # returns: "efg"
```
<details>
<summary>sample output:</summary>
```
7
efg
```
</details>
## Contribution
Have you faced this in past or even now, do you have good ideas for improvement, all is welcome!
Raw data
{
"_id": null,
"home_page": "https://borda.github.io/pyDeprecate",
"name": "pyDeprecate",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "python,development,deprecation",
"author": "Jiri Borovec",
"author_email": "jiri.borovec@fel.cvut.cz",
"download_url": "https://files.pythonhosted.org/packages/ad/8a/04cb2f59d7d24f9dc6c690835933147f068d26b8b765c2ca1220cbc38750/pyDeprecate-0.3.2.tar.gz",
"platform": "",
"description": "# pyDeprecate\n\n**Simple tooling for marking deprecated functions or classes and re-routing to the new successors' instance.**\n\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pyDeprecate)](https://pypi.org/project/pyDeprecate/)\n[![PyPI Status](https://badge.fury.io/py/pyDeprecate.svg)](https://badge.fury.io/py/pyDeprecate)\n[![PyPI Status](https://pepy.tech/badge/pyDeprecate)](https://pepy.tech/project/pyDeprecate)\n[![Conda](https://img.shields.io/conda/v/conda-forge/pyDeprecate?label=conda&color=success)](https://anaconda.org/conda-forge/pyDeprecate)\n![Conda](https://img.shields.io/conda/dn/conda-forge/pyDeprecate)\n[![license](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/Borda/pyDeprecate/blob/master/LICENSE)\n\n[![CI testing](https://github.com/Borda/pyDeprecate/actions/workflows/ci_testing.yml/badge.svg?tag=0.3.2)](https://github.com/Borda/pyDeprecate/actions/workflows/ci_testing.yml)\n[![Code formatting](https://github.com/Borda/pyDeprecate/actions/workflows/code-format.yml/badge.svg?tag=0.3.2)](https://github.com/Borda/pyDeprecate/actions/workflows/code-format.yml)\n[![codecov](https://codecov.io/gh/Borda/pyDeprecate/release/0.3.2/graph/badge.svg?token=BG7RQ86UJA)](https://codecov.io/gh/Borda/pyDeprecate)\n[![CodeFactor](https://www.codefactor.io/repository/github/borda/pydeprecate/badge)](https://www.codefactor.io/repository/github/borda/pydeprecate)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/Borda/pyDeprecate/main.svg)](https://results.pre-commit.ci/latest/github/Borda/pyDeprecate/main)\n\n<!--\n[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/Borda/pyDeprecate.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Borda/pyDeprecate/context:python)\n-->\n\n---\n\nThe common use-case is moving your functions across codebase or outsourcing some functionalities to new packages.\nFor most of these cases, you want to hold some compatibility, so you cannot simply remove past function, and also for some time you want to warn users that functionality they have been using is moved and not it is deprecated in favor of another function (which shall be used instead) and soon it will be removed completely.\n\nAnother good aspect is to do not overwhelm a user with too many warnings, so per function/class, this warning is raised only N times in the preferable stream (warning, logger, etc.).\n\n## Installation\n\nSimple installation from PyPI:\n```bash\npip install pyDeprecate\n```\n\n<details>\n <summary>Other installations</summary>\n\n Simply install with pip from source:\n ```bash\n pip install https://github.com/Borda/pyDeprecate/archive/main.zip\n ```\n\n</details>\n\n## Use-cases\n\nThe functionality is kept simple and all default shall be reasonable, but still you can do extra customization such as:\n\n* define user warning message and preferable stream\n* extended argument mapping to target function/method\n* define deprecation logic for self arguments\n* specify warning count per:\n - called function (for func deprecation)\n - used arguments (for argument deprecation)\n* define conditional skip (e.g. depending on some package version)\n\nIn particular the target values (cases):\n\n- _None_ - raise only warning message (ignore all argument mapping)\n- _True_ - deprecation some argument of itself (argument mapping shall be specified)\n- _Callable_ - forward call to new methods (optional also argument mapping or extras)\n\n### Simple function forwarding\n\nIt is very straight forward, you forward your function call to new function and all arguments are mapped:\n\n```python\ndef base_sum(a: int = 0, b: int = 3) -> int:\n \"\"\"My new function anywhere in codebase or even other package.\"\"\"\n return a + b\n\n# ---------------------------\n\nfrom deprecate import deprecated\n\n@deprecated(target=base_sum, deprecated_in=\"0.1\", remove_in=\"0.5\")\ndef depr_sum(a: int, b: int = 5) -> int:\n \"\"\"\n My deprecated function which now has empty body\n as all calls are routed to the new function.\n \"\"\"\n pass # or you can just place docstring as one above\n\n# call this function will raise deprecation warning:\n# The `depr_sum` was deprecated since v0.1 in favor of `__main__.base_sum`.\n# It will be removed in v0.5.\nprint(depr_sum(1, 2))\n```\n<details>\n <summary>sample output:</summary>\n ```\n 3\n ```\n</details>\n\n### Advanced target argument mapping\n\nAnother more complex example is using argument mapping is:\n\n\n<details>\n <summary>Advanced example</summary>\n\n ```python\n import logging\n from sklearn.metrics import accuracy_score\n from deprecate import deprecated, void\n\n @deprecated(\n # use standard sklearn accuracy implementation\n target=accuracy_score,\n # custom warning stream\n stream=logging.warning,\n # number or warnings per lifetime (with -1 for always_\n num_warns=5,\n # custom message template\n template_mgs=\"`%(source_name)s` was deprecated, use `%(target_path)s`\",\n # as target args are different, define mapping from source to target func\n args_mapping={'preds': 'y_pred', 'target': 'y_true', 'blabla': None}\n )\n def depr_accuracy(preds: list, target: list, blabla: float) -> float:\n \"\"\"My deprecated function which is mapping to sklearn accuracy.\"\"\"\n # to stop complain your IDE about unused argument you can use void/empty function\n return void(preds, target, blabla)\n\n # call this function will raise deprecation warning:\n # WARNING:root:`depr_accuracy` was deprecated, use `sklearn.metrics.accuracy_score`\n print(depr_accuracy([1, 0, 1, 2], [0, 1, 1, 2], 1.23))\n ```\n sample output:\n ```\n 0.5\n ```\n\n</details>\n\n\n### Deprecation warning only\n\nBase use-case with no forwarding and just raising warning :\n\n```python\nfrom deprecate import deprecated\n\n@deprecated(target=None, deprecated_in=\"0.1\", remove_in=\"0.5\")\ndef my_sum(a: int, b: int = 5) -> int:\n \"\"\"My deprecated function which still has to have implementation.\"\"\"\n return a + b\n\n# call this function will raise deprecation warning:\n# The `my_sum` was deprecated since v0.1. It will be removed in v0.5.\nprint(my_sum(1, 2))\n```\n<details>\n <summary>sample output:</summary>\n ```\n 3\n ```\n</details>\n\n### Self argument mapping\n\nWe also support deprecation and argument mapping for the function itself:\n\n```python\nfrom deprecate import deprecated\n\n@deprecated(\n # define as depreaction some self argument - mapping\n target=True, args_mapping={'coef': 'new_coef'},\n # common version info\n deprecated_in=\"0.2\", remove_in=\"0.4\",\n)\ndef any_pow(base: float, coef: float = 0, new_coef: float = 0) -> float:\n \"\"\"My function with deprecated argument `coef` mapped to `new_coef`.\"\"\"\n return base ** new_coef\n\n# call this function will raise deprecation warning:\n# The `any_pow` uses deprecated arguments: `coef` -> `new_coef`.\n# They were deprecated since v0.2 and will be removed in v0.4.\nprint(any_pow(2, 3))\n```\n<details>\n <summary>sample output:</summary>\n ```\n 8\n ```\n</details>\n\n### Multiple deprecation levels\n\nEventually you can set multiple deprecation levels via chaining deprecation arguments as each could be deprecated in another version:\n\n<details>\n <summary>Multiple deprecation levels</summary>\n\n ```python\n from deprecate import deprecated\n\n @deprecated(\n True, \"0.3\", \"0.6\", args_mapping=dict(c1='nc1'),\n template_mgs=\"Depr: v%(deprecated_in)s rm v%(remove_in)s for args: %(argument_map)s.\"\n )\n @deprecated(\n True, \"0.4\", \"0.7\", args_mapping=dict(nc1='nc2'),\n template_mgs=\"Depr: v%(deprecated_in)s rm v%(remove_in)s for args: %(argument_map)s.\"\n )\n def any_pow(base, c1: float = 0, nc1: float = 0, nc2: float = 2) -> float:\n return base**nc2\n\n # call this function will raise deprecation warning:\n # DeprecationWarning('Depr: v0.3 rm v0.6 for args: `c1` -> `nc1`.')\n # DeprecationWarning('Depr: v0.4 rm v0.7 for args: `nc1` -> `nc2`.')\n print(any_pow(2, 3))\n ```\n sample output:\n ```\n 8\n ```\n\n</details>\n\n### Conditional skip\n\nConditional skip of which can be used for mapping between different target functions depending on additional input such as package version\n\n```python\nfrom deprecate import deprecated\n\nFAKE_VERSION = 1\n\ndef version_greater_1():\n return FAKE_VERSION > 1\n\n@deprecated(\n True, \"0.3\", \"0.6\", args_mapping=dict(c1='nc1'), skip_if=version_greater_1\n)\ndef skip_pow(base, c1: float = 1, nc1: float = 1) -> float:\n return base**(c1 - nc1)\n\n# call this function will raise deprecation warning\nprint(skip_pow(2, 3))\n\n# change the fake versions\nFAKE_VERSION = 2\n\n# Will not raise any warning\nprint(skip_pow(2, 3))\n```\n<details>\n <summary>sample output:</summary>\n ```\n 0.25\n 4\n ```\n</details>\n\nThis can be beneficial with multiple deprecation levels shown above...\n\n### Class deprecation\n\nThis case can be quite complex as you may deprecate just some methods, here we show full class deprecation:\n\n```python\nclass NewCls:\n \"\"\"My new class anywhere in the codebase or other package.\"\"\"\n\n def __init__(self, c: float, d: str = \"abc\"):\n self.my_c = c\n self.my_d = d\n\n# ---------------------------\n\nfrom deprecate import deprecated, void\n\nclass PastCls(NewCls):\n \"\"\"\n The deprecated class shall be inherited from the successor class\n to hold all methods.\n \"\"\"\n\n @deprecated(target=NewCls, deprecated_in=\"0.2\", remove_in=\"0.4\")\n def __init__(self, c: int, d: str = \"efg\"):\n \"\"\"\n You place the decorator around __init__ as you want\n to warn user just at the time of creating object.\n \"\"\"\n return void(c, d)\n\n# call this function will raise deprecation warning:\n# The `PastCls` was deprecated since v0.2 in favor of `__main__.NewCls`.\n# It will be removed in v0.4.\ninst = PastCls(7)\nprint(inst.my_c) # returns: 7\nprint(inst.my_d) # returns: \"efg\"\n```\n<details>\n <summary>sample output:</summary>\n ```\n 7\n efg\n ```\n</details>\n\n## Contribution\n\nHave you faced this in past or even now, do you have good ideas for improvement, all is welcome!\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Deprecation tooling",
"version": "0.3.2",
"split_keywords": [
"python",
"development",
"deprecation"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "ad7c9171c14346068fa3708c36ff5a2f",
"sha256": "ed86b68ed837e6465245904a3de2f59bf9eef78ac7a2502ee280533d04802457"
},
"downloads": -1,
"filename": "pyDeprecate-0.3.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ad7c9171c14346068fa3708c36ff5a2f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 10400,
"upload_time": "2021-06-11T09:33:40",
"upload_time_iso_8601": "2021-06-11T09:33:40.149534Z",
"url": "https://files.pythonhosted.org/packages/40/9c/173f3cf770e66f3c9592318806aebb8617ba405d6d4c09493dabea75985c/pyDeprecate-0.3.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "1a1defba963f403e895945d60d994a8b",
"sha256": "d481116cc5d7f6c473e7c4be820efdd9b90a16b594b350276e9e66a6cb5bdd29"
},
"downloads": -1,
"filename": "pyDeprecate-0.3.2.tar.gz",
"has_sig": false,
"md5_digest": "1a1defba963f403e895945d60d994a8b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 11148,
"upload_time": "2021-06-11T09:33:41",
"upload_time_iso_8601": "2021-06-11T09:33:41.199677Z",
"url": "https://files.pythonhosted.org/packages/ad/8a/04cb2f59d7d24f9dc6c690835933147f068d26b8b765c2ca1220cbc38750/pyDeprecate-0.3.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-06-11 09:33:41",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "pydeprecate"
}