Skip to content

Clean up of logic within Node#910

Open
ogenstad wants to merge 1 commit intoinfrahub-developfrom
pog-node-cleanup
Open

Clean up of logic within Node#910
ogenstad wants to merge 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 to schema classes for improved feature detection and cardinality checking
  • Improvements

    • Enhanced node feature detection to support more flexible schema configurations
    • Simplified hierarchical relationship initialization for better maintainability

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 31, 2026

Walkthrough

The pull request refactors node feature flag initialization and hierarchical relationship setup across schema and node classes. In infrahub_sdk/schema/main.py, new capability-detection properties are added to schema classes (supports_artifacts, supports_file_object, supports_hierarchy, supports_artifact_definition) along with cardinality helper properties on relationship schemas. In infrahub_sdk/node/node.py, node initialization logic is updated to set feature flags directly from these schema properties instead of relying on schema subclass inspection. Hierarchical relationships are now initialized dynamically based on hierarchical_relationship_schemas from the schema rather than hardcoded construction. Changes apply to both async and sync node implementations.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 5.56% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Clean up of logic within Node' accurately summarizes the main refactoring effort: moving capability-detection logic from the node layer to the schema layer and simplifying hierarchical relationship initialization.
Description check ✅ Passed The description comprehensively covers the Why, What changed, and problem context with clear reasoning. Key behavioral intent is documented, though How to review and testing sections could be more detailed.

✏️ 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.06%   -0.03%     
====================================================
  Files                   121      121              
  Lines                 10592    10644      +52     
  Branches               1581     1586       +5     
====================================================
+ Hits                   8590     8629      +39     
- Misses                 1487     1498      +11     
- Partials                515      517       +2     
Flag Coverage Δ
integration-tests 40.84% <53.06%> (-0.23%) ⬇️
python-3.10 52.42% <53.06%> (-0.26%) ⬇️
python-3.11 52.40% <53.06%> (-0.28%) ⬇️
python-3.12 52.42% <53.06%> (-0.24%) ⬇️
python-3.13 52.42% <53.06%> (-0.26%) ⬇️
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 86.25% <100.00%> (+0.03%) ⬆️
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 on lines +344 to +350
@property
def supports_artifacts(self) -> bool:
return "CoreArtifactTarget" in self.inherit_from

@property
def supports_file_object(self) -> bool:
return "CoreFileObject" in self.inherit_from
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Share inherit_from capability checks with profile/template schemas.

Lines 68-69 in infrahub_sdk/node/node.py now read these properties for every MainSchemaTypesAPI, but only NodeSchemaAPI overrides them here. ProfileSchemaAPI and TemplateSchemaAPI still carry inherit_from, so any of those kinds inheriting CoreArtifactTarget or CoreFileObject now regress to False and can start raising FeatureNotSupportedError. Please move these checks onto a shared inherit_from-aware base/mixin, or override them on the profile/template schema classes as well.

🤖 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 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.

Comment on lines +352 to +359
@property
def supports_hierarchy(self) -> bool:
return self.hierarchy is not None

@property
def hierarchical_relationship_schemas(self) -> list[RelationshipSchemaAPI]:
if not self.hierarchy:
return []
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Keep the hierarchy enablement predicate consistent.

Line 354 treats hierarchy="" as enabled, while Line 358 treats the same value as disabled. That can leave _hierarchy_support=True but produce no hierarchical wrappers. Use the same predicate in both properties.

🛠️ Minimal fix
     `@property`
     def supports_hierarchy(self) -> bool:
-        return self.hierarchy is not None
+        return bool(self.hierarchy)
🤖 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 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.

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 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.

🥇

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