extend warnings.warn with callee parameter
.. image:: https://sourceforge.net/p/ruamel-std-warnings/code/ci/default/tree/_doc/_static/license.svg?format=raw
:target: https://opensource.org/licenses/MIT
.. image:: https://sourceforge.net/p/ruamel-std-warnings/code/ci/default/tree/_doc/_static/pypi.svg?format=raw
:target: https://pypi.org/project/ruamel.std.warning
.. image:: https://sourceforge.net/p/oitnb/code/ci/default/tree/_doc/_static/oitnb.svg?format=raw
:target: https://pypi.org/project/oitnb/
When you have some library, with some deprecated function `X`, you can use the `stacklevel=2` parameter
on `warnings.warn` to show the file-name and line-number of the routine calling `X`
But if you have some framework that calls the user provided code, either through plug-ins or
by explicit registering, the `stacklevel` doesn't help you to complain about return values
that are deprecated.
This library extends `warnings.warn` with a `callee` parameter. If this is provided `stacklevel` should
not be provided and the value for `callee` should be a method or function for
which the warning is raised.
The warning will usually be a `PendingDeprecationWarning`, a `DeprecationWarning` or a subclass of either.
As an example, if you have two files `p0.py` and `p2.py` both with content::
class PlugIn:
def status(self):
return {'result': 'ok'}
And a file `p1.py`::
class PlugIn:
def status(self):
return ['ok'] # this plug-in has been updated
And these files are in a subfolder `plug_ins` where your framework can find them. Then running::
import sys
from pathlib import Path
from importlib import import_module
import ruamel.std.warnings
import warnings
class DictReturnPendingDeprecationWarning(PendingDeprecationWarning):
pass
class Driver:
def __init__(self):
self.plug_ins = []
def load_plug_ins(self):
sys.path.append('plug_ins')
for file_name in Path('plug_ins').glob('p*.py'):
mod = import_module(file_name.stem)
self.plug_ins.append(mod.PlugIn())
def check_status(self):
for p in self.plug_ins:
retval = p.status()
if isinstance(retval, dict):
# assume dict
warnings.warn(
'callable should return list, not dict',
DictReturnPendingDeprecationWarning,
callee=p.status,
)
else:
pass # assume list
warnings.simplefilter('once', PendingDeprecationWarning)
def doit():
driver = Driver()
driver.load_plug_ins()
for idx in range(2):
driver.check_status()
warnings.warn('almost done', PendingDeprecationWarning)
doit()
will result in::
/tmp/plug_ins/p0.py:2: DictReturnPendingDeprecationWarning: callable should return list, not dict
def status(self):
/tmp/plug_ins/p2.py:2: DictReturnPendingDeprecationWarning: callable should return list, not dict
def status(self):
/tmp/tmp_00.py:40: PendingDeprecationWarning: almost done
warnings.warn('almost done', PendingDeprecationWarning)
Raw data
{
"_id": null,
"home_page": "https://sourceforge.net/p/ruamel-std-warnings/code/ci/default/tree",
"name": "ruamel.std.warnings",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3",
"maintainer_email": "",
"keywords": "pypi statistics",
"author": "Anthon van der Neut",
"author_email": "a.van.der.neut@ruamel.eu",
"download_url": "https://files.pythonhosted.org/packages/0f/3f/b1806d5c68f713051707105cd109ec0f0dbaa70bdf84eb502d2717a999fd/ruamel.std.warnings-0.3.0.tar.gz",
"platform": null,
"description": "\nextend warnings.warn with callee parameter\n\n.. image:: https://sourceforge.net/p/ruamel-std-warnings/code/ci/default/tree/_doc/_static/license.svg?format=raw\n :target: https://opensource.org/licenses/MIT\n\n.. image:: https://sourceforge.net/p/ruamel-std-warnings/code/ci/default/tree/_doc/_static/pypi.svg?format=raw\n :target: https://pypi.org/project/ruamel.std.warning\n\n.. image:: https://sourceforge.net/p/oitnb/code/ci/default/tree/_doc/_static/oitnb.svg?format=raw\n :target: https://pypi.org/project/oitnb/\n\n\n\nWhen you have some library, with some deprecated function `X`, you can use the `stacklevel=2` parameter \non `warnings.warn` to show the file-name and line-number of the routine calling `X`\n\nBut if you have some framework that calls the user provided code, either through plug-ins or\nby explicit registering, the `stacklevel` doesn't help you to complain about return values\nthat are deprecated.\n\nThis library extends `warnings.warn` with a `callee` parameter. If this is provided `stacklevel` should\nnot be provided and the value for `callee` should be a method or function for\nwhich the warning is raised.\n\nThe warning will usually be a `PendingDeprecationWarning`, a `DeprecationWarning` or a subclass of either.\n\nAs an example, if you have two files `p0.py` and `p2.py` both with content::\n\n class PlugIn:\n def status(self):\n return {'result': 'ok'}\n\nAnd a file `p1.py`::\n\n class PlugIn:\n def status(self):\n return ['ok'] # this plug-in has been updated\n\nAnd these files are in a subfolder `plug_ins` where your framework can find them. Then running::\n\n\n import sys\n from pathlib import Path\n from importlib import import_module\n import ruamel.std.warnings\n import warnings\n\n class DictReturnPendingDeprecationWarning(PendingDeprecationWarning):\n pass\n\n class Driver:\n def __init__(self):\n self.plug_ins = []\n\n def load_plug_ins(self):\n sys.path.append('plug_ins')\n for file_name in Path('plug_ins').glob('p*.py'):\n mod = import_module(file_name.stem) \n self.plug_ins.append(mod.PlugIn())\n\n def check_status(self):\n for p in self.plug_ins:\n retval = p.status()\n if isinstance(retval, dict):\n # assume dict\n warnings.warn(\n 'callable should return list, not dict',\n DictReturnPendingDeprecationWarning,\n callee=p.status,\n )\n else:\n pass # assume list\n\n warnings.simplefilter('once', PendingDeprecationWarning)\n\n def doit():\n driver = Driver()\n driver.load_plug_ins()\n for idx in range(2):\n driver.check_status()\n warnings.warn('almost done', PendingDeprecationWarning)\n\n doit()\n\nwill result in::\n\n /tmp/plug_ins/p0.py:2: DictReturnPendingDeprecationWarning: callable should return list, not dict\n def status(self):\n /tmp/plug_ins/p2.py:2: DictReturnPendingDeprecationWarning: callable should return list, not dict\n def status(self):\n /tmp/tmp_00.py:40: PendingDeprecationWarning: almost done\n warnings.warn('almost done', PendingDeprecationWarning)\n",
"bugtrack_url": null,
"license": "Copyright Ruamel bvba 2007-2023",
"summary": "extend warnings.warn with callee parameter",
"version": "0.3.0",
"project_urls": {
"Homepage": "https://sourceforge.net/p/ruamel-std-warnings/code/ci/default/tree"
},
"split_keywords": [
"pypi",
"statistics"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "466d3d01e94d98c04e07c2fa33ffa5c74022f5cbd96d5d00679a327440ea103a",
"md5": "b70b4aa553fac223160b23929870b642",
"sha256": "2d340805aaf70d39366daafd02d53fcfb9ec60f67e4e0c51450f934b259b8fff"
},
"downloads": -1,
"filename": "ruamel.std.warnings-0.3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b70b4aa553fac223160b23929870b642",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3",
"size": 3868,
"upload_time": "2023-07-27T19:25:23",
"upload_time_iso_8601": "2023-07-27T19:25:23.712589Z",
"url": "https://files.pythonhosted.org/packages/46/6d/3d01e94d98c04e07c2fa33ffa5c74022f5cbd96d5d00679a327440ea103a/ruamel.std.warnings-0.3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0f3fb1806d5c68f713051707105cd109ec0f0dbaa70bdf84eb502d2717a999fd",
"md5": "7e0ba0f6a3ad75f3c2f01b1cde342b7c",
"sha256": "078f284834f4b2bb86c06c9567ab5d7156b120e02ca699e52d02eb8a03d834ec"
},
"downloads": -1,
"filename": "ruamel.std.warnings-0.3.0.tar.gz",
"has_sig": false,
"md5_digest": "7e0ba0f6a3ad75f3c2f01b1cde342b7c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3",
"size": 12640,
"upload_time": "2023-07-27T19:25:21",
"upload_time_iso_8601": "2023-07-27T19:25:21.965532Z",
"url": "https://files.pythonhosted.org/packages/0f/3f/b1806d5c68f713051707105cd109ec0f0dbaa70bdf84eb502d2717a999fd/ruamel.std.warnings-0.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-27 19:25:21",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "ruamel.std.warnings"
}