rok commented on code in PR #50203:
URL: https://github.com/apache/arrow/pull/50203#discussion_r3468697553


##########
python/pyarrow/array.pxi:
##########
@@ -266,6 +266,26 @@ def array(object obj, type=None, mask=None, size=None, 
from_pandas=None,
     if type is not None and type.id == _Type_EXTENSION:
         extension_type = type
         type = type.storage_type
+        # GH-49644: when building a fixed_shape_tensor from a sequence of 
arrays,
+        # the converter only sees the flat storage type, so validate the
+        # tensor-specific constraints here where the type is still known.
+        if (isinstance(extension_type, FixedShapeTensorType)
+                and isinstance(obj, Sequence)
+                and not _is_array_like(obj)
+                and not isinstance(obj, (str, bytes, bytearray))):
+            if extension_type.permutation is not None:
+                raise NotImplementedError(
+                    "Converting a sequence of arrays to a fixed_shape_tensor 
with "
+                    "a permutation is not supported; use "
+                    "FixedShapeTensorArray.from_numpy_ndarray instead")
+            if np is not None:
+                expected_shape = tuple(extension_type.shape)
+                for element in obj:
+                    if (isinstance(element, np.ndarray) and element.ndim >= 2
+                            and tuple(element.shape) != expected_shape):
+                        raise ValueError(
+                            f"Cannot convert array of shape {element.shape} to 
a "
+                            f"fixed_shape_tensor of shape {expected_shape}")

Review Comment:
   Perhaps this is defensive enough? We're not necessarily limiting this to 
numpy only @AlenkaF ? Though I don't know of a realistic alternative :)
   ```suggestion
               expected_shape = tuple(extension_type.shape)
               if any(tuple(element.shape) != expected_shape) for element in 
obj):
                   raise ValueError(
                       f"Cannot convert array of shape {element.shape} to a "
                       f"fixed_shape_tensor of shape {expected_shape}")
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to