Repository navigation
Keep the registered type keys across BaseFBAPkg.clear, and add a media accessor - #171
Merged
Merged
Conversation
…a accessor
1. clear() replaced self.variables and self.constraints with fresh empty dicts,
which dropped the per-type keys registered in __init__. Every build method
indexes those keys directly -- build_variable does
len(self.variables[obj_type]) and build_constraint does
`if name in self.constraints[obj_type]` -- so rebuilding anything after a
clear() raised KeyError, measured as KeyError('tf') on TotalFluxPkg, and no
BaseFBAPkg subclass could be reused once cleared. Each obj_type dict is now
emptied in place, so the keys survive and the package stays usable. Note
there is no in-tree caller of clear(), so this is a latent defect in a public
method rather than an observed field failure.
2. get_current_media() lets any package ask what media is in force without
reaching into the package manager. It is deliberately NOT named
current_media: KBaseMediaPkg is itself a BaseFBAPkg and holds the media in an
instance attribute of that name, which would shadow the method on that one
subclass, so KBaseMediaPkg().current_media() would raise
"TypeError: 'NoneType' object is not callable". A test pins that.
Tests: tests/fbapkg/test_basefbapkg.py, 4 cases on an inline two-reaction model
-- type keys surviving a clear and the package rebuilding afterwards, clear
still detaching its objects from the solver, the accessor resolving to
KBaseMediaPkg's attribute, and the accessor staying callable on the media
package itself. 3 of the 4 fail against the unpatched tree; the solver-detach
case passes on both sides by design, because it pins the behaviour this change
must preserve rather than the behaviour it adds.
Full suite: 85 passed. pre-commit run --all-files is clean apart from the
pre-existing executable-shebang hook on upstream file modes.
Co-Authored-By: Claude Opus 5 <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.
Two small, independent changes to
BaseFBAPkg.1.
clear()destroyed the type keys the build methods depend on__init__registers one dict per declared variable and constraint type:and every build method indexes those keys directly —
build_variabledoeslen(self.variables[obj_type]),build_constraintdoesif name in self.constraints[obj_type]. Butclear()threw the whole mapping away:so rebuilding anything after a
clear()raisedKeyError. Measured onTotalFluxPkg:No
BaseFBAPkgsubclass could be reused once cleared. Eachobj_typedict is now emptied in place, so the keys survive and the package stays usable.Worth flagging: there is no in-tree caller of
clear(), so this is a latent defect in a public method rather than something that has bitten a shipped code path. It is a two-line change and the method is part of the base-class API, so it seemed worth fixing rather than leaving.2.
get_current_media()A small accessor so any package can ask what media is in force without reaching into the package manager itself.
It is deliberately not named
current_media, which would be the obvious name:KBaseMediaPkgis itself aBaseFBAPkgand stores the media in an instance attribute of exactly that name (self.current_media = Nonein its__init__). A method of the same name on the base class is shadowed by that attribute on that one subclass, soKBaseMediaPkg(...).current_media()would raiseTypeError: 'NoneType' object is not callablewhile the same call worked on every other package. One of the tests pins that the accessor stays callable on the media package too.A
@propertynamedcurrent_mediaon the base, withKBaseMediaPkgrenaming its attribute, is the cleaner end state, but it changes that subclass and anything reading its attribute, so this PR takes the narrower accessor. Request the property form here if you would rather have it and I will make that change instead.Tests
tests/fbapkg/test_basefbapkg.py, 4 cases on an inline two-reaction model:clear()and the package rebuilds afterwards;clear()still detaches its objects from the solver;KBaseMediaPkg's attribute;3 of the 4 fail against the unpatched tree. The solver-detach case passes on both sides by design — it pins the behaviour this change must preserve, not the behaviour it adds, and I would rather say so than imply it discriminates.
Full suite: 85 passed.
pre-commit run --all-filesis clean apart from the pre-existingcheck that executables have shebangshook on upstream file modes, which is unrelated to this change.Not in this PR
Our fork also has a change to
BaseFBAPkg.__init__that scansmodel.solverand adopts every constraint and variable whose name suffix matches a registered type. That one is a behaviour change affecting every subclass, with a real collision hazard, so it is going up separately as an opt-in proposal rather than riding along with these two.🤖 Generated with Claude Code