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

AlenkaF pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new dc7ee12b87a GH-51293: [Python] Reject a null Expression in two public 
APIs (#51294)
dc7ee12b87a is described below

commit dc7ee12b87a5da858354691711fb50d3830ab961
Author: Stefan Wang <[email protected]>
AuthorDate: Mon Nov 16 02:28:06 2026 -0500

    GH-51293: [Python] Reject a null Expression in two public APIs (#51294)
    
    ### Rationale for this change
    
    Passing None to partition-key extraction or expression equality terminates 
Python instead of raising an exception. Applications cannot catch that failure.
    
    Fixes https://github.com/apache/arrow/issues/51293.
    
    ### What changes are included in this PR?
    
    Both expression parameters reject None at the Cython boundary. Regression 
tests cover these two entry points.
    
    ### Are these changes tested?
    
    Built from source on macOS arm64 with Python 3.12.14. Both calls crash on 
[upstream 
main](https://github.com/apache/arrow/commit/1f789ffd70f96907698e93ef99f4e034caf0788b)
 and raise TypeError on the rebased branch.
    
    ```console
    $ python -c 'import pyarrow.dataset as ds; ds.get_partition_keys(None)'
    Before:
    returncode=-10
    After:
    TypeError: Argument 'partition_expression' has incorrect type (expected 
pyarrow._compute.Expression, got NoneType)
    returncode=1
    
    $ python -c "import pyarrow.compute as pc; pc.field('a').equals(None)"
    Before:
    returncode=-10
    After:
    TypeError: Argument 'other' has incorrect type (expected 
pyarrow._compute.Expression, got NoneType)
    returncode=1
    ```
    
    ### Are there any user-facing changes?
    
    Null expressions raise TypeError instead of terminating Python. Valid calls 
keep their existing behavior.
    
    ### Was AI used for this PR?
    
    In accordance to the [AI generation 
guidelines](https://arrow.apache.org/docs/dev/developers/overview.html#ai-generated-code),
 please disclose below whether and how AI was used in this PR.
    
    **PR code and description written by:**
    
    - [ ] Human
    - [x] AI
    
    **Reviewed before submission by:**
    
    - [x] Human
    - [ ] AI
    - [ ] Not reviewed
    
    * GitHub Issue: #51293
    
    Authored-by: 1fanwang <[email protected]>
    Signed-off-by: AlenkaF <[email protected]>
---
 python/pyarrow/_compute.pyx          | 2 +-
 python/pyarrow/_dataset.pyx          | 2 +-
 python/pyarrow/tests/test_compute.py | 3 +++
 python/pyarrow/tests/test_dataset.py | 3 +++
 4 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/python/pyarrow/_compute.pyx b/python/pyarrow/_compute.pyx
index ee3ebd6d0e9..789e710875e 100644
--- a/python/pyarrow/_compute.pyx
+++ b/python/pyarrow/_compute.pyx
@@ -2686,7 +2686,7 @@ cdef class Expression(_Weakrefable):
     cdef inline CExpression unwrap(self):
         return self.expr
 
-    def equals(self, Expression other):
+    def equals(self, Expression other not None):
         """
         Parameters
         ----------
diff --git a/python/pyarrow/_dataset.pyx b/python/pyarrow/_dataset.pyx
index b2b400d9f3b..4820027ee82 100644
--- a/python/pyarrow/_dataset.pyx
+++ b/python/pyarrow/_dataset.pyx
@@ -4055,7 +4055,7 @@ cdef class Scanner(_Weakrefable):
         return reader
 
 
-def get_partition_keys(Expression partition_expression):
+def get_partition_keys(Expression partition_expression not None):
     """
     Extract partition keys (equality constraints between a field and a scalar)
     from an expression as a dict mapping the field's name to its value.
diff --git a/python/pyarrow/tests/test_compute.py 
b/python/pyarrow/tests/test_compute.py
index 691d2ec98f3..0e0260808d6 100644
--- a/python/pyarrow/tests/test_compute.py
+++ b/python/pyarrow/tests/test_compute.py
@@ -4371,6 +4371,9 @@ def test_expression_construction():
     with pytest.raises(TypeError):
         field.isin(1)
 
+    with pytest.raises(TypeError, match="Argument 'other' has incorrect type"):
+        field.equals(None)
+
     with pytest.raises(pa.ArrowInvalid):
         field != object()
 
diff --git a/python/pyarrow/tests/test_dataset.py 
b/python/pyarrow/tests/test_dataset.py
index e80786cb94f..fd5a418aaf2 100644
--- a/python/pyarrow/tests/test_dataset.py
+++ b/python/pyarrow/tests/test_dataset.py
@@ -952,6 +952,9 @@ def test_partition_keys():
     null = ds.field('a').is_null()
     assert ds.get_partition_keys(null) == {'a': None}
 
+    with pytest.raises(TypeError, match="Argument 'partition_expression'"):
+        ds.get_partition_keys(None)
+
 
 @pytest.mark.parquet
 def test_parquet_read_options():

Reply via email to