Fix load_custom_model when base class package is unavailable#5441
Merged
Fix load_custom_model when base class package is unavailable#5441
Conversation
Restore the pre-#5389 contract where `Serialise.load_custom_model` always produced a plain `BaseModel` when the recorded subclass isn't available in the loading environment. Previously it raised `ImportError`, making models authored in third-party packages unloadable by anyone without that package installed, even though the model is stored fully symbolically. Now the loader opportunistically re-imports the original class when possible (preserving subclass-specific Python behaviour) and otherwise warns and falls back to `BaseModel`. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Covers the common case of a custom model that subclasses (or is reparented to) the lithium-ion BaseModel: after save + load, the recorded base class is re-imported, the options are restored as BatteryModelOptions, and the default_geometry / default_spatial_methods still cover the particle domains — so Simulation can discretise without the caller passing any extra geometry/submesh/spatial_methods dicts. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5441 +/- ##
=======================================
Coverage 98.31% 98.31%
=======================================
Files 336 336
Lines 30700 30699 -1
=======================================
Hits 30183 30183
+ Misses 517 516 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Member
BradyPlanden
left a comment
There was a problem hiding this comment.
Looks good, could you also add a changelog entry
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
BradyPlanden
approved these changes
Apr 10, 2026
rtimms
added a commit
that referenced
this pull request
Apr 10, 2026
* Fall back to BaseModel when custom model base class can't be imported Restore the pre-#5389 contract where `Serialise.load_custom_model` always produced a plain `BaseModel` when the recorded subclass isn't available in the loading environment. Previously it raised `ImportError`, making models authored in third-party packages unloadable by anyone without that package installed, even though the model is stored fully symbolically. Now the loader opportunistically re-imports the original class when possible (preserving subclass-specific Python behaviour) and otherwise warns and falls back to `BaseModel`. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Test that li-ion BaseModel round-trips with options and defaults intact Covers the common case of a custom model that subclasses (or is reparented to) the lithium-ion BaseModel: after save + load, the recorded base class is re-imported, the options are restored as BatteryModelOptions, and the default_geometry / default_spatial_methods still cover the particle domains — so Simulation can discretise without the caller passing any extra geometry/submesh/spatial_methods dicts. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Remove verbose inline comments Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Update changelog for #5441 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> (cherry picked from commit 428a181)
This was referenced Apr 10, 2026
Closed
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
Serialise.load_custom_modelnow falls back topybamm.BaseModel(with a warning) when the recordedbase_classcan't be re-imported, instead of raisingImportError. This restores the pre-Model to json #5389 contract and makes models authored in third-party packages loadable by anyone, since the model content is stored fully symbolically."builtins.object"sentinel now lives in the top-level guard alongside""and"pybamm.BaseModel", removing a dead branch inside theexceptclause (importingbuiltinsnever raises).test_import_base_class_non_builtin_objectto assert the new warn-and-fallback behaviour, and simplified its setup to round-trip throughsave_custom_model+ a single JSON field edit rather than hand-rolling the model JSON.Context
Reported by a user loading a
HalfCellGITTModeldefined in package on a machine without that package installed.Before #5389,
load_custom_model(filename, battery_model=None)defaulted topybamm.BaseModel()and never tried to import anything. #5389 started storingf"{cls.__module__}.{cls.__name__}"andimportlib.import_module-ing it at load time, with no fallback for non-pybamm subclasses. This PR keeps the opportunistic re-import (so subclass behaviour still round-trips when the package is installed) but degrades gracefully when it isn't.Test plan
pytest tests/unit/test_serialisation/test_serialisation.py -k "custom_model or base_class"— 8 passed🤖 Generated with Claude Code