|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import importlib.metadata |
| 4 | +import inspect |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +def _get_signature_params(callable_obj) -> tuple[set[str], bool]: |
| 10 | + """ |
| 11 | + Return allowed keyword names for callable, allowing for **kwargs. |
| 12 | + |
| 13 | + Example: |
| 14 | + >>> params, accepts_var_kw = _get_signature_params(lambda x, y, **kwargs: None, {"x", "y"}) |
| 15 | + >>> params == {"x", "y"} |
| 16 | + True |
| 17 | + >>> accepts_var_kw |
| 18 | + True |
| 19 | + """ |
| 20 | + sig = inspect.signature(callable_obj) |
| 21 | + params = sig.parameters |
| 22 | + accepts_var_kw = any(p.kind == inspect.Parameter.VAR_KEYWORD for p in params.values()) |
| 23 | + return params, accepts_var_kw |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.dlclive_compat |
| 27 | +def test_dlclive_package_is_importable(): |
| 28 | + from dlclive import DLCLive # noqa: PLC0415 |
| 29 | + |
| 30 | + assert DLCLive is not None |
| 31 | + # Helpful for CI logs to confirm matrix install result. |
| 32 | + _ = importlib.metadata.version("deeplabcut-live") |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.dlclive_compat |
| 36 | +def test_dlclive_constructor_accepts_gui_expected_kwargs(): |
| 37 | + """ |
| 38 | + GUI passes these kwargs when constructing DLCLive. |
| 39 | + This test catches upstream API changes that would break initialization. |
| 40 | + """ |
| 41 | + from dlclive import DLCLive # noqa: PLC0415 |
| 42 | + |
| 43 | + expected = { |
| 44 | + "model_path", |
| 45 | + "model_type", |
| 46 | + "processor", |
| 47 | + "dynamic", |
| 48 | + "resize", |
| 49 | + "precision", |
| 50 | + "single_animal", |
| 51 | + "device", |
| 52 | + } |
| 53 | + params, accepts_var_kw = _get_signature_params(DLCLive.__init__) |
| 54 | + missing = {name for name in expected if name not in params} |
| 55 | + assert not missing, f"DLCLive.__init__ is missing expected kwargs called by GUI: {sorted(missing)}" |
| 56 | + assert accepts_var_kw, "DLCLive.__init__ should accept **kwargs" # captures current behavior |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.dlclive_compat |
| 60 | +def test_dlclive_methods_match_gui_usage(): |
| 61 | + """ |
| 62 | + GUI expects: |
| 63 | + - init_inference(frame) |
| 64 | + - get_pose(frame, frame_time=<float>) |
| 65 | + """ |
| 66 | + from dlclive import DLCLive # noqa: PLC0415 |
| 67 | + |
| 68 | + assert hasattr(DLCLive, "init_inference"), "DLCLive must provide init_inference(frame)" |
| 69 | + assert hasattr(DLCLive, "get_pose"), "DLCLive must provide get_pose(frame, frame_time=...)" |
| 70 | + |
| 71 | + init_params, _ = _get_signature_params(DLCLive.init_inference) |
| 72 | + init_missing = {name for name in {"frame"} if name not in init_params} |
| 73 | + assert not init_missing, f"DLCLive.init_inference signature mismatch, missing: {sorted(init_missing)}" |
| 74 | + |
| 75 | + get_pose_params, _ = _get_signature_params(DLCLive.get_pose) |
| 76 | + get_pose_missing = {name for name in {"frame", "frame_time"} if name not in get_pose_params} |
| 77 | + assert not get_pose_missing, f"DLCLive.get_pose signature mismatch, missing: {sorted(get_pose_missing)}" |
0 commit comments