Skip to content

**WIP** fix: Support attribute assignment of cardinality-many relationships#1154

Draft
PhillSimonds wants to merge 2 commits into
developfrom
sts/fix-1152-many-rel-assignment
Draft

**WIP** fix: Support attribute assignment of cardinality-many relationships#1154
PhillSimonds wants to merge 2 commits into
developfrom
sts/fix-1152-many-rel-assignment

Conversation

@PhillSimonds

@PhillSimonds PhillSimonds commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

What

Fixes #1152.

Assigning to a cardinality-many relationship attribute (e.g. node.members = [peer]) previously fell through InfrahubNode.__setattr__ to super().__setattr__, storing the raw value as a normal attribute and not correctly wrapping the object in a RelationshipManager. The node then failed in save() with the confusing 'list' object has no attribute 'initialized' error. This behavior is different than instantiating an object via .create() with a list passed into data=, which correctly used .setattr to construct a RelationshipManager and doesn't fall through to super().__setattr__.

Change

__setattr__ now handles cardinality-many relationships (both InfrahubNode and InfrahubNodeSync):

  • a list rebuilds the RelationshipManager from the value and marks it _has_update so save() actually persists it;
  • a non-list value raises the existing "expects a list of nodes" error from the RelationshipManager constructor, at assignment time rather than deferred to save().

Tests

Added to tests/unit/sdk/test_node.py (async + sync):

  • test_cardinality_many_assignment_requires_list — single node/dict assignment raises "expects a list of nodes".
  • test_cardinality_many_assignment_accepts_list — list assignment populates peers and sets has_update.

uv run invoke format lint-code clean; uv run pytest tests/unit/sdk/test_node.py → 226 passed.

Related

Same relationship-editing ergonomics area as #1031 and the sibling PR for #1153.


Summary by cubic

Fixes assignment to cardinality-many relationships so list assignments populate the manager and non-list values fail fast, eliminating the confusing 'InfrahubNode' object has no attribute 'initialized' save-time error. Applies to both async and sync nodes.

  • Bug Fixes
    • Handle cardinality-many attrs in __setattr__ for InfrahubNode and InfrahubNodeSync.
    • List assignment rebuilds the relationship manager, marks _has_update, and is included in mutation payloads.
    • Non-list assignment raises a clear “expects a list of nodes” error at assignment time.
    • Added tests for list vs non-list assignment, input payload generation, and the save() path for both clients.

Written for commit 9beed45. Summary will update on new commits.

Review in cubic

Assigning to a cardinality-many relationship attribute (e.g.
`node.members = [peer]`) previously fell through `__setattr__` to
`super().__setattr__`, shadowing the RelationshipManager with a raw value.
The node then failed in `save()` with a confusing
`'InfrahubNode' object has no attribute 'initialized'` error.

`__setattr__` now handles cardinality-many relationships: a list rebuilds
the RelationshipManager (marked as updated so save() persists it), and a
non-list value raises the existing "expects a list of nodes" error at
assignment time. Applied to both InfrahubNode and InfrahubNodeSync.

Fixes #1152

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@PhillSimonds
PhillSimonds requested a review from a team as a code owner July 10, 2026 00:38
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 10, 2026

Copy link
Copy Markdown

Deploying infrahub-sdk-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: 9beed45
Status: ✅  Deploy successful!
Preview URL: https://388d8111.infrahub-sdk-python.pages.dev
Branch Preview URL: https://sts-fix-1152-many-rel-assign.infrahub-sdk-python.pages.dev

View logs

@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.77778% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
infrahub_sdk/node/node.py 77.77% 2 Missing and 2 partials ⚠️

❗ There is a different number of reports uploaded between BASE (17483a0) and HEAD (9beed45). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (17483a0) HEAD (9beed45)
integration-tests 1 0
@@             Coverage Diff             @@
##           develop    #1154      +/-   ##
===========================================
- Coverage    82.35%   75.51%   -6.84%     
===========================================
  Files          138      138              
  Lines        12065    12060       -5     
  Branches      1805     1809       +4     
===========================================
- Hits          9936     9107     -829     
- Misses        1573     2401     +828     
+ Partials       556      552       -4     
Flag Coverage Δ
integration-tests ?
python-3.10 55.76% <77.77%> (-0.05%) ⬇️
python-3.11 55.77% <77.77%> (-0.05%) ⬇️
python-3.12 55.76% <77.77%> (-0.06%) ⬇️
python-3.13 55.76% <77.77%> (-0.06%) ⬇️
python-3.14 55.77% <77.77%> (-0.03%) ⬇️
python-filler-3.12 22.44% <0.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
infrahub_sdk/node/node.py 80.42% <77.77%> (-7.15%) ⬇️

... and 34 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

Re-trigger cubic

@PhillSimonds PhillSimonds changed the title fix: support assigning to cardinality-many relationships **WIP** fix: support assigning to cardinality-many relationships Jul 10, 2026
@PhillSimonds
PhillSimonds marked this pull request as draft July 10, 2026 03:32
@PhillSimonds PhillSimonds changed the title **WIP** fix: support assigning to cardinality-many relationships **WIP** fix: Support attribute assignment of cardinality-many relationships Jul 10, 2026
Add coverage that a list assigned to a many-relationship after construction is
carried through the save() payload:
- test_cardinality_many_assignment_included_in_input_data asserts the peers are
  present in _generate_input_data() (the build path that previously crashed, and
  a guard that _strip_unmodified keeps the assignment via has_update).
- test_cardinality_many_assignment_saved calls save() with a mocked mutation and
  asserts the node is persisted and the peers are sent in the request.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant