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..e8614d072 --- /dev/null +++ b/tests/unit/sdk/test_client_overload_types.py @@ -0,0 +1,109 @@ +"""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/``. + +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 typing import TYPE_CHECKING, Any + +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 +from tests.constants import PYPROJECT_FILE + +if TYPE_CHECKING: + from pathlib import Path + + from infrahub_sdk import InfrahubClient, InfrahubClientSync + + +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, 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, 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) + 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 + ) + 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, 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, 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) + 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 + ) + 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(PYPROJECT_FILE), + "--cache-dir", + str(tmp_path / "mypy_cache"), + "--follow-imports", + "silent", + "--show-error-codes", + __file__, + ] + ) + assert exit_status == 0, f"mypy rejected the pinned overload types:\n{stdout}{stderr}"