Skip to content

Clean up of logic within Node#910

Merged
ogenstad merged 1 commit intoinfrahub-developfrom
pog-node-cleanup
Apr 1, 2026
Merged

Clean up of logic within Node#910
ogenstad merged 1 commit intoinfrahub-developfrom
pog-node-cleanup

Conversation

@ogenstad
Copy link
Copy Markdown
Contributor

@ogenstad ogenstad commented Mar 31, 2026

Why

The node initialization code in InfrahubNodeBase was determining capability flags (_artifact_support, _file_object_support, _hierarchy_support, _artifact_definition_support) by inspecting schema internals directly — using getattr to reach into inherit_from, string comparisons against hardcoded kind names, and isinstance checks to exclude schema types that don't carry certain fields. This logic belonged on the schema layer, not the node layer.

The change moves these capability checks to where the relevant data lives: BaseSchema provides safe defaults (False / []) for all schema types, and NodeSchemaAPI overrides them with real logic using its own fields (inherit_from, hierarchy). The same pattern is applied to hierarchical_relationship_schemas — rather than constructing four hardcoded RelationshipSchemaAPI objects inline in _init_relationships (duplicated across the async and sync classes), the schema itself now produces the correct pseudo-schemas for hierarchical nodes.

The result is that node.py no longer needs to know anything about the structure of the schema to determine what a node supports. Four uniform assignments replace a mix of getattr hacks, isinstance guards, and inline schema construction. The _init_relationships hierarchical block drops from ~70 lines (×2 for async/sync) to a single loop.

What changed

  • infrahub_sdk/schema/main.py: define various generic properties related to the schema
  • infrahub_sdk/node/node.py: refactor to use the new properties to simplify the code.

Summary by CodeRabbit

  • New Features

    • Added helper properties on schemas to expose capability and cardinality information for more accurate feature detection.
  • Improvements

    • Improved node capability detection to support broader and more flexible schema configurations.
    • Simplified hierarchical relationship initialization for more consistent behavior, fewer runtime checks, and easier maintenance.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 31, 2026

Walkthrough

Schemas (infrahub_sdk/schema/main.py) gain capability properties: supports_artifact_definition, supports_artifacts, supports_file_object, supports_hierarchy, hierarchical_relationship_schemas, and RelationshipSchemaAPI cardinality helpers. Node initialization (infrahub_sdk/node/node.py) now reads feature flags directly from those schema properties instead of inspecting subclass/type attributes. Hierarchical relationships are initialized by iterating schema.hierarchical_relationship_schemas and creating RelatedNode/RelatedNodeSync for single-cardinality or RelationshipManager/RelationshipManagerSync for many-cardinality relationships. SDK-side construction of pseudo-schemas and RelationshipKind-based wiring was removed; hierarchical entries originate from the schema.

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title is vague and generic, using 'Clean up of logic' which does not convey the specific refactoring focus of moving capability detection from node layer to schema layer. Consider a more specific title like 'Move node capability detection to schema layer' or 'Refactor hierarchy initialization using schema properties' to better convey the main change.
✅ Passed checks (1 passed)
Check name Status Explanation
Description check ✅ Passed The PR description includes all key sections: Why (motivation for moving logic), What changed (affected files and specific changes), but lacks How to test, Impact & rollout details, and a checklist to fully align with the template.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 31, 2026

Codecov Report

❌ Patch coverage is 97.95918% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
infrahub_sdk/schema/main.py 97.14% 1 Missing ⚠️
@@                 Coverage Diff                  @@
##           infrahub-develop     #910      +/-   ##
====================================================
- Coverage             81.09%   81.04%   -0.06%     
====================================================
  Files                   121      121              
  Lines                 10592    10644      +52     
  Branches               1581     1586       +5     
====================================================
+ Hits                   8590     8626      +36     
- Misses                 1487     1502      +15     
- Partials                515      516       +1     
Flag Coverage Δ
integration-tests 40.82% <53.06%> (-0.26%) ⬇️
python-3.10 52.42% <53.06%> (-0.26%) ⬇️
python-3.11 52.40% <53.06%> (-0.28%) ⬇️
python-3.12 52.40% <53.06%> (-0.26%) ⬇️
python-3.13 52.40% <53.06%> (-0.28%) ⬇️
python-3.14 54.11% <53.06%> (-0.23%) ⬇️
python-filler-3.12 24.04% <44.89%> (+0.22%) ⬆️

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 85.94% <100.00%> (-0.27%) ⬇️
infrahub_sdk/schema/main.py 90.99% <97.14%> (+0.77%) ⬆️

... and 11 files with indirect coverage changes

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

@ogenstad ogenstad marked this pull request as ready for review March 31, 2026 17:07
@ogenstad ogenstad requested a review from a team as a code owner March 31, 2026 17:07
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@infrahub_sdk/schema/main.py`:
- Around line 344-350: The supports_artifacts and supports_file_object checks
currently live only on NodeSchemaAPI causing ProfileSchemaAPI and
TemplateSchemaAPI (which also have inherit_from) to incorrectly return False;
move these property implementations to a shared base or mixin used by
MainSchemaTypesAPI (e.g., create an InheritFromSupportMixin with
supports_artifacts and supports_file_object that check "CoreArtifactTarget" and
"CoreFileObject" in inherit_from) or alternatively implement the same properties
on ProfileSchemaAPI and TemplateSchemaAPI so they mirror NodeSchemaAPI's
behavior; update references to supports_artifacts and supports_file_object
across MainSchemaTypesAPI and node/node.py to use the new shared implementation
to avoid FeatureNotSupportedError regressions.
- Around line 352-359: supports_hierarchy uses "self.hierarchy is not None" but
hierarchical_relationship_schemas uses "if not self.hierarchy", causing
inconsistent treatment of empty-string hierarchy; update
hierarchical_relationship_schemas to use the same predicate as
supports_hierarchy (i.e., check "self.hierarchy is not None") so both
supports_hierarchy and hierarchical_relationship_schemas (and any related
_hierarchy_support logic) treat an empty string consistently.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: de4b62fb-d38d-4b74-9fdc-36ae1126c1dc

📥 Commits

Reviewing files that changed from the base of the PR and between abe1d0d and 7f6ece9.

📒 Files selected for processing (2)
  • infrahub_sdk/node/node.py
  • infrahub_sdk/schema/main.py

Comment thread infrahub_sdk/schema/main.py
Comment thread infrahub_sdk/schema/main.py
Copy link
Copy Markdown
Contributor

@ajtmccarty ajtmccarty left a comment

Choose a reason for hiding this comment

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

coderabbit might have a couple legitimate comments, but, other than that, it looks great. big improvement

Comment thread infrahub_sdk/node/node.py
Comment on lines 13 to 16
GenericSchemaAPI,
ProfileSchemaAPI,
RelationshipCardinality,
RelationshipKind,
RelationshipSchemaAPI,
TemplateSchemaAPI,
)
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.

🥇

@ogenstad
Copy link
Copy Markdown
Contributor Author

coderabbit might have a couple legitimate comments, but, other than that, it looks great. big improvement

@ajtmccarty, yeah the reason for the major one was to get rid of an if-statement within the __init__() method that also included those isinstance statements (I'm not a big fan of having different code paths within these init methods..). I think this could perhaps be solved by adding some docstrings for those properties to indicate that it's to ensure that we have the same methods on all schema nodes but that they will always return false/negative. What do you think about that?

Will have to check on the hierarchy thing.

@ajtmccarty
Copy link
Copy Markdown
Contributor

coderabbit might have a couple legitimate comments, but, other than that, it looks great. big improvement

@ajtmccarty, yeah the reason for the major one was to get rid of an if-statement within the __init__() method that also included those isinstance statements (I'm not a big fan of having different code paths within these init methods..). I think this could perhaps be solved by adding some docstrings for those properties to indicate that it's to ensure that we have the same methods on all schema nodes but that they will always return false/negative. What do you think about that?

Will have to check on the hierarchy thing.

sounds good to me

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Apr 1, 2026

Deploying infrahub-sdk-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: dc28bad
Status: ✅  Deploy successful!
Preview URL: https://7c189d38.infrahub-sdk-python.pages.dev
Branch Preview URL: https://pog-node-cleanup.infrahub-sdk-python.pages.dev

View logs

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
infrahub_sdk/schema/main.py (1)

370-383: Consider adding kind=RelationshipKind.HIERARCHY to ancestors/descendants for consistency.

parent and children explicitly set kind=RelationshipKind.HIERARCHY, but ancestors and descendants rely on the default (GENERIC). While functionally this may not matter (since _process_hierarchical_fields determines behavior by name rather than kind), semantically all four represent hierarchical relationships.

♻️ Suggested change for semantic consistency
             RelationshipSchemaAPI(
-                name="ancestors", peer=self.hierarchy, cardinality="many", read_only=True, optional=True
+                name="ancestors", peer=self.hierarchy, kind=RelationshipKind.HIERARCHY, cardinality="many", read_only=True, optional=True
             ),
             RelationshipSchemaAPI(
-                name="descendants", peer=self.hierarchy, cardinality="many", read_only=True, optional=True
+                name="descendants", peer=self.hierarchy, kind=RelationshipKind.HIERARCHY, cardinality="many", read_only=True, optional=True
             ),
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@infrahub_sdk/schema/main.py` around lines 370 - 383, The ancestors and
descendants RelationshipSchemaAPI entries currently omit kind, defaulting to
GENERIC; update them to explicitly set kind=RelationshipKind.HIERARCHY for
semantic consistency with parent and children. Locate the list returning
RelationshipSchemaAPI instances (symbols: RelationshipSchemaAPI, ancestors,
descendants) and add kind=RelationshipKind.HIERARCHY to those two entries; leave
other properties (cardinality, read_only, optional) unchanged and ensure no
behavior relies on the default kind in _process_hierarchical_fields.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@infrahub_sdk/schema/main.py`:
- Around line 370-383: The ancestors and descendants RelationshipSchemaAPI
entries currently omit kind, defaulting to GENERIC; update them to explicitly
set kind=RelationshipKind.HIERARCHY for semantic consistency with parent and
children. Locate the list returning RelationshipSchemaAPI instances (symbols:
RelationshipSchemaAPI, ancestors, descendants) and add
kind=RelationshipKind.HIERARCHY to those two entries; leave other properties
(cardinality, read_only, optional) unchanged and ensure no behavior relies on
the default kind in _process_hierarchical_fields.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ba4bb2ba-54ac-4a98-a960-852de8d0a005

📥 Commits

Reviewing files that changed from the base of the PR and between 7f6ece9 and dc28bad.

📒 Files selected for processing (2)
  • infrahub_sdk/node/node.py
  • infrahub_sdk/schema/main.py

@ogenstad ogenstad merged commit 4704159 into infrahub-develop Apr 1, 2026
21 checks passed
@ogenstad ogenstad deleted the pog-node-cleanup branch April 1, 2026 06:13
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.

2 participants