Skip to content

Keep the registered type keys across BaseFBAPkg.clear, and add a media accessor - #171

Merged
Fxe merged 1 commit into
ModelSEED:devfrom
cshenry:fix-basefbapkg-clear-type-keys
Nov 18, 2026
Merged

Fxe merged 1 commit into
ModelSEED:devfrom
cshenry:fix-basefbapkg-clear-type-keys

Conversation

@cshenry

@cshenry cshenry commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

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:

for type in variable_types:
    self.variables[type] = dict()

and every build method indexes those keys directly — build_variable does len(self.variables[obj_type]), build_constraint does if name in self.constraints[obj_type]. But clear() threw the whole mapping away:

self.variables = {}
self.constraints = {}

so rebuilding anything after a clear() raised KeyError. Measured on TotalFluxPkg:

after build: variables keys = ['tf'] | tf count = 2
after clear: variables = {} | constraints = {}
REBUILD FAILED: KeyError KeyError('tf')

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.

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: KBaseMediaPkg is itself a BaseFBAPkg and stores the media in an instance attribute of exactly that name (self.current_media = None in its __init__). A method of the same name on the base class is shadowed by that attribute on that one subclass, so KBaseMediaPkg(...).current_media() would raise TypeError: 'NoneType' object is not callable while the same call worked on every other package. One of the tests pins that the accessor stays callable on the media package too.

A @property named current_media on the base, with KBaseMediaPkg renaming 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:

  • the type keys survive a clear() and the package rebuilds afterwards;
  • clear() still detaches its objects from the solver;
  • the accessor resolves to KBaseMediaPkg's attribute;
  • the accessor stays 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 — 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-files is clean apart from the pre-existing check that executables have shebangs hook on upstream file modes, which is unrelated to this change.

Not in this PR

Our fork also has a change to BaseFBAPkg.__init__ that scans model.solver and 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

…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>
@Fxe
Fxe merged commit f64e9dc into ModelSEED:dev Nov 18, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants