Type cardinality-one relationships as an assignable descriptor (#1064)#1067
Merged
Conversation
Parameterise RelatedNode, RelatedNodeSync, RelationshipManager and RelationshipManagerSync over the peer type (defaulting to InfrahubNode/ InfrahubNodeSync for backward compatibility) and have infrahubctl protocols emit the peer type for each relationship. Relationship traversal via .peer/ .peers/indexing now preserves the peer type instead of collapsing to the dynamic InfrahubNode. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Generated cardinality-one relationships now use a RelationshipAttribute descriptor that reads back as RelatedNode[Peer] but accepts assignment of an id string, an HFID, a peer node, or None — matching the runtime __setattr__ that wraps the value in a RelatedNode. Previously relationships were typed read-only as RelatedNode, so node.rel = "<id>" / node.rel = peer failed type checking. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Deploying infrahub-sdk-python with
|
| Latest commit: |
c5887f0
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://050bdb6d.infrahub-sdk-python.pages.dev |
| Branch Preview URL: | https://ihs-relationship-assignment.infrahub-sdk-python.pages.dev |
Codecov Report❌ Patch coverage is
@@ Coverage Diff @@
## develop #1067 +/- ##
===========================================
- Coverage 82.26% 81.82% -0.45%
===========================================
Files 135 135
Lines 11956 11684 -272
Branches 1785 1766 -19
===========================================
- Hits 9836 9560 -276
- Misses 1573 1575 +2
- Partials 547 549 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 2 files with indirect coverage changes 🚀 New features to boost your workflow:
|
…neric-1063 # Conflicts: # .pre-commit-config.yaml # infrahub_sdk/node/related_node.py # infrahub_sdk/node/relationship.py # infrahub_sdk/protocols_base.py
The develop merge combined upstream peer/get docstrings with our generic PeerT/PeerTSync return types, leaving the committed related_node.mdx stale. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
# Conflicts: # infrahub_sdk/protocols_generator/generator.py
# Conflicts: # infrahub_sdk/node/related_node.py # infrahub_sdk/protocols_generator/template.j2 # tests/unit/sdk/test_protocols_generator.py
ogenstad
approved these changes
Jun 9, 2026
ogenstad
left a comment
Contributor
There was a problem hiding this comment.
LGTM, this will be a great improvement! One thing I think is missing here is that the protocols will need to be recreated once this is in place and we should highlight this in the changelog newsfragment so that it's clear that it won't just work out of the box.
Highlight in the 1064 newsfragment that the new relationship-assignment typing lives in generated protocols, so existing projects must rerun `infrahubctl protocols` to pick it up — it won't work out of the box. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Fixes #1064. Stacked on #1063 (base branch is
ihs-relatednode-generic-1063); retarget once #1063 / #763 merge.Problem
Generated cardinality-one relationships are typed read-only as
RelatedNode, but the documented way to set a relationship isnode.rel = "<id>"ornode.rel = peer_node. Under mypy/ty (sincepy.typedin 1.19.0) every such assignment fails withIncompatible types in assignment ... variable has type "RelatedNode"(assignment).Change
RelationshipAttribute/RelationshipAttributeSync(ininfrahub_sdk/node/related_node.py). It reads back asRelatedNode[Peer](so.peerkeeps the peer type from bug: RelatedNode.peer is typed InfrahubNode, losing the peer type during relationship traversal #1063) and accepts assignment ofstr | list[str] | Peer | None, mirroring the runtimeInfrahubNode.__setattr__, which wraps the assigned value in aRelatedNode.infrahubctl protocolsnow emits cardinality-one relationships asRelationshipAttribute[Peer](cardinality-many staysRelationshipManager[Peer]).node.relseparate read/assign types.Verification
mypy(125 files) ✅,ty✅,ruff✅, generator test updated, 258 node/relationship/protocol unit tests ✅.assignmenterrors (node.rel = "<id>"/= peer) drop to 0. Remaining downstream errors are unrelated (untypedclient.create(kind="...")results that need a cast, and anOptionalnot handled) — not relationship assignments.Depends on
#1063 (generic
RelatedNode) —RelationshipAttribute.__get__returnsRelatedNode[Peer].🤖 Generated with Claude Code
Summary by cubic
Make cardinality-one relationships assignable in type-checked code by adding a typing-only
RelationshipAttribute[...]descriptor. Reads still returnRelatedNode[Peer], so.peerkeeps its type andnode.rel = "<id>"or a peer node now type-checks.Bug Fixes
RelationshipAttribute/RelationshipAttributeSyncthat acceptstr | list[str] | Peer | Noneand read asRelatedNode[Peer]; typing-only, never used at runtime.RelationshipAttribute[Peer]for cardinality-one (includingparent); many staysRelationshipManager[Peer].infrahub_sdk.node; removes mypy/ty assignment errors without changing runtime behavior.Migration
infrahubctl protocolsto pick up the new typing; existing generated files won’t update automatically.Written for commit c5887f0. Summary will update on new commits.