Fix dynamic_object for key-based classes#177
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes dynamic_object() so it can populate identifier fields for inlined dicts when the target class uses key: true (not only identifier: true), avoiding crashes when neither is present. This unblocks transformations involving LinkML-Map’s own key-based datamodel classes.
Changes:
- Update
dynamic_object()to fall back toSchemaView.get_identifier_slot(..., use_key=True)and guard when no id/key slot exists. - Add a regression test using an inline schema with a key-based class (
key: true).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/linkml_map/utils/dynamic_object.py |
Adds key-based identifier fallback and avoids None dereference when no id/key slot exists. |
tests/test_utils/test_dynamic_object.py |
Adds coverage for dynamic objects built from dict-keyed collections using key: true. |
Comments suppressed due to low confidence (1)
tests/test_utils/test_dynamic_object.py:56
- The test asserts
valueforalphabut not forbeta, so a regression affecting only subsequent entries could slip through. Consider also assertingdynobj.items["beta"].value == "two"for completeness.
data = {"items": {"alpha": {"value": "one"}, "beta": {"value": "two"}}}
dynobj = dynamic_object(data, sv, "Container")
assert type(dynobj).__name__ == "Container"
assert isinstance(dynobj.items, dict)
assert type(dynobj.items["alpha"]).__name__ == "Item"
assert dynobj.items["alpha"].name == "alpha"
assert dynobj.items["alpha"].value == "one"
assert dynobj.items["beta"].name == "beta"
…asses Fixes #169. `get_identifier_slot()` returns None for classes that use `key: true` instead of `identifier: true`. Now falls back to `use_key=True` lookup, and guards against None when neither is present. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
fd4f05c to
c7a24fd
Compare
Add test for classes with neither identifier nor key to cover the id_slot None guard. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
csiege
left a comment
There was a problem hiding this comment.
Review: Approve
Clean, well-scoped fix. The production code change is 4 lines and does exactly what it should:
use_key=True— theSchemaView.get_identifier_slot()API already supports falling back tokey: trueslots; the old call just wasn't using it.if id_slot is not None:guard — correct behavior when a class has neitheridentifiernorkey(the newNO_ID_SCHEMAtest covers exactly this case). The old code would have crashed withAttributeError: 'NoneType' object has no attribute 'name'.
Both new tests are meaningful: one exercises key: true, one exercises the no-id/no-key path. The existing test_dynamic_object continues to cover the identifier: true path.
Our production bdchm schema uses identifier: true on one class, so this bug was not triggered by our pipeline today — but the guard is the right defensive behavior regardless. 7/7 CI checks pass.
Summary
dynamic_object.pydoes not handle key-based (vs identifier-based) classes #169 —dynamic_object.pyfailed when processing classes that usekey: trueinstead ofidentifier: trueget_identifier_slot()now falls back touse_key=True, and guards againstNonewhen neither is presentTest plan
test_dynamic_object_key_based_classwith inline schema usingkey: truetest_dynamic_objectstill passes (identifier-based)🤖 Generated with Claude Code