Fix #94: resolve instance cross-references to typed Python objects instead of raw dicts#95
Merged
Raphael-Gazzotti merged 4 commits intoJun 26, 2026
Conversation
…y instances store properties as dicts)
…w dicts instead of typed objects
Problem: Instances such as Accessibility.direct_virtual_open_access stored
properties like payment_models as raw {"@id": "..."} dicts rather than typed
objects (e.g. PaymentModelType.zero_cost_payment_model). This caused KeyError
in Collection.load() and broke round-trip serialisation.
Root cause: The builder treated {"@id": "..."} values in instance data as
opaque strings rather than resolving them to references to typed Python objects.
Fix (part 1): Resolve {"@id": "..."} values to typed Python object references
(e.g. PaymentModelType.zero_cost_payment_model) during code generation.
Fix (part 2): Implementing part 1 surfaced circular import problems when two
classes in the same directory have instances that reference each other (e.g.
ParcellationEntity <-> ParcellationEntityVersion in v4). Rather than generating
instance assignments inline in each class module, generate separate *_instances.py
files that can be imported independently, and later.
Class modules are now pure class definitions and trigger
instance loading via `from . import *_instances as _`. To avoid cyclic imports,
certain files are patched in a second phase.
Comment on lines
+78
to
+85
| def _instance_needs_iri(props): | ||
| """ | ||
| True if any property of props will be rendered as IRI(...) by the instances template, | ||
| i.e. is a string starting with 'http' in a property other than 'id'. | ||
| """ | ||
| return any( | ||
| isinstance(value, str) and value.startswith("http") for key, value in props.items() if key != "id" | ||
| ) |
Member
There was a problem hiding this comment.
This looks like a heuristic. Are we validating against the schema to ensure that an IRI is actually allowed here?
Member
Author
There was a problem hiding this comment.
that's a good point, but I think it's out of scope for this pull request (we were already doing this elsewhere). I've opened #99 to keep track of it.
The list branch of _resolve_links() raised KeyError when an item's identifier was absent from node_lookup, whereas the scalar branch already fell back to keeping the Link. Use node_lookup.get(item.identifier, item) in the list branch too, so unresolvable links are kept (and can be resolved later, e.g. against the KG) instead of crashing. This is needed by fairgraph's initialise_instances, which resolves the recast openMINDS library instances against an id->object lookup that need not contain every referenced id. Adds a regression test (test_issue0094_resolve_links_tolerates_missing_id).
…ef and docstring - Store PythonRef as separate class_name/attr_name components instead of a pre-joined string, removing the format-then-split round-trip in the cyclic-reference checks (_has_cyclic_ref, _collect_python_ref_classes). - Stop referencing the internal PythonRef class from the translator docstring.
Raphael-Gazzotti
approved these changes
Jun 26, 2026
0f13fc0
into
openMetadataInitiative:pipeline
3 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #94. Library instances such as
Accessibility.direct_virtual_open_accessstoredproperties like
payment_models(and similarlychannel,eligibility,form,process, and equivalents on other classes) as raw{"@id": "..."}dicts rather thanas references to the typed Python objects they point to (e.g.
PaymentModelType.zero_cost_payment_model). This brokevalidate(), and caused aKeyErrorinCollection.load()after a save/reload round trip, because the referencedinstance was never included in the saved collection.
Root cause
During code generation, the builder treated
{"@id": "..."}values found in instancedata as opaque strings and emitted them verbatim, instead of resolving them to references
to the corresponding generated class attributes.
Fix
Resolve
@idreferences to typed objects. Instance properties whose value is a{"@id": "..."}dict (or a list of such dicts) that matches the@idof another loadedinstance are now emitted as references to that instance's generated Python attribute
(e.g.
payment_models=[PaymentModelType.zero_cost_payment_model]) rather than as aliteral dict. References that don't resolve to a known instance (pre-existing dangling
@ids in the upstream data) fall back to the previous raw-dict behaviour.Handle mutually-referencing classes. Resolving references this way exposed circular
import problems wherever two classes reference each other's instances (e.g.
ParcellationEntityandParcellationEntityVersioninsands/atlas, v3/v4). Thegeneration of instance code was restructured so that such references no longer need to
be resolved at class-definition time: instances are first created with the properties
that don't create a cycle, and the cyclic ones are filled in afterwards, once every
class involved has been defined.
Test plan
pipeline/tests/test_regressions.py) reproducing thereported scenario: building a collection containing
Accessibilityinstances,validating, saving, and reloading, and asserting that cross-referenced properties
are typed objects rather than dicts.
pytest -v pipeline/testssuite passes against the rebuilt package, includingchecks that mutually-referencing instances (e.g.
ParcellationEntity/ParcellationEntityVersion) resolve to typed objects with no import errors.