-
Notifications
You must be signed in to change notification settings - Fork 7
Fix optional relationships preventing object template defaults from applying #634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. Comment |
- Modified _generate_input_data() to skip adding None for optional relationships on new nodes - For existing nodes, still include None to allow clearing relationships (preserves PR #515 behavior) - Updated tests to reflect new behavior - Added test to verify existing nodes still work correctly Co-authored-by: minitriga <26367336+minitriga@users.noreply.github.com>
Deploying infrahub-sdk-python with
|
| Latest commit: |
15beefe
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://59bec498.infrahub-sdk-python.pages.dev |
| Branch Preview URL: | https://copilot-fix-optional-attribu.infrahub-sdk-python.pages.dev |
Codecov Report✅ All modified and coverable lines are covered by tests. @@ Coverage Diff @@
## stable #634 +/- ##
=======================================
Coverage 76.03% 76.03%
=======================================
Files 113 113
Lines 9743 9744 +1
Branches 1490 1491 +1
=======================================
+ Hits 7408 7409 +1
Misses 1840 1840
Partials 495 495
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
No, this PR does not cause a regression for #479. Issue #479 was about allowing users to explicitly set optional cardinality-one relationships to The key distinction in this PR is:
I verified this by running Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
tests/unit/sdk/test_node.py
Outdated
|
|
||
| @pytest.mark.parametrize("client_type", client_types) | ||
| async def test_update_input_data_existing_node_with_optional_relationship( | ||
| client, location_schema: NodeSchemaAPI, client_type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good if all of these parameters included the type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Fix this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit dd24d0f. Added type annotations for all parameters:
clients: "BothClients"(using string annotation to avoid runtime import issues)location_schema: NodeSchemaAPIclient_type: str
Also switched from the client fixture to the clients fixture which properly supports both sync and async clients via clients.standard and clients.sync.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot instead of "BothClients" can we use from future import annotations so we can use clients: BothClients? Can you also ensure there is a changelog for this change called 630.fixed.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit de39974:
- Added
from __future__ import annotationsto enable postponed evaluation - Changed
clients: "BothClients"toclients: BothClients(no string quotes) - Moved
GenericSchema,NodeSchemaAPI, andHTTPXMockimports to TYPE_CHECKING block per ruff TC rules - Created
changelog/630.fixed.mdwith the fix description
tests/unit/sdk/test_node.py
Outdated
| if client_type == "standard": | ||
| node = InfrahubNode(client=client, schema=location_schema, data=data) | ||
| else: | ||
| node = InfrahubNodeSync(client=client, schema=location_schema, data=data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As client doesn't have a type as per above we don't see the type violation here as InfrahubNodeSync doesn't accept an async client. We have a fixture called both_clients (or similar) which supports both sync and async.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Fix this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit dd24d0f. The test now uses the clients fixture (which is a BothClients instance) instead of client, and correctly accesses clients.standard for async and clients.sync for sync clients. This prevents the type violation where InfrahubNodeSync would receive an async client.
- Added BothClients import to TYPE_CHECKING block - Updated test_update_input_data_existing_node_with_optional_relationship to use clients fixture with proper type annotations - Changed from client fixture to clients fixture (clients.standard and clients.sync) - Added type annotation for client_type parameter Co-authored-by: minitriga <26367336+minitriga@users.noreply.github.com>
- Added `from __future__ import annotations` to enable postponed evaluation - Changed `clients: "BothClients"` to `clients: BothClients` without string quotes - Moved GenericSchema, NodeSchemaAPI, and HTTPXMock imports to TYPE_CHECKING block - Added changelog/630.fixed.md documenting the fix Co-authored-by: minitriga <26367336+minitriga@users.noreply.github.com>
|
@copilot can you fix linting |
Changed `from tests.unit.sdk.conftest import BothClients` to `from .conftest import BothClients` to fix ruff import sorting error after merge from stable branch. Co-authored-by: minitriga <26367336+minitriga@users.noreply.github.com>
…pplying (#634) * Initial plan * Fix optional attributes not being set when using object templates - Modified _generate_input_data() to skip adding None for optional relationships on new nodes - For existing nodes, still include None to allow clearing relationships (preserves PR #515 behavior) - Updated tests to reflect new behavior - Added test to verify existing nodes still work correctly Co-authored-by: minitriga <26367336+minitriga@users.noreply.github.com> * Add type annotations to test parameters and use BothClients fixture - Added BothClients import to TYPE_CHECKING block - Updated test_update_input_data_existing_node_with_optional_relationship to use clients fixture with proper type annotations - Changed from client fixture to clients fixture (clients.standard and clients.sync) - Added type annotation for client_type parameter Co-authored-by: minitriga <26367336+minitriga@users.noreply.github.com> * Use __future__ annotations and add changelog for issue 630 - Added `from __future__ import annotations` to enable postponed evaluation - Changed `clients: "BothClients"` to `clients: BothClients` without string quotes - Moved GenericSchema, NodeSchemaAPI, and HTTPXMock imports to TYPE_CHECKING block - Added changelog/630.fixed.md documenting the fix Co-authored-by: minitriga <26367336+minitriga@users.noreply.github.com> * Fix linting: correct import path in TYPE_CHECKING block Changed `from tests.unit.sdk.conftest import BothClients` to `from .conftest import BothClients` to fix ruff import sorting error after merge from stable branch. Co-authored-by: minitriga <26367336+minitriga@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: minitriga <26367336+minitriga@users.noreply.github.com> Co-authored-by: Alex Gittings <agitting96@gmail.com>
Fix bug where creating objects with object templates fails to set optional attributes
Problem
When creating a new object with an object template, the SDK includes
nullvalues for uninitialized optional relationships with cardinality ONE. This prevents the backend from applying values from the object template.Root Cause
In
infrahub_sdk/node/node.py, the_generate_input_data()method always addsNonefor optional, uninitialized relationships (lines 237-239), even for new objects. The backend interprets these explicitNonevalues as overrides, preventing object template defaults from being applied.Solution Plan
_generate_input_data()to skip addingNonefor optional relationships when creating NEW nodes (whereself._existing == False)from __future__ import annotationsfor cleaner type hintsChanges Made
1. Core Logic Change (infrahub_sdk/node/node.py)
Modified
_generate_input_data()method to conditionally includeNonefor optional relationships:self._existing == False): Omit optional relationship fields entirely, allowing object template defaults to be appliedself._existing == True): IncludeNonefor optional relationships, preserving ability to clear relationships (PR IHS-152 Allow unsetting one-to-one relationship #515)2. Test Updates (tests/unit/sdk/test_node.py)
Nonefor new nodesNonefrom __future__ import annotationsfor cleaner type hints3. Changelog (changelog/630.fixed.md)
Test Results
✅ All 154 node unit tests pass
✅ All linting checks pass (ruff, mypy, yamllint)
✅ CodeQL security scan: 0 alerts
Security Summary
No security vulnerabilities were introduced or discovered by this change. CodeQL analysis found 0 alerts.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.