Repository navigation
Description
Overview
af.Model.from_instance(instance) copies every attribute in instance.__dict__ onto the model, including attributes __init__ derives from its arguments (ExternalShear's centre, Isothermal's slope). The model.json a search then writes is typed instance, and from_dict calls cls(**arguments), which raises TypeError: unexpected keyword argument. Aggregator.from_directory therefore fails before reading any result of a search whose model was anchored on a truth instance.
Plan
- Pass only constructor parameters from
from_instancetoModel, using the same signature inspectionModel.constructor_argument_namesalready relies on. - Keep all keys for classes whose
__init__accepts**kwargs; keep current behaviour when the signature cannot be inspected. - Regression tests: derived-attribute class round-trips through
dict()/from_dict;Aggregator.from_directoryloads amodel.jsonwritten from such a model.
Detailed implementation plan
Affected Repositories
- PyAutoFit (primary)
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| ./PyAutoFit | main | clean |
Suggested branch: claude/autofit-model-instance-roundtrip-erf439
Work classification: Library
Worktree root: n/a (web session clone)
Implementation Steps
autofit/mapper/prior_model/abstract.py,from_instance, generic-object branch: filterinstance.__dict__toinspect.getfullargspec(instance.__class__)args + kwonlyargs (minusself); skip the filter whenvarkwis set or inspection raisesTypeError.test_autofit/mapper/model/: regression test on a class with a derived attribute (set in__init__) and a@property; assert model keys,dict()["arguments"], andaf.Model.from_dict(model.dict())all succeed; assert a**kwargsclass keeps its extra keys.test_autofit/aggregator/test_from_directory.py: copy thesearch_outputfixture, overwritefiles/model.jsonwithfrom_instance(...).dict()of the derived-attribute class, assertAggregator.from_directoryyields one output whosemodelloads.pytest -n auto test_autofit/mapper test_autofit/aggregator, then the full suite.
Key Files
autofit/mapper/prior_model/abstract.py—from_instance(line ~1580), the fixautofit/mapper/prior_model/prior_model.py—Model.__init__/constructor_argument_names, the inspection pattern to mirrorautofit/mapper/model_object.py—from_dictinstancebranch that callscls(**arguments)test_autofit/aggregator/test_from_directory.py— fixture pattern for the aggregator test
Original Prompt
Click to expand starting prompt
af.Model.from_instance serialises derived attributes the class __init__ rejects, so the aggregator cannot deserialise model.json
Type: bug
Target: PyAutoFit
Repos:
- PyAutoFit
Difficulty: small
Autonomy: safe
Priority: high
Status: formalised
Consequence: judge
Witness:af.Model.from_dict(af.Model.from_instance(al.mp.ExternalShear(gamma_1=0.02, gamma_2=0.02)).dict())and the same foral.mp.Isothermal(...)round-trip without error, a regression test intest_autofit/mapperpins it on a class whose__init__rejects a derived attribute, and anAggregator.from_directoryover a search whose model was composed withfrom_instanceloads the result.
Review-minutes: 15
Unattended: ready
Filed: 2026-09-10
Symptom
Found 2026-09-10 while writing the autolens_workspace_test latent integration smoke
(autolens_workspace_test#315 / PR #316). Composing a model with
af.Model.from_instance(<profile instance>) copies derived attributes onto the model
that the class constructor does not accept, and the model.json the search then writes
cannot be deserialised, so Aggregator.from_directory raises before reading any result:
af.Model.from_dict(af.Model.from_instance(al.mp.ExternalShear(gamma_1=.02, gamma_2=.02)).dict())
-> TypeError: ExternalShear.__init__() got an unexpected keyword argument 'centre'
af.Model.from_instance(al.mp.Isothermal(...))
-> TypeError: ... unexpected keyword argument 'slope'
The serialised dict carries centre / ell_comps (ExternalShear) and slope
(Isothermal) as Constant / tuple arguments. af.Model(cls, ...) round-trips fine, and
al.lp.Sersic via from_instance also round-trips (no derived attribute clashes).
Blast radius
euclid_strong_lens_modeling_pipeline/tests/test_latent_run_level.pycomposes its
model withfrom_instance; any output written that way is unreadable by the
aggregator (the test never aggregates, so it passes).- Any workspace example that anchors a search on a truth instance via
from_instance
produces results the aggregator /AggregateCSVcannot open.
Do
from_instanceshould only carry attributes that are constructor parameters
(inspect the class signature, asModel.__init__already does for priors) — or
from_dictshould drop non-constructor keys with a warning. Prefer the former: the
writtenmodel.jsonshould be loadable as-is.- Regression test on a small class with a derived attribute (
@propertyor an
attribute set in__init__from other arguments) plus an aggregator round-trip.
The smoke composes its models with af.Model(cls, **fixed_values) as a workaround; the
script docstring records why.
Activity
Jammy2211 commentedon Sep 11, 2026
Library PR Created
pending-release)claude/autofit-model-instance-roundtrip-erf439, commit 1c152beAPI changes: none — internal.
from_instancenow passes only constructor parameters toModel(all keys kept for**kwargs/ uninspectable constructors); derived attributes are recomputed by__init__on rebuild.Tests: 5 new round-trip tests in
test_autofit/mapper/model/test_from_instance_roundtrip.pyand anAggregator.from_directoryregression intest_autofit/aggregator/test_from_directory.py, all failing without the fix. Full suite: 2575 passed, 43 skipped, 0 failed.Workspace impact: none (option iii) — no public symbol changed. Next:
/prmonce CI is green.Noted while validating, not touched here:
pytest -n autocannot run the suite becausetest_autofit/mapper/prior/test_prior_properties.pyembeds object addresses in parametrize ids, so xdist workers collect different ids. Reproduced on a clean tree; worth its own issue.Generated by Claude Code
Merge pull request #1609 from PyAutoLabs/claude/autofit-model-instanc…
Jammy2211 commentedon Sep 11, 2026
Shipped
pending-releasestays until the next PyPI release publishes it.AbstractPriorModel.from_instancenow passes only constructor parameters toModel(all attributes kept for**kwargs/ uninspectable constructors), so amodel.jsonwritten fromfrom_instanceround-trips throughfrom_dictand loads inAggregator.from_directory. No public API change.test_autofit/mapper/model/test_from_instance_roundtrip.py(5) andtest_autofit/aggregator/test_from_directory.py::test_model_written_from_instance_is_loaded, all failing without the fix.PyAutoMind/complete/2026/09/autofit-from-instance-roundtrip.md.Follow-up filed separately in PyAutoMind: the suite cannot run under
pytest -n autobecausetest_autofit/mapper/prior/test_prior_properties.pyembeds object addresses in its parametrize ids.Generated by Claude Code