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 0974e94ef5c GH-51043: [Python] Reject null required Arrow objects 
(#51161)
0974e94ef5c is described below

commit 0974e94ef5c2d510250923a7af239ec14a522700
Author: Stefan Wang <[email protected]>
AuthorDate: Fri Nov 13 10:52:03 2026 -0500

    GH-51043: [Python] Reject null required Arrow objects (#51161)
    
    ### Rationale for this change
    
    Passing None where pyarrow requires an object can abort the Python process 
or raise an unrelated AttributeError, because the value reaches native Arrow 
code that cannot handle it. The original report covered some constructors, and 
later comments found the same failure in ScanNodeOptions, ParquetSchema, 
ColumnSchema, SubTreeFileSystem and ProjectNodeOptions, so this PR fixes those 
too.
    
    Fixes https://github.com/apache/arrow/issues/51043.
    
    get_partition_keys(None) is in https://github.com/apache/arrow/pull/51294.
    
    ### What changes are included in this PR?
    
    The Python layer now checks required arguments before native code runs, so 
None raises a TypeError that names the argument instead of crashing. This 
covers schemas, formats, types, dictionaries, fragments, filesystems and child 
arrays, plus the classes listed above. Array buffer constructors also require a 
data type.
    
    ### Are these changes tested?
    
    A native Arrow 26.0.0-SNAPSHOT build reproduced the failures. The same 
commands then raised TypeError.
    
    ```console
    $ python -c 'from pyarrow import acero; acero.ScanNodeOptions(None)'
    Before: process aborted (SIGBUS)
    After: TypeError: Argument 'dataset' has incorrect type (expected 
pyarrow._dataset.Dataset, got NoneType)
    
    $ python -c 'import pyarrow.parquet as pq; pq.ParquetSchema(None)'
    Before: process aborted (SIGSEGV)
    After: TypeError: Argument 'container' has incorrect type (expected 
pyarrow._parquet.FileMetaData, got NoneType)
    
    $ python -c 'import pyarrow as pa; pa.Array.from_buffers(type=None, 
length=0, buffers=[])'
    Before: AttributeError: 'NoneType' object has no attribute 'num_fields'
    After: TypeError: Argument 'type' has incorrect type (expected 
pyarrow.lib.DataType, got NoneType)
    ```
    
    ProjectNodeOptions([None]) and SubTreeFileSystem("", None) aborted with 
SIGBUS. ColumnSchema(None, 0) aborted on .name with SIGSEGV. All three now 
raise TypeError.
    
    ### Are there any user-facing changes?
    
    Missing required objects raise TypeError. Valid null buffers keep their 
existing behavior.
    
    [New Contributor's 
Guide](https://arrow.apache.org/docs/dev/developers/guide/step_by_step/pr_lifecycle.html#reviews-and-merge-of-the-pull-request)
 |
    [Contributing 
Overview](https://arrow.apache.org/docs/dev/developers/overview.html) |
    [AI-generated Code 
Guidance](https://arrow.apache.org/docs/dev/developers/overview.html#ai-generated-code)
    
    ### Was AI used for this PR?
    
    AI assisted with the code, tests, and description.
    
    **PR code and description written by:**
    
    - [ ] Human
    - [x] AI
    
    **Reviewed before submission by:**
    
    - [x] Human
    - [ ] AI
    - [ ] Not reviewed
    
    * GitHub Issue: #51043
    
    Authored-by: 1fanwang <[email protected]>
    Signed-off-by: AlenkaF <[email protected]>
---
 python/pyarrow/_acero.pyx                     |  2 ++
 python/pyarrow/_dataset.pyx                   |  7 +++++--
 python/pyarrow/_fs.pyx                        |  2 +-
 python/pyarrow/_parquet.pyx                   |  9 +++++----
 python/pyarrow/array.pxi                      | 15 +++++++++------
 python/pyarrow/tests/parquet/test_metadata.py | 12 ++++++++++++
 python/pyarrow/tests/test_acero.py            |  5 +++++
 python/pyarrow/tests/test_array.py            | 17 +++++++++++++++++
 python/pyarrow/tests/test_dataset.py          |  6 ++++++
 python/pyarrow/tests/test_fs.py               |  3 +++
 10 files changed, 65 insertions(+), 13 deletions(-)

diff --git a/python/pyarrow/_acero.pyx b/python/pyarrow/_acero.pyx
index e27509469ef..0a528505693 100644
--- a/python/pyarrow/_acero.pyx
+++ b/python/pyarrow/_acero.pyx
@@ -141,6 +141,8 @@ cdef class _ProjectNodeOptions(ExecNodeOptions):
             vector[c_string] c_names
 
         for expr in expressions:
+            if expr is None:
+                raise TypeError("Expression must not be None")
             c_expressions.push_back(expr.unwrap())
 
         if names is not None:
diff --git a/python/pyarrow/_dataset.pyx b/python/pyarrow/_dataset.pyx
index 8889090801c..b2b400d9f3b 100644
--- a/python/pyarrow/_dataset.pyx
+++ b/python/pyarrow/_dataset.pyx
@@ -1121,7 +1121,8 @@ cdef class FileSystemDataset(Dataset):
     cdef:
         CFileSystemDataset* filesystem_dataset
 
-    def __init__(self, fragments, Schema schema, FileFormat format,
+    def __init__(self, fragments, Schema schema not None,
+                 FileFormat format not None,
                  FileSystem filesystem=None, root_partition=None):
         cdef:
             FileFragment fragment=None
@@ -1138,6 +1139,8 @@ cdef class FileSystemDataset(Dataset):
             )
 
         for fragment in fragments:
+            if fragment is None:
+                raise TypeError("Fragment must not be None")
             c_fragments.push_back(
                 static_pointer_cast[CFileFragment, CFragment](
                     fragment.unwrap()))
@@ -4236,5 +4239,5 @@ class ScanNodeOptions(_ScanNodeOptions):
         Preserve implicit ordering of data.
     """
 
-    def __init__(self, Dataset dataset, **kwargs):
+    def __init__(self, Dataset dataset not None, **kwargs):
         self._set_options(dataset, kwargs)
diff --git a/python/pyarrow/_fs.pyx b/python/pyarrow/_fs.pyx
index 0739b6acba3..10b6ac52dd5 100644
--- a/python/pyarrow/_fs.pyx
+++ b/python/pyarrow/_fs.pyx
@@ -1205,7 +1205,7 @@ cdef class SubTreeFileSystem(FileSystem):
     For usage of the methods see examples for 
:func:`~pyarrow.fs.LocalFileSystem`.
     """
 
-    def __init__(self, base_path, FileSystem base_fs):
+    def __init__(self, base_path, FileSystem base_fs not None):
         cdef:
             c_string pathstr
             shared_ptr[CSubTreeFileSystem] wrapped
diff --git a/python/pyarrow/_parquet.pyx b/python/pyarrow/_parquet.pyx
index afe6ee99450..fb963f31368 100644
--- a/python/pyarrow/_parquet.pyx
+++ b/python/pyarrow/_parquet.pyx
@@ -755,7 +755,8 @@ cdef class SortingColumn:
         self.nulls_first = nulls_first
 
     @classmethod
-    def from_ordering(cls, Schema schema, sort_keys, null_placement='at_end'):
+    def from_ordering(cls, Schema schema not None, sort_keys,
+                      null_placement='at_end'):
         """
         Create a tuple of SortingColumn objects from the same arguments as
         :class:`pyarrow.compute.SortOptions`.
@@ -816,7 +817,7 @@ cdef class SortingColumn:
         return tuple(sorting_columns)
 
     @staticmethod
-    def to_ordering(Schema schema, sorting_columns):
+    def to_ordering(Schema schema not None, sorting_columns):
         """
         Convert a tuple of SortingColumn objects to the same format as
         :class:`pyarrow.compute.SortOptions`.
@@ -1247,7 +1248,7 @@ cdef class FileMetaData(_Weakrefable):
 cdef class ParquetSchema(_Weakrefable):
     """A Parquet schema."""
 
-    def __cinit__(self, FileMetaData container):
+    def __cinit__(self, FileMetaData container not None):
         self.parent = container
         self.schema = container._metadata.schema()
 
@@ -1336,7 +1337,7 @@ cdef class ColumnSchema(_Weakrefable):
         ParquetSchema parent
         const ColumnDescriptor* descr
 
-    def __cinit__(self, ParquetSchema schema, int index):
+    def __cinit__(self, ParquetSchema schema not None, int index):
         self.parent = schema
         self.index = index  # for pickling support
         self.descr = schema.schema.Column(index)
diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi
index e090b1d0b3d..239d968f69e 100644
--- a/python/pyarrow/array.pxi
+++ b/python/pyarrow/array.pxi
@@ -1329,8 +1329,8 @@ cdef class Array(_PandasConvertible):
             (_reduce_array_data(self.sp_array.get().data().get()),)
 
     @staticmethod
-    def from_buffers(DataType type, length, buffers, null_count=-1, offset=0,
-                     children=None):
+    def from_buffers(DataType type not None, length, buffers, null_count=-1,
+                     offset=0, children=None):
         """
         Construct an Array from a sequence of buffers.
 
@@ -1386,6 +1386,8 @@ cdef class Array(_PandasConvertible):
             c_buffers.push_back(pyarrow_unwrap_buffer(buf))
 
         for child in children:
+            if child is None:
+                raise TypeError("Array child must not be None")
             c_child_data.push_back(child.ap.data())
 
         array_data = CArrayData.MakeWithChildren(type.sp_type, length,
@@ -4328,8 +4330,9 @@ cdef class DictionaryArray(Array):
         return self._indices
 
     @staticmethod
-    def from_buffers(DataType type, int64_t length, buffers, Array dictionary,
-                     int64_t null_count=-1, int64_t offset=0):
+    def from_buffers(DataType type not None, int64_t length, buffers,
+                     Array dictionary not None, int64_t null_count=-1,
+                     int64_t offset=0):
         """
         Construct a DictionaryArray from buffers.
 
@@ -4745,8 +4748,8 @@ cdef class RunEndEncodedArray(Array):
                                                run_ends, values, 0)
 
     @staticmethod
-    def from_buffers(DataType type, length, buffers, null_count=-1, offset=0,
-                     children=None):
+    def from_buffers(DataType type not None, length, buffers, null_count=-1,
+                     offset=0, children=None):
         """
         Construct a RunEndEncodedArray from all the parameters that make up an
         Array.
diff --git a/python/pyarrow/tests/parquet/test_metadata.py 
b/python/pyarrow/tests/parquet/test_metadata.py
index 817566125fe..8e9c6c403f1 100644
--- a/python/pyarrow/tests/parquet/test_metadata.py
+++ b/python/pyarrow/tests/parquet/test_metadata.py
@@ -351,6 +351,12 @@ def test_parquet_sorting_column():
     with pytest.raises(ValueError):
         pq.SortingColumn.from_ordering(schema, (("a", "not a valid sort 
order")))
 
+    with pytest.raises(TypeError, match="Argument 'schema' has incorrect 
type"):
+        pq.SortingColumn.from_ordering(None, ())
+
+    with pytest.raises(TypeError, match="Argument 'schema' has incorrect 
type"):
+        pq.SortingColumn.to_ordering(None, ())
+
     with pytest.raises(ValueError, match="inconsistent null placement"):
         sorting_cols = (
             pq.SortingColumn(1, nulls_first=True),
@@ -871,6 +877,12 @@ def test_internal_class_instantiation():
     with pytest.raises(TypeError, match=msg("FileMetaData")):
         pq.FileMetaData()
 
+    with pytest.raises(TypeError, match="Argument 'container'"):
+        pq.ParquetSchema(None)
+
+    with pytest.raises(TypeError, match="Argument 'schema'"):
+        pq.ColumnSchema(None, 0)
+
 
 def test_read_schema_uuid_extension_type(tmp_path):
     # These are the raw 16-byte payloads for
diff --git a/python/pyarrow/tests/test_acero.py 
b/python/pyarrow/tests/test_acero.py
index 6e471d61199..bbda3abee19 100644
--- a/python/pyarrow/tests/test_acero.py
+++ b/python/pyarrow/tests/test_acero.py
@@ -247,6 +247,8 @@ def test_project(table_source):
     # input validation
     with pytest.raises(ValueError):
         ProjectNodeOptions([pc.multiply(field("a"), 2)], ["a2", "b2"])
+    with pytest.raises(TypeError, match="Expression must not be None"):
+        ProjectNodeOptions([None])
 
     # no scalar expression
     decl = Declaration.from_sequence([
@@ -597,3 +599,6 @@ def test_scan(tempdir):
     assert result["a"].to_pylist() == [1, 2, 3]
     # "b" is still included, but without data as it will be removed by the 
projection
     assert pc.all(result["b"].is_null()).as_py()
+
+    with pytest.raises(TypeError, match="Argument 'dataset'"):
+        ScanNodeOptions(None)
diff --git a/python/pyarrow/tests/test_array.py 
b/python/pyarrow/tests/test_array.py
index 63c0eb0bea4..6e19fbd9583 100644
--- a/python/pyarrow/tests/test_array.py
+++ b/python/pyarrow/tests/test_array.py
@@ -784,6 +784,9 @@ def test_array_from_buffers():
     with pytest.raises(TypeError):
         pa.Array.from_buffers(pa.int16(), 3, ['', ''], offset=1)
 
+    with pytest.raises(TypeError, match="Argument 'type' has incorrect type"):
+        pa.Array.from_buffers(type=None, length=0, buffers=[])
+
 
 def test_string_binary_from_buffers():
     array = pa.array(["a", None, "b", "c"])
@@ -868,6 +871,10 @@ def test_list_from_buffers(list_type_factory):
         pa.Array.from_buffers(ty, 4, buffers[:ty.num_buffers],
                               children=[child, child])
 
+    with pytest.raises(TypeError, match="Array child must not be None"):
+        pa.Array.from_buffers(
+            type=ty, length=4, buffers=buffers[:ty.num_buffers], 
children=[None])
+
 
 def test_struct_from_buffers():
     ty = pa.struct([pa.field('a', pa.int16()), pa.field('b', pa.utf8())])
@@ -987,6 +994,13 @@ def test_dictionary_from_buffers(offset):
                                         offset=offset)
     assert a[offset:] == b
 
+    with pytest.raises(TypeError, match="Argument 'type' has incorrect type"):
+        pa.DictionaryArray.from_buffers(
+            None, len(a), a.indices.buffers(), a.dictionary)
+    with pytest.raises(TypeError, match="Argument 'dictionary' has incorrect 
type"):
+        pa.DictionaryArray.from_buffers(
+            a.type, len(a), a.indices.buffers(), None)
+
 
 @pytest.mark.numpy
 def test_dictionary_from_numpy():
@@ -4275,6 +4289,9 @@ def test_run_end_encoded_from_buffers():
         pa.RunEndEncodedArray.from_buffers(ree_type, length, buffers,
                                            1, offset, children)
 
+    with pytest.raises(TypeError, match="Argument 'type' has incorrect type"):
+        pa.RunEndEncodedArray.from_buffers(type=None, length=0, buffers=[])
+
 
 @pytest.mark.numpy
 def test_run_end_encoded_from_array_with_type():
diff --git a/python/pyarrow/tests/test_dataset.py 
b/python/pyarrow/tests/test_dataset.py
index 3b575e24d5e..e80786cb94f 100644
--- a/python/pyarrow/tests/test_dataset.py
+++ b/python/pyarrow/tests/test_dataset.py
@@ -396,6 +396,12 @@ def test_filesystem_dataset(mockfs):
     # validation of required arguments
     with pytest.raises(TypeError, match="incorrect type"):
         ds.FileSystemDataset(fragments, file_format, schema)
+    with pytest.raises(TypeError, match="Fragment must not be None"):
+        ds.FileSystemDataset([None], schema=schema, format=file_format)
+    with pytest.raises(TypeError, match="Argument 'schema' has incorrect 
type"):
+        ds.FileSystemDataset(fragments, schema=None, format=file_format)
+    with pytest.raises(TypeError, match="Argument 'format' has incorrect 
type"):
+        ds.FileSystemDataset(fragments, schema=schema, format=None)
     # validation of root_partition
     with pytest.raises(TypeError, match="incorrect type"):
         ds.FileSystemDataset(fragments, schema=schema,
diff --git a/python/pyarrow/tests/test_fs.py b/python/pyarrow/tests/test_fs.py
index 5bf1950c065..416c75bccd2 100644
--- a/python/pyarrow/tests/test_fs.py
+++ b/python/pyarrow/tests/test_fs.py
@@ -635,6 +635,9 @@ def test_subtree_filesystem():
     assert repr(subfs).startswith('SubTreeFileSystem(base_path=/another/base/,'
                                   ' base_fs=<pyarrow._fs.LocalFileSystem')
 
+    with pytest.raises(TypeError, match="Argument 'base_fs'"):
+        SubTreeFileSystem('/base', None)
+
 
 class _PickleModuleSubTreeFileSystemWrapper:
     def __init__(self, pickle_module):

Reply via email to