You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by ti...@apache.org on 2026/10/08 15:43:14 UTC

(datafusion-python) 03/04: docs: state that a declared function may not be a bare capsule

This is an automated email from the ASF dual-hosted git repository.

timsaucer pushed a commit to branch feat/bundle-functions
in repository https://gitbox.apache.org/repos/asf/datafusion-python.git

commit dc7bd86b773306defe8a37abbb9b823871966e13
Author: Tim Saucer <ti...@gmail.com>
AuthorDate: Thu Oct 8 09:03:53 2026 -0400

    docs: state that a declared function may not be a bare capsule
    
    udf(), udaf() and udwf() accept a bare PyCapsule, but a declaration in
    SessionExtensionComponents must be a wrapper or an object exposing the
    getter. Say so on the field, and pin it with an FFI example test that
    shows the same capsule wrapping fine through udf().
    
    Co-Authored-By: Claude Opus 5.5 (1M context) <no...@anthropic.com>
---
 .../python/tests/_test_session_extension.py        | 28 ++++++++++++++++++++--
 python/datafusion/extensions.py                    |  6 +++--
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/examples/datafusion-ffi-example/python/tests/_test_session_extension.py b/examples/datafusion-ffi-example/python/tests/_test_session_extension.py
index fadc1fd7..42a827fc 100644
--- a/examples/datafusion-ffi-example/python/tests/_test_session_extension.py
+++ b/examples/datafusion-ffi-example/python/tests/_test_session_extension.py
@@ -21,8 +21,8 @@ from __future__ import annotations
 
 import pyarrow as pa
 import pytest
-from datafusion import SessionContext, SessionExtensionComponents
-from datafusion_ffi_example import MyFunctionExtension
+from datafusion import SessionContext, SessionExtensionComponents, udf
+from datafusion_ffi_example import IsNullUDF, MyFunctionExtension
 
 
 def _session():
@@ -96,6 +96,30 @@ def test_installing_the_library_twice_is_refused():
         ctx.udf("my_custom_is_null")
 
 
+def test_a_bare_capsule_is_not_a_declaration():
+    """A declared function is an object, even though ``udf`` takes a capsule.
+
+    The capsule itself is fine -- ``udf`` wraps it -- so the refusal is the
+    declaration rule, not a bad capsule.
+    """
+    ctx = SessionContext()
+    capsule = IsNullUDF().__datafusion_scalar_udf__()
+
+    class CapsuleExtension:
+        def __datafusion_session_components__(
+            self, ctx: SessionContext
+        ) -> SessionExtensionComponents:
+            return SessionExtensionComponents(udfs=(capsule,))
+
+    with pytest.raises(TypeError, match=r"__datafusion_scalar_udf__"):
+        ctx.with_extensions(CapsuleExtension())
+
+    with pytest.raises(KeyError):
+        ctx.udf("my_custom_is_null")
+
+    assert udf(capsule).name == "my_custom_is_null"
+
+
 def test_a_failure_after_the_hook_registers_nothing():
     """The transaction covers functions imported across the FFI boundary too."""
     ctx = SessionContext()
diff --git a/python/datafusion/extensions.py b/python/datafusion/extensions.py
index 61b59088..0ddb4429 100644
--- a/python/datafusion/extensions.py
+++ b/python/datafusion/extensions.py
@@ -276,8 +276,10 @@ class SessionExtensionComponents:
 
     Either a :py:class:`~datafusion.user_defined.ScalarUDF` or an object
     exposing ``__datafusion_scalar_udf__``, which is wrapped with
-    :py:func:`~datafusion.udf` on the way in. The registered name comes from
-    the function itself, not from this field.
+    :py:func:`~datafusion.udf` on the way in. A bare ``PyCapsule`` is refused,
+    though :py:func:`~datafusion.udf` accepts one; declare the object that
+    exports it. The registered name comes from the function itself, not from
+    this field.
 
     One :py:meth:`~datafusion.context.SessionContext.with_extensions` call may
     not declare a name twice, whether the two claims come from one extension or


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org