model: replace assert-based validation with raise ValueError (works under python -O) - #516
Merged
erikbosch merged 2 commits intoMay 12, 2026
Conversation
VSSData / VSSDataDatatype / VSSUnit pydantic validators in model.py used `assert COND, "msg"` to enforce ~32 invariants. Python's `-O` optimisation flag strips assert statements at compile time, so any deployment running `python -O` (some packaging pipelines, performance- tuned environments) silently skipped all VSS spec validation. The parser would then accept invalid specs as valid. This is a documented anti-pattern in pydantic's own docs: https://docs.pydantic.dev/latest/concepts/validators/ Replace each `assert COND, "msg"` with the equivalent `if not COND: raise ValueError("msg")` form. Pydantic wraps the ValueError into a ValidationError automatically, so externally- observable behaviour is identical in normal mode. The difference: the new form is preserved under `python -O`, restoring validation in optimised builds. No test changes needed: the existing parametric suite in tests/test_model.py already exercises 50+ invalid-input cases and expects ValidationError. Those tests would have failed under `python -O` before this change; now they pass in both modes. Signed-off-by: Matt Jones <47545907+SoundMatt@users.noreply.github.com>
SoundMatt
force-pushed
the
fix/model-validators-asserts-to-raise
branch
from
May 5, 2026 17:26
32ac507 to
be01a6e
Compare
This was referenced May 5, 2026
sschleemilch
approved these changes
May 5, 2026
sschleemilch
left a comment
Collaborator
There was a problem hiding this comment.
Yeah, that's a valid one
Six tests in tests/vspec/test_description_error/test_description_error.py
and tests/vspec/test_overlay/test_overlay.py asserted on the literal
string `'type': 'assertion_error'` in the captured log output.
Pydantic reports the error type field differently depending on what the
validator raises:
- `assert COND, "msg"` raises AssertionError, which pydantic surfaces
as `'type': 'assertion_error'`.
- `if not COND: raise ValueError("msg")` raises ValueError, which
pydantic surfaces as `'type': 'value_error'`.
The previous commit replaced asserts with raises across model.py;
update the test expectations accordingly.
No production behaviour change — the affected tests still verify that
invalid input is rejected with a ValidationError carrying the same
message; only the type-tag string differs.
Signed-off-by: Matt Jones <47545907+SoundMatt@users.noreply.github.com>
Contributor
Author
|
CI caught a regression: six tests in |
erikbosch
approved these changes
May 6, 2026
Collaborator
|
MoM:
|
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.
Problem
The validators in
src/vss_tools/model.pyuseassert COND, "msg"to enforce ~32 invariants on
VSSData,VSSDataDatatype, andVSSUnitfields. Pydantic's own documentation explicitly warnsagainst this pattern: Python's
-Ooptimisation flag disablesasserts, allowing invalid data to pass through.
Anyone running vss-tools under
python -O(some packagingpipelines, performance-tuned environments) gets all VSS spec
validation silently bypassed. The parser accepts invalid specs as
valid, and downstream exporters then act on garbage data.
Reproduction
Fix
Mechanically replace each:
with:
Pydantic wraps a
ValueErrorraised inside a validator as aValidationErrorautomatically, so externally-observable behaviouris identical in normal mode. The new form is preserved under
python -O.Test
tests/test_model.pyalready parametrises 50+ invalid-input casesthat expect
pydantic.ValidationError. All continue to pass afterthis PR in normal mode. They would have failed under
python -Obefore this change; now they pass in both modes.
I considered adding an explicit
python -Ojob to CI to catchfuture regressions of this anti-pattern, but kept it out of this PR
to stay focused. Happy to follow up if maintainers want.
Notes
assertstatements replaced acrossVSSData,VSSUnit, andVSSDataDatatypevalidators.not (X and Y)form; those becameif X and Yfor natural inversion. Equivalent semantics, idiomatic Python.
if not self.default:incheck_default_min_max) is intentionally left untouched — it'sa separate validation bug that deserves a focused PR (signal
with
default=0, min=10silently passes today).