Update BaseModelWithContext with extra=forbid and fix tests#13457
Update BaseModelWithContext with extra=forbid and fix tests#13457erlenlh wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR tightens Pydantic validation for ERT models by making BaseModelWithContextSupport forbid unknown fields, then adjusts affected run-model construction and unit tests to comply (issue #13444).
Changes:
- Configure
BaseModelWithContextSupportto useextra="forbid"(and update downstream call sites/tests impacted by stricter validation). - Update run-model serialization roundtrip tests to avoid validating computed/serialized-only fields.
- Adjust Everest/ManualUpdate run-model construction/config so instantiation works under
extra="forbid".
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
src/ert/base_model_context.py |
Makes context-enabled Pydantic base model forbid extra fields (and also enables arbitrary types). |
src/ert/run_models/everest_run_model.py |
Adds fields to EverestRunModelConfig to satisfy stricter model validation; introduces optimization_callback handling as a config field. |
src/ert/run_models/model_factory.py |
Stops passing observations into ManualUpdateConfig (now forbidden as an extra field). |
tests/ert/unit_tests/run_models/test_experiment_serialization.py |
Excludes experiment_type from dumped payload when re-validating runmodels (to avoid extra="forbid" failures). |
tests/ert/unit_tests/ensemble_evaluator/conftest.py |
Removes unsupported job_script argument from QueueConfig fixture construction. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #13457 +/- ##
==========================================
- Coverage 89.54% 89.50% -0.05%
==========================================
Files 464 464
Lines 32778 32783 +5
==========================================
- Hits 29351 29342 -9
- Misses 3427 3441 +14
Flags with carried forward coverage won't be shown. Click here to find out more.
|
74d18a9 to
e138a24
Compare
dda17fb to
dcf4875
Compare
| experiment_name: str | ||
| target_ensemble: str | ||
|
|
||
| controls: Annotated[list[ControlConfig], AfterValidator(unique_items)] |
There was a problem hiding this comment.
This is a bit puzzling. How this could have worked in the first place?
There was a problem hiding this comment.
Not sure! Like we discussed, the values are definately used/assigned further below in the return cls(.... Seems like they should also exist here
There was a problem hiding this comment.
@oyvindeide wrote:
No, controls are parameters, those are not being used, and should not be in the constructor (nor the cls), there is no self.controls
There was a problem hiding this comment.
Ah great, thanks for clearing that up!
There was a problem hiding this comment.
assuming this holds for the other additions i made as well? objective_names, objective_functions, optimization_callback
|
|
||
|
|
||
| class RunModel(RunModelConfig, ABC): | ||
| status_queue: queue.SimpleQueue[StatusEvents] = Field(exclude=True, repr=False) |
There was a problem hiding this comment.
this can go back to be just a regular class attribute as before.
There was a problem hiding this comment.
I think it needs to stay as a field. Mypy is complaining about it if we keep is at attribute with strict pydantic settings.
There was a problem hiding this comment.
What kind of events are consumed in status_queue? The downside is that this would do validation on each processing of a new event, which might just fine though.
| ert_templates=config.ert_templates, | ||
| shape_registry=config.shape_registry, | ||
| ) | ||
| return ManualUpdateEnIF(**runmodel_config.model_dump(), status_queue=status_queue) |
There was a problem hiding this comment.
this should be tested. At least if parameters are propagated correctly.
|
|
||
|
|
||
| class RunModelConfig(BaseModelWithContextSupport): | ||
| model_config = ConfigDict(arbitrary_types_allowed=True, extra="forbid") |
There was a problem hiding this comment.
let's check if this is required.
There was a problem hiding this comment.
It seems to be needed, due to adding "status_queue" as a field to the runmodel.
| hooked_workflows=config.hooked_workflows, | ||
| log_path=config.analysis_config.log_path, | ||
| ert_templates=config.ert_templates, | ||
| observations=config.observation_declarations, |
There was a problem hiding this comment.
let's double-check if obs are not needed here.
There was a problem hiding this comment.
As far as i can tell, only observations from prior is used. Checked with Håvard, and he agrees
dcf4875 to
85a3b11
Compare
85a3b11 to
08882b1
Compare



Issue
Resolves #13444
Approach
Add extra=forbid flag to BaseModelWithContextSupport. Deal with downstream effect, adding to models or removing from tests where it seems fit.
(Screenshot of new behavior in GUI if applicable)
git rebase -i main --exec 'just rapid-tests')When applicable