Repository navigation
Conversation
There was a problem hiding this comment.
All reported issues were addressed across 5 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
|
|
||
| for name, value in data.items(): | ||
| if name in self._attributes: | ||
| attribute = Attribute(name=name, schema=self._schema.get_attribute(name=name), data=value) |
There was a problem hiding this comment.
is it necessary to create a new Attribute here? can this do self._get_attribute and then attr.value = value? I think that would also inherently handle self._attribute_data
There was a problem hiding this comment.
Changed in 66e0613. A plain value now goes through self._get_attribute(name).value = value, so it reuses the existing attribute.
A dict with value and properties (is_protected, source and so on) or with from_pool still builds a new Attribute, because the value setter only takes a bare value and the constructor already parses the rest. That new attribute is now bound to the node with _bind(), which from_pool and only need on current stable. If you would rather have an in-place update on Attribute for the dict case as well, I can add it.
|
|
||
| super().__setattr__(name, value) | ||
|
|
||
| def _update_relationship_from_dict(self, name: str, data: Any) -> None: |
There was a problem hiding this comment.
basically the same question for this method. is it necessary to create a new RelationshipManager[Sync]? can this use self._get_relationship_many or self._get_relationship_one and then all be handled within the InfrahubNodeBase class instead of different Sync/Async versions?
There was a problem hiding this comment.
Yes, it is all in InfrahubNodeBase now and the async/sync versions are gone.
- cardinality many:
self._get_relationship_many(name), then the peers are replaced withextend()on the existing manager. The manager is marked initialized first, because the whole list is replaced and the current peers do not have to be fetched. - cardinality one:
setattr(self, name, value), the same path asnode.rel = x.RelatedNodehas no setter for the peer, so I kept that instead of_get_relationship_one.
Relationships that take peers as fields raise FeatureNotSupportedError before anything is changed, the same as add() does.
Allow updating several attributes and relationships of an existing InfrahubNode or InfrahubNodeSync at once from a dict, mirroring the data format accepted when creating a node. Attribute values accept scalars, dicts with value and properties, or from_pool allocations; relationships accept IDs, dicts, node objects, or lists of those for cardinality-many. None clears a cardinality-one relationship and [] clears a cardinality-many one. Unknown field names raise ValueError and leave the node unmodified. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Move update_from_dict into InfrahubNodeBase so the async and sync nodes share one implementation. - Scalar attribute values go through the existing attribute setter. A dict with properties or from_pool still builds a new Attribute, because the setter takes a bare value. The new Attribute is now bound to its node, and the caller's dict is copied so from_pool is kept. - Cardinality-many relationships replace the peers of the existing manager instead of building a new manager. - Cardinality-many relationships that take peers as fields raise FeatureNotSupportedError before any field changes. Claude-Session: https://claude.ai/code/session_019U7jtHQEPGWByz5sZjofdD
b3376bc to
66e0613
Compare
Deploying infrahub-sdk-python with
|
| Latest commit: |
66e0613
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://1476e528.infrahub-sdk-python.pages.dev |
| Branch Preview URL: | https://feat-update-node-from-dict.infrahub-sdk-python.pages.dev |
Codecov Report❌ Patch coverage is
@@ Coverage Diff @@
## stable #1206 +/- ##
==========================================
- Coverage 88.37% 87.97% -0.40%
==========================================
Files 157 157
Lines 16323 15703 -620
Branches 2412 2123 -289
==========================================
- Hits 14425 13815 -610
- Misses 1326 1330 +4
+ Partials 572 558 -14
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
Why
Creating a node accepts a dict of attributes and relationships, but updating an existing node requires assigning each field one by one — integrations that receive external data as dicts end up hand-rolling
AttributeandRelationshipManagerrebuilding (as shown in the linked issue).This PR adds
update_from_dict()so an existing node can be updated from a dict in one call, mirroring the data formats accepted at creation time.Non-goals: hierarchical relationships (
parent/children) are not addressed; they are rejected as unknown fields.Closes #272
What changed
update_from_dict()method onInfrahubNodeandInfrahubNodeSync:valueand optional properties, orfrom_poolallocations; updated attributes are marked mutated so partial updates vianode.update()include them (including clearing an optional attribute withNone).Noneclears them.[](orNone) clears them.ValueErrorbefore anything is touched, so a typo cannot half-apply an update.FeatureNotSupportedErrorbefore anything is touched.CoreNodeBaseprotocol so protocol-typed code can call it.Attribute,RelatedNode, andRelationshipManagermachinery.How to review
infrahub_sdk/node/node.py: all the logic is inInfrahubNodeBase.update_from_dict(), shared by the async and sync nodes. Plain attribute values use the existingvaluesetter. Cardinality-one relationships use the existing__setattr__path. Cardinality-many relationships replace the peers of the existing manager from_get_relationship_many().docs/.../node.mdxis regenerated;changelog/272.added.mdis the towncrier fragment.How to test
14 new test cases (7 scenarios × async/sync) covering attribute scalars, attribute dicts with properties,
from_poolwithout changing the caller's dict, both relationship cardinalities, replacing peers that were not fetched, clearing relationships, and the unknown-field error with payload assertions via_generate_input_data().Impact & rollout
Checklist
🤖 Generated with Claude Code
Summary by cubic
Add
update_from_dict()toInfrahubNodeandInfrahubNodeSyncto update multiple attributes and relationships from a dict in one call, matching create-time payloads. This simplifies integrations and validates keys up front to prevent partial updates.{ value, ...props }, orfrom_pool; changes are marked mutated andNoneclears optional values.Noneclears ONE and[]orNoneclears MANY.ValueErrorbefore any change is applied.CoreNodeBaseprotocol; existing save/update payload generation is unchanged.Written for commit b3376bc. Summary will update on new commits.