Skip to main content

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:besteffort registers the meta-path finder so from besteffort import yourmodule works without any additional steps. Call :func:besteffort.install manually if you need to reinstall it.
  • Function decorator – use @besteffort to 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 besteffort will 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

besteffort-0.1.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

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

besteffort-0.1.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

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

Hashes for besteffort-0.1.1.tar.gz
Algorithm Hash digest
SHA256 074a5d8233d99d106794b4bdfbc5afd209e7e51dea8807d280946b386f7e8408
MD5 b4fb5c16e4f8c646d208141eb3ba4816
BLAKE2b-256 30742a51bdc0696af816f8a48b2e1220a1b6d1568522d769072db3e5730f8d24

See more details on using hashes here.

Provenance

The following attestation bundles were made for besteffort-0.1.1.tar.gz:

Publisher: release.yml on grantjenks/python-besteffort

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for besteffort-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a3169ba2bba9f7eabc8678a563941d424ef873c1caa1c2719c94b83f8e08b18e
MD5 455ef67f28a178a82e6c50e56b2475dc
BLAKE2b-256 e7334acffde31f27ddaa8abd04df13e27e446b3b0671e04761887388bda8c087

See more details on using hashes here.

Provenance

The following attestation bundles were made for besteffort-0.1.1-py3-none-any.whl:

Publisher: release.yml on grantjenks/python-besteffort

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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