From 5fb075815c93b337cd96223d6d9134010ad1f41a Mon Sep 17 00:00:00 2001 From: Babatunde Olusola Date: Wed, 29 Oct 2025 06:01:40 +0100 Subject: [PATCH] IHS-128: Replace `Sync` in protocol sync classes schema name (#586) * IHS-128: Replace `Sync` in protocol sync classes schema name Fixes https://github.com/opsmill/infrahub-sdk-python/issues/380 This PR replaces the `Sync` word in the protocol schema name so that the correct kind can be gotten from the cache. * add towncrier * refactor get_schema_name, add test * update test --- changelog/380.fixed.md | 1 + infrahub_sdk/schema/__init__.py | 11 ++++++++--- tests/unit/sdk/test_schema.py | 12 +++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 changelog/380.fixed.md diff --git a/changelog/380.fixed.md b/changelog/380.fixed.md new file mode 100644 index 00000000..9e764dfa --- /dev/null +++ b/changelog/380.fixed.md @@ -0,0 +1 @@ +Replaced the `Sync` word in the protocol schema name so that the correct kind can be gotten from the cache diff --git a/infrahub_sdk/schema/__init__.py b/infrahub_sdk/schema/__init__.py index 4c2b9c79..4c89ff52 100644 --- a/infrahub_sdk/schema/__init__.py +++ b/infrahub_sdk/schema/__init__.py @@ -1,6 +1,7 @@ from __future__ import annotations import asyncio +import inspect import json import warnings from collections.abc import MutableMapping @@ -185,12 +186,16 @@ def _validate_load_schema_response(response: httpx.Response) -> SchemaLoadRespon @staticmethod def _get_schema_name(schema: type[SchemaType | SchemaTypeSync] | str) -> str: - if hasattr(schema, "_is_runtime_protocol") and schema._is_runtime_protocol: # type: ignore[union-attr] - return schema.__name__ # type: ignore[union-attr] - if isinstance(schema, str): return schema + if hasattr(schema, "_is_runtime_protocol") and getattr(schema, "_is_runtime_protocol", None): + if inspect.iscoroutinefunction(schema.save): + return schema.__name__ + if schema.__name__[-4:] == "Sync": + return schema.__name__[:-4] + return schema.__name__ + raise ValueError("schema must be a protocol or a string") @staticmethod diff --git a/tests/unit/sdk/test_schema.py b/tests/unit/sdk/test_schema.py index 52f04fe7..4db4d37b 100644 --- a/tests/unit/sdk/test_schema.py +++ b/tests/unit/sdk/test_schema.py @@ -9,7 +9,8 @@ from infrahub_sdk import Config, InfrahubClient, InfrahubClientSync from infrahub_sdk.ctl.schema import display_schema_load_errors from infrahub_sdk.exceptions import SchemaNotFoundError, ValidationError -from infrahub_sdk.schema import BranchSchema, InfrahubSchema, InfrahubSchemaSync, NodeSchemaAPI +from infrahub_sdk.protocols import BuiltinIPAddress, BuiltinIPAddressSync, BuiltinTag, BuiltinTagSync +from infrahub_sdk.schema import BranchSchema, InfrahubSchema, InfrahubSchemaBase, InfrahubSchemaSync, NodeSchemaAPI from infrahub_sdk.schema.repository import ( InfrahubCheckDefinitionConfig, InfrahubJinja2TransformConfig, @@ -452,3 +453,12 @@ async def test_display_schema_load_errors_details_when_error_is_in_attribute_or_ Node: SecurityTailscaleSSHRule | Attribute: check_period (10080) | Extra inputs are not permitted (extra_forbidden) """ assert output == expected_console + + +def test_schema_base__get_schema_name__returns_correct_schema_name_for_protocols(): + assert InfrahubSchemaBase._get_schema_name(schema=BuiltinTagSync) == "BuiltinTag" + assert InfrahubSchemaBase._get_schema_name(schema=BuiltinTag) == "BuiltinTag" + assert InfrahubSchemaBase._get_schema_name(schema="BuiltinTag") == "BuiltinTag" + assert InfrahubSchemaBase._get_schema_name(schema=BuiltinIPAddressSync) == "BuiltinIPAddress" + assert InfrahubSchemaBase._get_schema_name(schema=BuiltinIPAddress) == "BuiltinIPAddress" + assert InfrahubSchemaBase._get_schema_name(schema="BuiltinIPAddress") == "BuiltinIPAddress"