From 5000a4a0c855e4e7042c65c66ce13c7ff6183572 Mon Sep 17 00:00:00 2001 From: Saltaferis Dimitrios Date: Wed, 2 Dec 2026 11:08:39 +0000 Subject: [PATCH 1/3] test: pin the return types of the client's get and filters overloads Passing a generated protocol class as kind makes client.filters() and client.get() return that class, even with arbitrary filter keyword arguments. Only a one-off reveal_type check during review of #1218 showed this. The new module asserts those types with assert_type(), for the async and sync clients. ty checks it through the existing `ty check .` lint step; a unit test runs mypy on it, because CI runs mypy only over infrahub_sdk/. Widening the filters() protocol overload to list[Any] makes both checks fail. Refs: INBOX-199 Co-Authored-By: Claude Opus 5.5 --- tests/unit/sdk/test_client_overload_types.py | 88 ++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 tests/unit/sdk/test_client_overload_types.py diff --git a/tests/unit/sdk/test_client_overload_types.py b/tests/unit/sdk/test_client_overload_types.py new file mode 100644 index 000000000..462fbb6b6 --- /dev/null +++ b/tests/unit/sdk/test_client_overload_types.py @@ -0,0 +1,88 @@ +"""Pin the return types that the client's ``get()`` and ``filters()`` overloads give typed callers. + +Passing a generated protocol class as ``kind`` makes ``client.filters(kind=NetworkDevice, ...)`` return +``list[NetworkDevice]``. The overloads also accept any filter keyword argument through ``**kwargs: Any``, +and nothing else in the suite fails if a signature change makes those calls resolve to ``Any`` instead. + +The ``assert_type()`` calls below are never executed. Type checkers read them: + +- ty, through the ``uv run ty check .`` lint step, which covers ``tests/``. +- mypy, through ``test_mypy_accepts_the_pinned_overload_types``, because CI runs mypy only over + ``infrahub_sdk/``. +""" + +from __future__ import annotations + +from pathlib import Path +from typing import TYPE_CHECKING + +from mypy import api as mypy_api +from typing_extensions import assert_type + +from infrahub_sdk.node import InfrahubNode, InfrahubNodeSync +from infrahub_sdk.protocols_base import CoreNode, CoreNodeSync + +if TYPE_CHECKING: + from infrahub_sdk import InfrahubClient, InfrahubClientSync + +REPO_ROOT = Path(__file__).resolve().parents[3] + + +class NetworkDevice(CoreNode): + """Stands in for a protocol generated by ``infrahubctl protocols``.""" + + +class NetworkDeviceSync(CoreNodeSync): + """Stands in for the sync variant of a generated protocol.""" + + +async def _async_client_calls(client: InfrahubClient) -> None: + assert_type( + await client.filters(kind=NetworkDevice, name__value="spine1", role__value="leaf", status__value="active"), + list[NetworkDevice], + ) + assert_type(await client.filters(kind=NetworkDevice, include=["interfaces"]), list[NetworkDevice]) + assert_type(await client.filters(kind="NetworkDevice", role__value="leaf"), list[InfrahubNode]) + + assert_type(await client.get(kind=NetworkDevice, id="1234"), NetworkDevice) + assert_type(await client.get(NetworkDevice, name__value="spine1", role__value="leaf"), NetworkDevice) + assert_type( + await client.get(kind=NetworkDevice, name__value="spine1", raise_when_missing=False), NetworkDevice | None + ) + assert_type(await client.get(kind="NetworkDevice", name__value="spine1"), InfrahubNode) + assert_type( + await client.get(kind="NetworkDevice", name__value="spine1", raise_when_missing=False), InfrahubNode | None + ) + + +def _sync_client_calls(client: InfrahubClientSync) -> None: + assert_type( + client.filters(kind=NetworkDeviceSync, name__value="spine1", role__value="leaf", status__value="active"), + list[NetworkDeviceSync], + ) + assert_type(client.filters(kind=NetworkDeviceSync, include=["interfaces"]), list[NetworkDeviceSync]) + assert_type(client.filters(kind="NetworkDevice", role__value="leaf"), list[InfrahubNodeSync]) + + assert_type(client.get(kind=NetworkDeviceSync, id="1234"), NetworkDeviceSync) + assert_type(client.get(NetworkDeviceSync, name__value="spine1", role__value="leaf"), NetworkDeviceSync) + assert_type( + client.get(kind=NetworkDeviceSync, name__value="spine1", raise_when_missing=False), NetworkDeviceSync | None + ) + assert_type(client.get(kind="NetworkDevice", name__value="spine1"), InfrahubNodeSync) + assert_type( + client.get(kind="NetworkDevice", name__value="spine1", raise_when_missing=False), InfrahubNodeSync | None + ) + + +def test_mypy_accepts_the_pinned_overload_types(tmp_path: Path) -> None: + stdout, stderr, exit_status = mypy_api.run( + [ + "--config-file", + str(REPO_ROOT / "pyproject.toml"), + "--cache-dir", + str(tmp_path / "mypy_cache"), + "--show-error-codes", + __file__, + ] + ) + assert exit_status == 0, f"mypy rejected the pinned overload types:\n{stdout}{stderr}" From 95ece1ab23938f8598d7c79ddcd4d7adef8d6c5b Mon Sep 17 00:00:00 2001 From: Saltaferis Dimitrios Date: Wed, 2 Dec 2026 11:12:49 +0000 Subject: [PATCH 2/3] test: cover unpacked filters and the typing guide's call shapes Add filters(kind=P, **filters) with a dict[str, Any], a positional string kind for get(), and the guide's role__value plus include call. Run mypy with --follow-imports=silent so the test reports only errors in its own file: the mypy CI step already checks infrahub_sdk/, and the dependency-lower-bounds job would otherwise check it against the lowest dependency versions. Refs: INBOX-199 Co-Authored-By: Claude Opus 5.5 --- tests/unit/sdk/test_client_overload_types.py | 22 +++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/tests/unit/sdk/test_client_overload_types.py b/tests/unit/sdk/test_client_overload_types.py index 462fbb6b6..5cc2ada87 100644 --- a/tests/unit/sdk/test_client_overload_types.py +++ b/tests/unit/sdk/test_client_overload_types.py @@ -14,7 +14,7 @@ from __future__ import annotations from pathlib import Path -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any from mypy import api as mypy_api from typing_extensions import assert_type @@ -36,12 +36,15 @@ class NetworkDeviceSync(CoreNodeSync): """Stands in for the sync variant of a generated protocol.""" -async def _async_client_calls(client: InfrahubClient) -> None: +async def _async_client_calls(client: InfrahubClient, filters: dict[str, Any]) -> None: assert_type( await client.filters(kind=NetworkDevice, name__value="spine1", role__value="leaf", status__value="active"), list[NetworkDevice], ) - assert_type(await client.filters(kind=NetworkDevice, include=["interfaces"]), list[NetworkDevice]) + assert_type( + await client.filters(kind=NetworkDevice, role__value="leaf", include=["interfaces"]), list[NetworkDevice] + ) + assert_type(await client.filters(kind=NetworkDevice, **filters), list[NetworkDevice]) assert_type(await client.filters(kind="NetworkDevice", role__value="leaf"), list[InfrahubNode]) assert_type(await client.get(kind=NetworkDevice, id="1234"), NetworkDevice) @@ -50,17 +53,21 @@ async def _async_client_calls(client: InfrahubClient) -> None: await client.get(kind=NetworkDevice, name__value="spine1", raise_when_missing=False), NetworkDevice | None ) assert_type(await client.get(kind="NetworkDevice", name__value="spine1"), InfrahubNode) + assert_type(await client.get("NetworkDevice", name__value="spine1"), InfrahubNode) assert_type( await client.get(kind="NetworkDevice", name__value="spine1", raise_when_missing=False), InfrahubNode | None ) -def _sync_client_calls(client: InfrahubClientSync) -> None: +def _sync_client_calls(client: InfrahubClientSync, filters: dict[str, Any]) -> None: assert_type( client.filters(kind=NetworkDeviceSync, name__value="spine1", role__value="leaf", status__value="active"), list[NetworkDeviceSync], ) - assert_type(client.filters(kind=NetworkDeviceSync, include=["interfaces"]), list[NetworkDeviceSync]) + assert_type( + client.filters(kind=NetworkDeviceSync, role__value="leaf", include=["interfaces"]), list[NetworkDeviceSync] + ) + assert_type(client.filters(kind=NetworkDeviceSync, **filters), list[NetworkDeviceSync]) assert_type(client.filters(kind="NetworkDevice", role__value="leaf"), list[InfrahubNodeSync]) assert_type(client.get(kind=NetworkDeviceSync, id="1234"), NetworkDeviceSync) @@ -69,18 +76,23 @@ def _sync_client_calls(client: InfrahubClientSync) -> None: client.get(kind=NetworkDeviceSync, name__value="spine1", raise_when_missing=False), NetworkDeviceSync | None ) assert_type(client.get(kind="NetworkDevice", name__value="spine1"), InfrahubNodeSync) + assert_type(client.get("NetworkDevice", name__value="spine1"), InfrahubNodeSync) assert_type( client.get(kind="NetworkDevice", name__value="spine1", raise_when_missing=False), InfrahubNodeSync | None ) def test_mypy_accepts_the_pinned_overload_types(tmp_path: Path) -> None: + # Imported modules are analysed for their types but their errors are not reported: the mypy CI step + # already checks infrahub_sdk/, and the lower-bounds job would check it against other dependency versions. stdout, stderr, exit_status = mypy_api.run( [ "--config-file", str(REPO_ROOT / "pyproject.toml"), "--cache-dir", str(tmp_path / "mypy_cache"), + "--follow-imports", + "silent", "--show-error-codes", __file__, ] From 6195b5eeff79132f4b1ed0df64236f40ab904c5e Mon Sep 17 00:00:00 2001 From: Saltaferis Dimitrios Date: Wed, 2 Dec 2026 11:16:40 +0000 Subject: [PATCH 3/3] test: pin raise_when_missing=True and document the unpinned get() cases Add the explicit raise_when_missing=True overload for both clients, reuse PYPROJECT_FILE from tests/constants.py, and state in the module docstring which get() calls resolve to a type callers would not want and are therefore not pinned: an unpacked filters dictionary (Any) and a bool variable for raise_when_missing (the protocol type without None). Refs: INBOX-199 Co-Authored-By: Claude Opus 5.5 --- tests/unit/sdk/test_client_overload_types.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/unit/sdk/test_client_overload_types.py b/tests/unit/sdk/test_client_overload_types.py index 5cc2ada87..e8614d072 100644 --- a/tests/unit/sdk/test_client_overload_types.py +++ b/tests/unit/sdk/test_client_overload_types.py @@ -9,11 +9,17 @@ - ty, through the ``uv run ty check .`` lint step, which covers ``tests/``. - mypy, through ``test_mypy_accepts_the_pinned_overload_types``, because CI runs mypy only over ``infrahub_sdk/``. + +Not pinned, because both checkers resolve them to a type callers would not want: + +- ``get(kind=NetworkDevice, **filters)`` returns ``Any``: the unpacked dictionary could hold + ``raise_when_missing``, so every protocol overload matches. +- ``get(kind=NetworkDevice, raise_when_missing=flag)`` with ``flag: bool`` returns ``NetworkDevice``, + not ``NetworkDevice | None``. """ from __future__ import annotations -from pathlib import Path from typing import TYPE_CHECKING, Any from mypy import api as mypy_api @@ -21,11 +27,12 @@ from infrahub_sdk.node import InfrahubNode, InfrahubNodeSync from infrahub_sdk.protocols_base import CoreNode, CoreNodeSync +from tests.constants import PYPROJECT_FILE if TYPE_CHECKING: - from infrahub_sdk import InfrahubClient, InfrahubClientSync + from pathlib import Path -REPO_ROOT = Path(__file__).resolve().parents[3] + from infrahub_sdk import InfrahubClient, InfrahubClientSync class NetworkDevice(CoreNode): @@ -48,6 +55,7 @@ async def _async_client_calls(client: InfrahubClient, filters: dict[str, Any]) - assert_type(await client.filters(kind="NetworkDevice", role__value="leaf"), list[InfrahubNode]) assert_type(await client.get(kind=NetworkDevice, id="1234"), NetworkDevice) + assert_type(await client.get(kind=NetworkDevice, id="1234", raise_when_missing=True), NetworkDevice) assert_type(await client.get(NetworkDevice, name__value="spine1", role__value="leaf"), NetworkDevice) assert_type( await client.get(kind=NetworkDevice, name__value="spine1", raise_when_missing=False), NetworkDevice | None @@ -71,6 +79,7 @@ def _sync_client_calls(client: InfrahubClientSync, filters: dict[str, Any]) -> N assert_type(client.filters(kind="NetworkDevice", role__value="leaf"), list[InfrahubNodeSync]) assert_type(client.get(kind=NetworkDeviceSync, id="1234"), NetworkDeviceSync) + assert_type(client.get(kind=NetworkDeviceSync, id="1234", raise_when_missing=True), NetworkDeviceSync) assert_type(client.get(NetworkDeviceSync, name__value="spine1", role__value="leaf"), NetworkDeviceSync) assert_type( client.get(kind=NetworkDeviceSync, name__value="spine1", raise_when_missing=False), NetworkDeviceSync | None @@ -88,7 +97,7 @@ def test_mypy_accepts_the_pinned_overload_types(tmp_path: Path) -> None: stdout, stderr, exit_status = mypy_api.run( [ "--config-file", - str(REPO_ROOT / "pyproject.toml"), + str(PYPROJECT_FILE), "--cache-dir", str(tmp_path / "mypy_cache"), "--follow-imports",