Fix ParameterValues.to_json for create_from_bpx partials#5641
Open
rtimms wants to merge 2 commits into
Open
Conversation
create_from_bpx stores functional parameters as functools.partial objects with keyword arguments already bound (e.g. diffusivity binding D_ref/Ea/constant, OCP interpolants binding name/x/y). The partial branch of convert_function_to_symbolic_expression built its argument list from the full partial signature, which still lists those bound and defaulted parameters, and then called the partial positionally for every one of them, raising "got multiple values for argument 'D_ref'". Only create symbols for the parameters that still lack a value (no default and not variadic) — the true symbolic inputs. Bound kwargs and defaulted control flags (e.g. `constant`) keep their values. Also drop the dead get_name/get_args branch, which required an object type that never existed. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- Pass the traced symbols by keyword rather than positionally, so a partial whose bound argument is not the last positional parameter keeps its bound value instead of being displaced (robustness beyond the current bpx inputs). - Remove the redundant func_to_eval alias; call func directly. - Trim the fallback comment to the two-line convention. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5641 +/- ##
==========================================
+ Coverage 98.10% 98.11% +0.01%
==========================================
Files 339 339
Lines 32069 32061 -8
==========================================
- Hits 31460 31457 -3
+ Misses 609 604 -5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Description
pybamm.ParameterValues.create_from_bpx(...).to_json()raisedTypeError: ... got multiple values for argument 'D_ref'(and similar for OCP interpolants / exchange-current density).create_from_bpxstores several functional parameters asfunctools.partialobjects with keyword arguments already bound, e.g.:In
convert_function_to_symbolic_expression, the partial branch built its argument list from the full partial signature — which still lists the bound and defaulted parameters (D_ref,Ea,constant) — then called the partial positionally for every one of them. The already-bound kwargs then collided with the positional values.Fix
Only create symbolic
Parameters for the parameters that still lack a value (no default and not variadic) — the genuine symbolic inputs (sto,T, …). Bound kwargs and defaulted control flags (e.g.constant, which is used for Python-levelifbranching, not as a symbol) keep their values.Note the subtlety: filtering only on
func.keywordsis insufficient — a defaulted-but-unbound parameter (constantin the callable-diffusivity case) would still be turned into a symbol and shift positionally into a bound parameter's slot, re-triggering the same collision.Also removes the dead
get_name/get_argsbranch, which required an object exposingget_name(),get_args(), and.funcsimultaneously — a type that has never existed in the codebase or its history.Tests
TestConvertFunctionToSymbolicExpressionintest_serialisation.py— kwarg-bound partials returning scalars and interpolants, plus the defaulted-unbound-arg regression.test_bpx_to_json_roundtrip(function / constant / table forms) intest_bpx.py— fullcreate_from_bpx→to_json→from_jsonround-trip.🤖 Generated with Claude Code