-
Notifications
You must be signed in to change notification settings - Fork 13
fix: duck-type get_kind() instead of isinstance against write-side schema classes #188
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
Open
qduk
wants to merge
7
commits into
main
Choose a base branch
from
fix/get-kind-schema-api-isinstance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e46fdce
fix: duck-type get_kind() instead of isinstance against write-side sc…
e3241d8
fix: satisfy ty by casting after the cardinality duck-type check
b253bb3
test: cover get_kind() duck-typing and give it an honest Protocol sig…
7d18475
style: drop the rationale comment inside get_kind()
0400759
style: trim _AttributeLike docstring to one line
7ca68fe
fix: split get_kind() into per-context attribute/relationship filters
6b0513f
Removed unneeded type casting
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| """Tests for infrahub_sync.generator.get_attribute_type_annotation / get_relationship_type_annotation. | ||
|
|
||
| These functions decide the pydantic type annotation for each generated DiffSync | ||
| model field. They must work on whatever object `client.schema.all()` hands back | ||
| -- infrahub-sdk's read-side schema classes (AttributeSchemaAPI / | ||
| RelationshipSchemaAPI) -- not just on the write-side AttributeSchema / | ||
| RelationshipSchema classes used to build schema payloads. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
| from infrahub_sdk.schema import AttributeSchema, RelationshipSchema | ||
| from infrahub_sdk.schema.main import AttributeKind, AttributeSchemaAPI, RelationshipSchemaAPI | ||
|
|
||
| from infrahub_sync.generator import get_attribute_type_annotation, get_relationship_type_annotation | ||
|
|
||
|
|
||
| @dataclass | ||
| class _FakeAttribute: | ||
| """Duck-types an attribute schema without subclassing any SDK class.""" | ||
|
|
||
| kind: str | ||
| optional: bool = False | ||
| default_value: Any = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class _FakeRelationship: | ||
| """Duck-types a relationship schema without subclassing any SDK class.""" | ||
|
|
||
| cardinality: str | ||
| optional: bool = False | ||
|
|
||
|
|
||
| def test_fake_schema_objects_do_not_subclass_sdk_schema_classes() -> None: | ||
| """Pins down the premise the other tests rely on. | ||
|
|
||
| If this starts failing, the fakes below stopped simulating the | ||
| decoupled-class scenario and no longer guard the regression. | ||
| """ | ||
| assert not isinstance(_FakeAttribute(kind="Text"), AttributeSchema) | ||
| assert not isinstance(_FakeRelationship(cardinality="one"), RelationshipSchema) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("attribute", "expected"), | ||
| [ | ||
| (_FakeAttribute(kind="Text"), "str"), | ||
| (_FakeAttribute(kind="Number"), "int"), | ||
| (_FakeAttribute(kind="Boolean"), "bool"), | ||
| (_FakeAttribute(kind="SomeUnmappedKind"), "str"), | ||
| (_FakeAttribute(kind="Text", optional=True), "str | None = None"), | ||
| (_FakeAttribute(kind="Text", optional=True, default_value="foo"), 'str | None = "foo"'), | ||
| (_FakeAttribute(kind="Number", optional=True, default_value=5), "int | None = 5"), | ||
| (_FakeAttribute(kind="Boolean", optional=True, default_value=True), "bool | None = True"), | ||
| ], | ||
| ) | ||
| def test_get_attribute_type_annotation_for_attribute_shaped_objects(attribute: _FakeAttribute, expected: str) -> None: | ||
| assert get_attribute_type_annotation(attribute) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("relationship", "expected"), | ||
| [ | ||
| (_FakeRelationship(cardinality="one"), "str"), | ||
| (_FakeRelationship(cardinality="one", optional=True), "str | None = None"), | ||
| (_FakeRelationship(cardinality="many"), "list[str] = []"), | ||
| (_FakeRelationship(cardinality="many", optional=True), "list[str] | None = []"), | ||
| ], | ||
| ) | ||
| def test_get_relationship_type_annotation_for_relationship_shaped_objects( | ||
| relationship: _FakeRelationship, expected: str | ||
| ) -> None: | ||
| assert get_relationship_type_annotation(relationship) == expected | ||
|
|
||
|
|
||
| def test_get_attribute_type_annotation_against_real_sdk_read_side_schema_classes() -> None: | ||
| """Sanity check against the actual classes `client.schema.all()` returns.""" | ||
| optional_attr = AttributeSchemaAPI(id="1", name="description", kind=AttributeKind.TEXT, optional=True) | ||
| assert get_attribute_type_annotation(optional_attr) == "str | None = None" | ||
|
|
||
| required_attr = AttributeSchemaAPI(id="2", name="name", kind=AttributeKind.TEXT, optional=False) | ||
| assert get_attribute_type_annotation(required_attr) == "str" | ||
|
|
||
|
|
||
| def test_get_relationship_type_annotation_against_real_sdk_read_side_schema_classes() -> None: | ||
| """Sanity check against the actual classes `client.schema.all()` returns.""" | ||
| many_rel = RelationshipSchemaAPI(id="3", name="tags", peer="BuiltinTag", cardinality="many", optional=True) | ||
| assert get_relationship_type_annotation(many_rel) == "list[str] | None = []" | ||
|
|
||
| one_rel = RelationshipSchemaAPI(id="4", name="status", peer="StatusGeneric", cardinality="one", optional=False) | ||
| assert get_relationship_type_annotation(one_rel) == "str" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Add concise docstrings to both public test functions.
tests/test_generator.py#L62-L63: document the attribute-shaped object annotation contract.tests/test_generator.py#L75-L78: document the relationship-shaped object annotation contract.As per coding guidelines, public functions and classes get concise docstrings.
📍 Affects 1 file
tests/test_generator.py#L62-L63(this comment)tests/test_generator.py#L75-L78🤖 Prompt for AI Agents
Source: Coding guidelines