Best-effort importer and decorator that suppresses exceptions.
Project description
python-besteffort
python-besteffort installs a custom importer that rewrites Python source on
the fly so that every statement executes inside a
contextlib.suppress(Exception) block. When an unexpected exception is
raised, execution simply skips the failing statement and keeps going—perfect
for exploratory data analysis, flaky integrations, or anywhere failure should
not immediately abort a workflow.
Features
- Auto-installing importer – importing :mod:
besteffortregisters the meta-path finder sofrom besteffort import yourmoduleworks without any additional steps. Call :func:besteffort.installmanually if you need to reinstall it. - Function decorator – use
@besteffortto protect just the functions you care about without rewriting entire modules. - Preserves module semantics – relative imports and globals are kept in sync with the original module, ensuring wrapped code still behaves like the unmodified version whenever no exceptions occur.
Installation
Install the package from PyPI:
pip install besteffort
Alternatively, install straight from the repository when testing a development build:
pip install git+https://github.com/grantjenks/python-besteffort.git
Python compatibility
python-besteffort supports CPython 3.10 and newer. The test suite runs on
Python 3.10 through 3.13 and automatically exercises syntax introduced in later
releases (such as structural pattern matching and exception groups) when the
interpreter supports it, while skipping those checks on earlier versions.
Quick start
Import best-effort modules
import besteffort # registers the meta-path finder on import
# ``yourmodule`` is an example module shipped with the project. Importing it
# through ``besteffort`` returns a wrapped version where each statement is
# guarded by ``contextlib.suppress(Exception)``.
from besteffort import yourmodule
yourmodule.example("G")
The wrapped module behaves as if with suppress(Exception): surrounded each
statement. Lines that raise simply fail closed and execution continues. Even a
bare from besteffort import yourmodule works because Python imports the
package before looking up yourmodule.
Decorate individual functions
from besteffort import besteffort
@besteffort
def fragile(value: int) -> int:
print("before divide")
value /= 0
print("after divide")
return value
fragile(10)
Only the decorated function is rewritten, allowing the rest of the module to
run normally. Use @besteffort as the innermost decorator when stacking it
with others.
Testing
Run the unit test suite locally before contributing:
pytest
# or, to use our automation profile
nox -s tests
Limitations
- Only rewrites Python source (
.py). Built-ins and C extensions cannot be transformed. - Top-level module code is not wrapped; if a module raises at import time,
importing via
besteffortwill still fail. - Relative imports from the instrumented module work by setting
__package__to the original package; absolute imports inside the module load the normal (unmodified) dependencies.
Contributing
If you have an issue, give it to Codex. If it's too complex for Codex then :shrug: Only pull requests opened by Codex will be accepted.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file besteffort-0.1.1.tar.gz.
File metadata
- Download URL: besteffort-0.1.1.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
074a5d8233d99d106794b4bdfbc5afd209e7e51dea8807d280946b386f7e8408
|
|
| MD5 |
b4fb5c16e4f8c646d208141eb3ba4816
|
|
| BLAKE2b-256 |
30742a51bdc0696af816f8a48b2e1220a1b6d1568522d769072db3e5730f8d24
|
Provenance
The following attestation bundles were made for besteffort-0.1.1.tar.gz:
Publisher:
release.yml on grantjenks/python-besteffort
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
besteffort-0.1.1.tar.gz -
Subject digest:
074a5d8233d99d106794b4bdfbc5afd209e7e51dea8807d280946b386f7e8408 - Sigstore transparency entry: 662110787
- Sigstore integration time:
-
Permalink:
grantjenks/python-besteffort@a9475b2eedfbb89a9f92a3d79443d59a8b23cf23 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/grantjenks
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a9475b2eedfbb89a9f92a3d79443d59a8b23cf23 -
Trigger Event:
push
-
Statement type:
File details
Details for the file besteffort-0.1.1-py3-none-any.whl.
File metadata
- Download URL: besteffort-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3169ba2bba9f7eabc8678a563941d424ef873c1caa1c2719c94b83f8e08b18e
|
|
| MD5 |
455ef67f28a178a82e6c50e56b2475dc
|
|
| BLAKE2b-256 |
e7334acffde31f27ddaa8abd04df13e27e446b3b0671e04761887388bda8c087
|
Provenance
The following attestation bundles were made for besteffort-0.1.1-py3-none-any.whl:
Publisher:
release.yml on grantjenks/python-besteffort
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
besteffort-0.1.1-py3-none-any.whl -
Subject digest:
a3169ba2bba9f7eabc8678a563941d424ef873c1caa1c2719c94b83f8e08b18e - Sigstore transparency entry: 662110789
- Sigstore integration time:
-
Permalink:
grantjenks/python-besteffort@a9475b2eedfbb89a9f92a3d79443d59a8b23cf23 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/grantjenks
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a9475b2eedfbb89a9f92a3d79443d59a8b23cf23 -
Trigger Event:
push
-
Statement type: