Skip to content

fix: Model.from_instance carries only constructor parameters (#1607) - #1609

Merged
Jammy2211 merged 1 commit into
mainfrom
claude/autofit-model-instance-roundtrip-erf439
Sep 11, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
claude/autofit-model-instance-roundtrip-erf439

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

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.dict() types a prior-free model as instance, and from_dict passed those keys back to the constructor, which raised TypeError, so any model.json written from such a model could not be loaded by Aggregator.from_directory.

The generic-object branch of from_instance now keeps only the constructor's parameters (inspect.getfullargspec args + kwonlyargs), the same inspection Model.constructor_argument_names already uses. Every attribute is still kept when the constructor takes **kwargs or cannot be inspected. Derived attributes are recomputed by __init__ on rebuild, so nothing is lost.

Closes #1607.

API Changes

None — internal changes only. from_instance no longer attaches non-constructor attributes to the returned Model; 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, **kwargs and nested round-trips through dict() / from_dict (5 tests; 4 fail without the fix, the **kwargs one pins preserved behaviour)
  • test_autofit/aggregator/test_from_directory.py::test_model_written_from_instance_is_loaded: Aggregator.from_directory loads a model.json written from from_instance (fails without the fix)
  • Full suite: 2575 passed, 43 skipped, 0 failed
Full API Changes (for automation & release notes)

Changed Behaviour

  • autofit.AbstractPriorModel.from_instance(instance, ...) — for a plain object, only attributes named in type(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 **kwargs or is not inspectable.

Generated by the PyAutoLabs agent workflow.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CWGZiKQo2PhvsymabkUJJm


Generated by Claude Code

`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 Jammy2211 added the pending-release PR queued for the next release build label Sep 11, 2026 — with Claude
@Jammy2211
Jammy2211 merged commit e612e20 into main Sep 11, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the claude/autofit-model-instance-roundtrip-erf439 branch September 11, 2026 01:50
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 26, 2026
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.

fix: Model.from_instance carries attrs the constructor rejects

2 participants