diff --git a/autofit/mapper/prior_model/abstract.py b/autofit/mapper/prior_model/abstract.py index f36ddbb28..cf4751481 100644 --- a/autofit/mapper/prior_model/abstract.py +++ b/autofit/mapper/prior_model/abstract.py @@ -1780,6 +1780,26 @@ def info(self) -> str: """ formatter = TextFormatter(line_length=info_whitespace()) + def _excluded_by_parent(path): + # Honor each parent's `__exclude_identifier_fields__` here so attributes + # that classes have already declared "not part of the model's identity" + # (e.g. JAX pytree tokens) do not leak into the human-readable model.info. + if not path: + return False + parent = self + for step in path[:-1]: + try: + if isinstance(step, int): + parent = parent[step] + elif isinstance(parent, dict): + parent = parent[step] + else: + parent = parent.__dict__[step] + except (AttributeError, KeyError, IndexError, TypeError): + return False + excluded = getattr(type(parent), "__exclude_identifier_fields__", ()) + return path[-1] in excluded + for t in find_groups( [ t @@ -1788,6 +1808,7 @@ def info(self) -> str: ignore_children=True, ) if t[0][-1] not in ("id", "item_number") + and not _excluded_by_parent(t[0]) ], limit=1, ): diff --git a/test_autofit/mapper/test_constant.py b/test_autofit/mapper/test_constant.py index 345ede14a..ac97120cc 100644 --- a/test_autofit/mapper/test_constant.py +++ b/test_autofit/mapper/test_constant.py @@ -185,3 +185,22 @@ def test_is_instance(): constant = af.Constant(1.0) assert isinstance(constant, float) + + +class _ClassWithExcludedField: + # Mirrors the LightProfileLinear pattern: an internal attribute set in + # __init__ that is declared not part of the model identity. The info + # property must respect that contract and suppress `token`. + __exclude_identifier_fields__ = ("token",) + + def __init__(self, value: float = 0.5): + self.value = value + self.token = 7 + + +def test_info_honors_exclude_identifier_fields(): + model = af.Collection(thing=_ClassWithExcludedField()) + + info = model.info + assert "token" not in info + assert "value" in info