diff --git a/cuda_bindings/cuda/bindings/utils/__init__.py b/cuda_bindings/cuda/bindings/utils/__init__.py index 0bfff4b78be..9c29bb4dd81 100644 --- a/cuda_bindings/cuda/bindings/utils/__init__.py +++ b/cuda_bindings/cuda/bindings/utils/__init__.py @@ -27,6 +27,9 @@ def get_cuda_native_handle(obj: Any) -> int: """ obj_type = type(obj) try: - return _handle_getters[obj_type](obj) + getter = _handle_getters[obj_type] except KeyError: raise TypeError("Unknown type: " + str(obj_type)) from None + # Deliberately outside the try: a KeyError raised by the getter itself is a + # bug in that getter, not an unregistered type. + return getter(obj) diff --git a/cuda_bindings/tests/test_utils.py b/cuda_bindings/tests/test_utils.py index c767996bced..84f7ca7b722 100644 --- a/cuda_bindings/tests/test_utils.py +++ b/cuda_bindings/tests/test_utils.py @@ -115,6 +115,27 @@ def test_get_handle_error(target): handle = get_cuda_native_handle(target) +@pytest.mark.agent_authored(model="claude-opus-5") +def test_get_handle_does_not_report_a_registered_type_as_unknown(monkeypatch): + """A KeyError from inside a handle getter is a bug in that getter. + + Reporting it as "Unknown type" is wrong twice over: the type *is* + registered, and `from None` hides the traceback that would say otherwise. + """ + from cuda.bindings.utils import _handle_getters + + class Registered: + pass + + def getter(_obj): + raise KeyError("lookup inside the getter failed") + + monkeypatch.setitem(_handle_getters, Registered, getter) + + with pytest.raises(KeyError, match="lookup inside the getter failed"): + get_cuda_native_handle(Registered()) + + @pytest.mark.parametrize( "module", # Top-level modules for external Python use