From af4c70b5af272c1ef96be10f82043c0c8c6ba1af Mon Sep 17 00:00:00 2001 From: LeSingh1 Date: Sat, 8 Aug 2026 18:55:12 -0700 Subject: [PATCH] Do not report a registered type as "Unknown type" in get_cuda_native_handle get_cuda_native_handle() wraps both the registry lookup and the getter call in one try: try: return _handle_getters[obj_type](obj) except KeyError: raise TypeError("Unknown type: " + str(obj_type)) from None The except clause is meant for "this type has no registered getter", but it also fires for a KeyError raised *inside* the getter. When that happens the diagnosis is wrong twice over: the reported type is registered, and `from None` suppresses the context so the traceback that would show the real failure is gone. >>> _add_cuda_native_handle_getter(Registered, getter_that_raises_keyerror) >>> get_cuda_native_handle(Registered()) TypeError: Unknown type: Move the getter call out of the try. The unregistered-type path is unchanged, which the existing test_get_handle_error still covers. --- cuda_bindings/cuda/bindings/utils/__init__.py | 5 ++++- cuda_bindings/tests/test_utils.py | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) 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