helpers
helpers
¶
Reusable helpers for writing PreflightCheck subclasses.
Translators between common failure modes and IssueCollector calls.
Every helper takes the issue code as an explicit parameter -- the caller
owns the code-to-failure mapping so each collector.error("code", ...)
sits next to the check that owns it.
Plugin authors import the helpers they want; they don't need to inherit a mixin or commit to an inheritance contract.
Example
from nemo_safe_synthesizer.preflight import helpers
class MyCheck(DataFrameCheck): def check(self, ctx, collector): torch = helpers.require_import( collector, "torch", code="no_torch", message="PyTorch is not installed.", ) if torch is None: return helpers.emit_on_raise( collector, lambda: some_validator(ctx.data), expect=ValueError, code="bad_data", )
Functions:
| Name | Description |
|---|---|
emit_on_raise |
Run |
require_import |
Import |
resolved_record_count |
Return |
emit_on_raise(collector, action, *, expect, code, severity='error')
¶
Run action; translate an expected raise into an issue.
If action raises one of expect, emit an issue on collector
with code and the exception's message, then return False.
Unexpected exceptions propagate to the orchestrator's crash handler
(surfacing as preflight.check_crash rather than silently swallowed).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collector
|
IssueCollector
|
Target for the translated issue on expected raises. |
required |
action
|
Callable[[], None]
|
Zero-arg callable; any return value is ignored. |
required |
expect
|
type[BaseException] | tuple[type[BaseException], ...]
|
Exception type or non-empty tuple of exception types to
translate. Passing an empty tuple is rejected because
|
required |
code
|
str
|
Issue code to emit on the collector. |
required |
severity
|
Severity
|
|
'error'
|
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
raised one of |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/nemo_safe_synthesizer/preflight/helpers.py
require_import(collector, module, *, code, message, severity='error')
¶
Import module or emit an issue and return None.
Useful for checks that depend on an optional runtime (e.g. torch)
and want to degrade to a deterministic issue rather than crash when
the dependency is missing.
Only ImportError (and its ModuleNotFoundError subclass) is
caught: a module whose top-level code raises some other exception
(for example, a broken install that raises RuntimeError during
import) is a genuine environment fault, so the exception propagates
to the orchestrator's crash handler and surfaces as
preflight.check_crash rather than being silently downgraded to
a "missing dependency" warning.
Source code in src/nemo_safe_synthesizer/preflight/helpers.py
resolved_record_count(ctx)
¶
Return num_input_records_to_sample once resolved to a concrete int.
num_input_records_to_sample may still carry a sentinel like "auto"
if AutoConfigResolver has not normalized it. Checks that only make
sense against a concrete count should short-circuit on None.