error
compression_safeguards.utils.error
Exception and warning types and helpers to raise exceptions with context.
Exceptions raised in this package carry ErrorContext, which
can be accessed through the ErrorContextMixin.
Classes:
-
ContextLayer–Context layer abstract base class.
-
ErrorContext–Context in which an error was raised.
-
ctx–Singleton error context type with which additional context scopes can be entered.
-
ErrorContextMixin–Mixin for exceptions that have
ErrorContext. -
SafeguardTypeContextLayer–Safeguard type context layer.
-
ParameterContextLayer–Parameter context layer.
-
LateBoundParameterContextLayer–Late-bound parameter context layer.
-
IndexContextLayer–Index context layer.
-
TypeCheckError–TypeErrorthat is raised when a value fails a type check. -
TypeSetError–TypeErrorthat is raised when a type is not in a type set. -
LateBoundParameterResolutionError–KeyErrorthat is raised when late-bound parameter resolution fails because one or more parameters are missing or extraneous. -
SafeguardsSafetyBug–RuntimeErrorthat is raised when a fatal safety bug occurs. -
QuantityOfInterestRuntimeWarning–RuntimeWarningthat is raised when a recoverable quantity of interest sanity check fails.
Functions:
-
lookup_enum_or_raise–Look up and return the
enummember with the givennameor raise anerror, with context.
Ei
module-attribute
Any enum type (invariant).
- API Reference compression_safeguards utils error lookup_enum_or_raise
- API Reference compression_safeguards utils error lookup_enum_or_raise
ContextLayer
Bases: ABC
Context layer abstract base class.
ErrorContext
ErrorContext(*context: ContextLayer)
Context in which an error was raised.
The context can be added to an exception using the
err | ErrorContext(...) syntax.
| Parameters: |
|
|---|
layers
property
layers: tuple[ContextLayer, ...]
The layers of the context, from outermost to innermost.
ctx
Singleton error context type with which additional context scopes can be entered.
An exception can be prepared for receiving context using the err | ctx
syntax.
Methods:
-
layer–Context manager that adds one
layerof context to any exception that -
safeguard–Context manager that adds one layer of context, the type of a
-
safeguardty–Context manager that adds one layer of context, the
safeguardtype, -
parameter–Context manager that adds one layer of context, the
nameof a -
late_bound_parameter–Context manager that adds one layer of context, the
nameof a -
index–Context manager that adds one layer of context, an
index, to any
layer
staticmethod
layer(layer: ContextLayer)
Context manager that adds one layer of context to any exception that
is raised within.
| Parameters: |
|
|---|
safeguard
staticmethod
safeguard(
safeguard: Safeguard,
) -> AbstractContextManager[None]
Context manager that adds one layer of context, the type of a
safeguard, to any exception that is raised within.
The added context layer will be a
SafeguardTypeContextLayer.
| Parameters: |
|
|---|
safeguardty
staticmethod
safeguardty(
safeguard: type[Safeguard],
) -> AbstractContextManager[None]
Context manager that adds one layer of context, the safeguard type,
to any exception that is raised within.
The added context layer will be a
SafeguardTypeContextLayer.
| Parameters: |
|---|
parameter
staticmethod
parameter(name: str) -> AbstractContextManager[None]
Context manager that adds one layer of context, the name of a
parameter, to any exception that is raised within.
The added context layer will be a
ParameterContextLayer.
| Parameters: |
|
|---|
late_bound_parameter
staticmethod
late_bound_parameter(
name: Parameter,
) -> AbstractContextManager[None]
Context manager that adds one layer of context, the name of a
late-bound parameter, to any exception that is raised within.
The added context layer will be a
LateBoundParameterContextLayer.
| Parameters: |
|
|---|
index
staticmethod
index(index: int) -> AbstractContextManager[None]
Context manager that adds one layer of context, an index, to any
exception that is raised within.
The added context layer will be an
IndexContextLayer.
| Parameters: |
|
|---|
ErrorContextMixin
Mixin for exceptions that have ErrorContext.
The context can be accessed after checking
isinstance(err, ErrorContextMixin).
SafeguardTypeContextLayer
Bases: ContextLayer
Safeguard type context layer.
| Parameters: |
|---|
ParameterContextLayer
ParameterContextLayer(parameter: str)
Bases: ContextLayer
Parameter context layer.
| Parameters: |
|
|---|
LateBoundParameterContextLayer
LateBoundParameterContextLayer(parameter: Parameter)
Bases: ContextLayer
Late-bound parameter context layer.
| Parameters: |
|
|---|
IndexContextLayer
IndexContextLayer(index: int)
Bases: ContextLayer
Index context layer.
| Parameters: |
|
|---|
TypeCheckError
Bases: TypeError
TypeError that is raised when a value fails a type check.
| Parameters: |
|---|
Methods:
-
check_instance_or_raise–Check
isinstance(obj, expected)or raise a type check error, with
check_instance_or_raise
classmethod
Check isinstance(obj, expected) or raise a type check error, with
context.
| Parameters: |
|---|
| Raises: |
|
|---|
TypeSetError
Bases: TypeError
TypeError that is raised when a type is not in a type set.
| Parameters: |
|---|
Methods:
-
check_or_raise–Check
issubclass(ty, expected)or raise a type set error, with -
check_dtype_or_raise–Check if
dtypeis in the set ofsupporteddata types or raise a
check_or_raise
classmethod
Check issubclass(ty, expected) or raise a type set error, with
context.
| Parameters: |
|---|
| Raises: |
|
|---|
check_dtype_or_raise
classmethod
Check if dtype is in the set of supported data types or raise a
type set error, with context.
| Parameters: |
|---|
| Raises: |
|
|---|
LateBoundParameterResolutionError
Bases: KeyError
KeyError that is raised when late-bound parameter resolution fails because one or more parameters are missing or extraneous.
| Parameters: |
|---|
Methods:
-
check_or_raise–Check if the
expectedset of late-bound parameters matches the
check_or_raise
staticmethod
Check if the expected set of late-bound parameters matches the
provided set or raise a late-bound parameter resolution error, with
context.
| Parameters: |
|---|
| Raises: |
|
|---|
missing
property
The missing (expected but not provided) set of late-bound parameters.
SafeguardsSafetyBug
SafeguardsSafetyBug(message: str)
Bases: RuntimeError
RuntimeError that is raised when a fatal safety bug occurs.
A fatal safety bug occurs when the safeguards are unable to provide the requested safety requirement.
By raising this error, the compression-safeguards avoid violating the
safety requirements.
When this error is raised, it is a fatal bug in the implementation of the
compression-safeguards, which should be reported at
https://github.com/juntyr/compression-safeguards/issues.
| Parameters: |
|
|---|
QuantityOfInterestRuntimeWarning
QuantityOfInterestRuntimeWarning(message: str)
Bases: RuntimeWarning
RuntimeWarning that is raised when a recoverable quantity of interest sanity check fails.
A quantity of interest safeguard raises this warning if it fails at providing the requested safety requirement using regular means but is still able to recover and uphold the requirement in the end.
When this warning is raised, it is a non-fatal bug in the implementation of
the compression-safeguards, which should be reported at
https://github.com/juntyr/compression-safeguards/issues.
| Parameters: |
|
|---|
lookup_enum_or_raise
lookup_enum_or_raise(
enum: type[Ei],
name: str,
error: type[Exception] = ValueError,
) -> Ei | Never
Look up and return the enum member with the given name or raise an error, with context.
| Parameters: |
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|