extend warnings.warn with callee parameter
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)
Metadata
Release files for ruamel.std.warnings 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ruamel.std.warnings-0.3.0.tar.gz | 12.6 kB | Without provenance | Details for ruamel.std.warnings-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ruamel.std.warnings-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for ruamel.std.warnings-0.3.0-py3-none-any.whl |
Total release size: 16.5 kB
Release files / ruamel.std.warnings-0.3.0.tar.gz
| Download URL | ruamel.std.warnings-0.3.0.tar.gz |
|---|---|
| Size | 12.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
078f284834f4b2bb86c06c9567ab5d7156b120e02ca699e52d02eb8a03d834ec
|
|
BLAKE2b-256 checksum How to use checksums |
0f3fb1806d5c68f713051707105cd109ec0f0dbaa70bdf84eb502d2717a999fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.5
|
Release files / ruamel.std.warnings-0.3.0-py3-none-any.whl
| Download URL | ruamel.std.warnings-0.3.0-py3-none-any.whl |
|---|---|
| Size | 3.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2d340805aaf70d39366daafd02d53fcfb9ec60f67e4e0c51450f934b259b8fff
|
|
BLAKE2b-256 checksum How to use checksums |
466d3d01e94d98c04e07c2fa33ffa5c74022f5cbd96d5d00679a327440ea103a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.5
|
Security for ruamel.std.warnings 0.3.0
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.