@@ -949,8 +949,7 @@ def outer(a: Any, b: Any) -> Any:
949
949
Tweaks the behavior of :func:`numpy.outer` to return a lower-dimensional
950
950
object if either/both of *a* and *b* are scalars (whereas :func:`numpy.outer`
951
951
always returns a matrix). Here the definition of "scalar" includes
952
- all non-array-container types and any scalar-like array container types
953
- (including non-object numpy arrays).
952
+ all non-array-container types and any scalar-like array container types.
954
953
955
954
If *a* and *b* are both array containers, the result will have the same type
956
955
as *a*. If both are array containers and neither is an object array, they must
@@ -968,12 +967,19 @@ def treat_as_scalar(x: Any) -> bool:
968
967
# This condition is whether "ndarrays should broadcast inside x".
969
968
and NumpyObjectArray not in x .__class__ ._outer_bcast_types )
970
969
970
+ a_is_ndarray = isinstance (a , np .ndarray )
971
+ b_is_ndarray = isinstance (b , np .ndarray )
972
+
973
+ if a_is_ndarray and a .dtype != object :
974
+ raise TypeError ("passing a non-object numpy array is not allowed" )
975
+ if b_is_ndarray and b .dtype != object :
976
+ raise TypeError ("passing a non-object numpy array is not allowed" )
977
+
971
978
if treat_as_scalar (a ) or treat_as_scalar (b ):
972
979
return a * b
973
- # After this point, "isinstance(o, ndarray)" means o is an object array.
974
- elif isinstance (a , np .ndarray ) and isinstance (b , np .ndarray ):
980
+ elif a_is_ndarray and b_is_ndarray :
975
981
return np .outer (a , b )
976
- elif isinstance ( a , np . ndarray ) or isinstance ( b , np . ndarray ) :
982
+ elif a_is_ndarray or b_is_ndarray :
977
983
return map_array_container (lambda x : outer (x , b ), a )
978
984
else :
979
985
if type (a ) is not type (b ):
0 commit comments