Repository navigation
fix: Model.from_instance carries only constructor parameters (#1607) - #1609
Merged
Merged
Conversation
`AbstractPriorModel.from_instance` copied every entry of `instance.__dict__` onto the model, including attributes an `__init__` derives from its arguments (ExternalShear's `centre`, Isothermal's `slope`). `ModelObject.from_dict` passed them back to the constructor, which raised `TypeError`, so any `model.json` written from such a model could not be loaded by the aggregator. The generic-object branch now filters to the constructor's parameters (`inspect.getfullargspec` args + kwonlyargs), keeping every attribute when the constructor takes `**kwargs` or cannot be inspected. Derived attributes are recomputed by `__init__` on rebuild, so nothing is lost. Regression tests: derived-attribute, tuple-argument, `**kwargs` and nested round-trips through `dict()`/`from_dict`, and `Aggregator.from_directory` over a `model.json` written from `from_instance`. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CWGZiKQo2PhvsymabkUJJm
Jammy2211
deleted the
claude/autofit-model-instance-roundtrip-erf439
branch
September 11, 2026 01:50
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
AbstractPriorModel.from_instancecopied every entry ofinstance.__dict__onto the model, including attributes an__init__derives from its arguments (ExternalShear'scentre, Isothermal'sslope).ModelObject.dict()types a prior-free model asinstance, andfrom_dictpassed those keys back to the constructor, which raisedTypeError, so anymodel.jsonwritten from such a model could not be loaded byAggregator.from_directory.The generic-object branch of
from_instancenow keeps only the constructor's parameters (inspect.getfullargspecargs + kwonlyargs), the same inspectionModel.constructor_argument_namesalready uses. Every attribute is still kept when the constructor takes**kwargsor cannot be inspected. Derived attributes are recomputed by__init__on rebuild, so nothing is lost.Closes #1607.
API Changes
None — internal changes only.
from_instanceno longer attaches non-constructor attributes to the returnedModel; instances built from that model are unchanged because__init__recomputes them.Test Plan
test_autofit/mapper/model/test_from_instance_roundtrip.py: derived-attribute, tuple-argument,**kwargsand nested round-trips throughdict()/from_dict(5 tests; 4 fail without the fix, the**kwargsone pins preserved behaviour)test_autofit/aggregator/test_from_directory.py::test_model_written_from_instance_is_loaded:Aggregator.from_directoryloads amodel.jsonwritten fromfrom_instance(fails without the fix)Full API Changes (for automation & release notes)
Changed Behaviour
autofit.AbstractPriorModel.from_instance(instance, ...)— for a plain object, only attributes named intype(instance).__init__'s signature become model arguments; attributes derived inside__init__are dropped from the model (and recomputed on instantiation). Unchanged when the constructor accepts**kwargsor is not inspectable.Generated by the PyAutoLabs agent workflow.
🤖 Generated with Claude Code
https://claude.ai/code/session_01CWGZiKQo2PhvsymabkUJJm
Generated by Claude Code