Narrow exception catch in _derive_from_expr to stop masking errors#186
Merged
amc-corey-cox merged 6 commits intomainfrom Apr 1, 2026
Merged
Narrow exception catch in _derive_from_expr to stop masking errors#186amc-corey-cox merged 6 commits intomainfrom
amc-corey-cox merged 6 commits intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR narrows exception handling in ObjectTransformer._derive_from_expr so restricted evaluation no longer masks genuine runtime/data errors behind a generic “safe subset” message, improving curator-facing diagnostics while preserving the unrestricted asteval fallback behavior.
Changes:
- Update
_derive_from_exprto only fall back toastevalwhenunrestricted_eval=True, while letting real errors propagate in restricted mode to be wrapped asTransformationErrorwith context. - Remove the misleading “Expression not in safe subset” wrapper behavior in restricted mode.
- Adjust the restricted-mode test to use an expression that is reliably rejected by
simpleevaland assert on the real underlying error message.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/linkml_map/transformer/object_transformer.py |
Restricts exception catching in expression derivation so restricted mode preserves real error messages and unrestricted mode continues to fall back to asteval. |
tests/test_transformer/test_object_transformer.py |
Updates restricted-mode test to reflect new error propagation behavior and to use a consistently-rejected expression. |
78a7791 to
9d23bc7
Compare
…tion Stop masking real errors as "Expression not in safe subset" — only catch InvalidExpression from simpleeval. Real errors propagate as proper TransformationErrors with full context. Arithmetic operators now coerce numeric strings to float and return None (with a warning) for non-coercible values. This enables case() guards with is_numeric() since case() evaluates eagerly: case((is_numeric(x), x * 2.54), (True, None)) Closes #185 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
9d23bc7 to
e7b201f
Compare
- is_numeric(True) now returns False (consistent with other coercion helpers) - Add ValueError to unrestricted eval fallback catch - Clarify _null_propagating docstring re: + on strings - Loosen lambda test match to case-insensitive "lambda" - Fix range_override tests to use restricted mode for error assertions Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Reduces duplication: _try_numeric handles coercion to numeric with bool exclusion, _is_numeric wraps it as a boolean check. Both _null_propagating and _is_numeric now use the same coercion logic. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #185
Fixes the broad
except Exceptionin_derive_from_exprthat masked real runtime errors as "Expression not in safe subset." Also adds arithmetic coercion for numeric strings and anis_numeric()expression function so curators can handle messy tabular data.Before
After
Changes
Exception handling
InvalidExpression(simpleeval safety violations) is caught. Real runtime errors (TypeError,ValueError,ZeroDivisionError) propagate asTransformationErrorwith full context (class, slot, populated_from, source row).InvalidExpression,TypeError, andValueErrorfall through to the asteval fallback (preserving existing behavior for multi-line expressions).Arithmetic coercion
"100" / "50"→2.0"abc" * 10→None(logged)case()evaluates eagerly — without it, curators can't guard expressions withis_numeric()since the guarded branch would crash beforecase()checks the conditionNew expression function
is_numeric(x): Returns True ifxcan be converted to float, False otherwise. Enables guard patterns:Shared helper
_try_numeric(value): Extracted as a top-level helper for numeric coercion with bool exclusion. Used by both_is_numericand_null_propagating.Test plan
is_numeric()guard pattern works withcase()🤖 Generated with Claude Code