AlenkaF commented on code in PR #38334:
URL: https://github.com/apache/arrow/pull/38334#discussion_r2043839573


##########
python/pyarrow/array.pxi:
##########
@@ -4220,23 +4220,46 @@ cdef class StructArray(Array):
 
         c_mask = c_mask_inverted_from_obj(mask, memory_pool)
 
-        arrays = [asarray(x) for x in arrays]
-        for arr in arrays:
-            c_array = pyarrow_unwrap_array(arr)
-            if c_array == nullptr:
-                raise TypeError(f"Expected Array, got {arr.__class__}")
-            c_arrays.push_back(c_array)
         if names is not None:
             for name in names:
                 c_names.push_back(tobytes(name))
+            arrays = [asarray(x) for x in arrays]
         else:
+            py_fields = []
             for item in fields:
                 if isinstance(item, tuple):
                     py_field = field(*item)
                 else:
                     py_field = item
+                py_fields.append(py_field)
                 c_fields.push_back(py_field.sp_field)
 
+            if len(py_fields) != len(arrays):
+                raise ValueError("Must pass same number of arrays as fields")
+            type_enforced_arrays = []
+            for ary, field in zip(arrays, fields):
+                # Verify the data type of arrays which are already
+                # Arrow Arrays. Coerce the data type of anything else,
+                # since that other stuff doesn't have a certain type
+                # we can verify.
+                if isinstance(ary, (Array, ChunkedArray)):
+                    if ary.type != field.type:
+                        raise ValueError(
+                            f"Field {field.name} is expected to have type 
{field.type}, but provided data is {ary.type}")
+                    type_enforced_arrays.append(ary)

Review Comment:
   I see this was added to preserve the original behavior in cases where the 
underlying child array has a different type than the one specified in the 
field. I agree that it's not entirely clear what the correct behavior should 
be, but I think we shouldn't change it here.
   
   Given that, would it make sense to remove the type check here and rely on 
the C++ layer to raise an error, as it currently does?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to