expect-exception
A decorator and context manager to run code that you expect (and want!) to raise an exception.
This module is meant to "reverse" the usage of try/except, for when you write
code where the exception is the "good" branch. It raises an
ExceptionNotRaisedError when the expected exception type is not raised in the
wrapped code, either because it finished executing or because another,
unexpected exception was raised.
For example, you can use expect_exception to replace the following code:
def upload_new_file(filename, content)
try:
some_api.fetch_file(filename)
except FileNotFoundError:
# We don't want to override existing files on SomeService, but SomeApi
# doesn't have a method to check if a file exists!
pass
some_api.upload_file(filename, content)
With this:
def upload_new_file(filename, content)
with expect_exception(FileNotFoundError):
# We don't want to override existing files on SomeService, but SomeApi
# doesn't have a method to check if a file exists!
some_api.fetch_file(filename)
some_api.upload_file(filename, content)
Alternatively, you can use expect_exception as a decorator for a helper
method:
# some_api.py
class SomeApi(...):
...
def fetch_file(self, filename):
...
@expect_exception(FileNotFoundError):
def ensure_file_missing(self, filename):
self.fetch_file(filename)
# ---
def upload_new_file(filename, content):
some_api.ensure_file_missing(filename)
some_api.upload_file(filename, content)
Usage
from expect_exception import expect_exception
# Use either as a @decorator or as a `with` statement context.
expect_exception(
SomeExceptionType[, SomeExceptionType, ...,
wrap_unexpected_exception: bool])
Arguments:
*exception_types: Type[BaseException](positional arguments): one or more exception types (class objects) that are expected to be raised in this context. Exceptions that inherit from any of those listed here will also be caught.wrap_unexpected_exception: bool(keyword only argument): True to wrap any unexpected exception in anExceptionNotRaisedError; False to bubble up the unexpected exception. Defaults to True.
Metadata
Release files for expect-exception 1.0.2
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 |
|---|---|---|---|---|
| expect-exception-1.0.2.tar.gz | 4.2 kB | Without provenance | Details for expect-exception-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| expect_exception-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for expect_exception-1.0.2-py3-none-any.whl |
Total release size: 10.4 kB
Release files / expect-exception-1.0.2.tar.gz
| Download URL | expect-exception-1.0.2.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
123ff0c60328c62dbe8716823dbdd787f94a35433d0524496352a8aedca4d4f7
|
|
BLAKE2b-256 checksum How to use checksums |
ff119e7bc6766e4b849c9d46e6df54b746dc8ada627adf54dbda51f12bad8050
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
|
Release files / expect_exception-1.0.2-py3-none-any.whl
| Download URL | expect_exception-1.0.2-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
83c45cbc5873d1068dfff8ac3b256db9fe9dd955fa5740b97c47548478cc61cf
|
|
BLAKE2b-256 checksum How to use checksums |
9c7775474417cff221d04108ed2f88b4086ce5032047c3b24036b95f5ca52fe2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
|
Security for expect-exception 1.0.2
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.