feat: add update_from_dict to update node fields from a dict
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 Attribute and RelationshipManager rebuilding (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
- New
update_from_dict()method onInfrahubNodeandInfrahubNodeSync:- Attributes accept scalars, dicts with
valueand optional properties, orfrom_poolallocations; updated attributes are marked mutated so partial updates vianode.update()include them (including clearing an optional attribute withNone). - Cardinality-one relationships accept an ID, HFID list, dict, or node object;
Noneclears them. - Cardinality-many relationships replace the peer list from a list of IDs/dicts/node objects;
[](orNone) clears them. - Unknown field names raise
ValueErrorbefore anything is touched, so a typo cannot half-apply an update. - Cardinality-many relationships that take peers as fields raise
FeatureNotSupportedErrorbefore anything is touched.
- Attributes accept scalars, dicts with
- Method declared on the
CoreNodeBaseprotocol so protocol-typed code can call it. - No changes to existing save/update payload generation โ the method reuses the existing
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
uv run pytest tests/unit/sdk/test_node.py -k update_from_dict
14 new test cases (7 scenarios ร async/sync) covering attribute scalars, attribute dicts with properties, from_pool without 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
- Backward compatibility: purely additive, no changes to existing behavior.
Checklist
- Tests added/updated
- Changelog entry added
- External docs updated (generated SDK reference)
๐ค Generated with Claude Code
Summary by cubic
Add update_from_dict() to InfrahubNode and InfrahubNodeSync to 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.
- New Features
- Attributes accept scalars,
{ value, ...props }, orfrom_pool; changes are marked mutated andNoneclears optional values. - Relationships accept an ID, dict, node object, or lists for cardinality-many;
Noneclears ONE and[]orNoneclears MANY. - Unknown fields raise
ValueErrorbefore any change is applied. - Method added to the
CoreNodeBaseprotocol; existing save/update payload generation is unchanged.
- Attributes accept scalars,