Skip to content

Commit 4652ed8

Browse files
authored
Merge pull request #1261 from PyAutoLabs/feature/info-exclude-identifier-fields
fix: exclude __exclude_identifier_fields__ attrs from model.info
2 parents 6610dd2 + 6c03fc7 commit 4652ed8

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

autofit/mapper/prior_model/abstract.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,26 @@ def info(self) -> str:
17801780
"""
17811781
formatter = TextFormatter(line_length=info_whitespace())
17821782

1783+
def _excluded_by_parent(path):
1784+
# Honor each parent's `__exclude_identifier_fields__` here so attributes
1785+
# that classes have already declared "not part of the model's identity"
1786+
# (e.g. JAX pytree tokens) do not leak into the human-readable model.info.
1787+
if not path:
1788+
return False
1789+
parent = self
1790+
for step in path[:-1]:
1791+
try:
1792+
if isinstance(step, int):
1793+
parent = parent[step]
1794+
elif isinstance(parent, dict):
1795+
parent = parent[step]
1796+
else:
1797+
parent = parent.__dict__[step]
1798+
except (AttributeError, KeyError, IndexError, TypeError):
1799+
return False
1800+
excluded = getattr(type(parent), "__exclude_identifier_fields__", ())
1801+
return path[-1] in excluded
1802+
17831803
for t in find_groups(
17841804
[
17851805
t
@@ -1788,6 +1808,7 @@ def info(self) -> str:
17881808
ignore_children=True,
17891809
)
17901810
if t[0][-1] not in ("id", "item_number")
1811+
and not _excluded_by_parent(t[0])
17911812
],
17921813
limit=1,
17931814
):

test_autofit/mapper/test_constant.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,22 @@ def test_is_instance():
185185
constant = af.Constant(1.0)
186186

187187
assert isinstance(constant, float)
188+
189+
190+
class _ClassWithExcludedField:
191+
# Mirrors the LightProfileLinear pattern: an internal attribute set in
192+
# __init__ that is declared not part of the model identity. The info
193+
# property must respect that contract and suppress `token`.
194+
__exclude_identifier_fields__ = ("token",)
195+
196+
def __init__(self, value: float = 0.5):
197+
self.value = value
198+
self.token = 7
199+
200+
201+
def test_info_honors_exclude_identifier_fields():
202+
model = af.Collection(thing=_ClassWithExcludedField())
203+
204+
info = model.info
205+
assert "token" not in info
206+
assert "value" in info

0 commit comments

Comments
 (0)