@@ -133,7 +133,7 @@ def find_castable_builtin_for_dtype(
133133 # None equals NumPy's xp.float64 object, so we specifically skip it here to
134134 # ensure that InvalidArgument is still raised. xp.float64 is in fact an
135135 # alias of np.dtype('float64'), and its equality with None is meant to be
136- # deprecated at some point - see https://github.com/numpy/numpy/issues/18434.
136+ # deprecated at some point. See https://github.com/numpy/numpy/issues/18434
137137 if dtype is not None and dtype in float_dtypes :
138138 return float
139139
@@ -266,28 +266,14 @@ def __init__(self, xp, elements_strategy, dtype, shape, fill, unique):
266266 self .array_size = math .prod (shape )
267267 self .builtin = find_castable_builtin_for_dtype (xp , dtype )
268268
269- def set_value (self , result , i , val , strategy = None ):
270- strategy = strategy or self .elements_strategy
271- try :
272- result [i ] = val
273- except TypeError as e :
274- raise InvalidArgument (
275- f"Could not add generated array element { val !r} "
276- f"of dtype { type (val )} to array of dtype { result .dtype } ."
277- ) from e
278- self .check_set_value (val , result [i ], strategy )
279-
280269 def check_set_value (self , val , val_0d , strategy ):
281- if self .builtin is bool :
282- finite = True
283- else :
284- finite = self .xp .isfinite (val_0d )
270+ finite = self .builtin is bool or self .xp .isfinite (val_0d )
285271 if finite and self .builtin (val_0d ) != val :
286272 raise InvalidArgument (
287273 f"Generated array element { val !r} from strategy { strategy } "
288- f"cannot be represented as dtype { self .dtype } . "
274+ f"cannot be represented with dtype { self .dtype } . "
289275 f"Array module { self .xp .__name__ } instead "
290- f"represents the element as { val_0d !r } . "
276+ f"represents the element as { val_0d } . "
291277 "Consider using a more precise elements strategy, "
292278 "for example passing the width argument to floats()."
293279 )
@@ -302,26 +288,34 @@ def do_draw(self, data):
302288 # elements strategy does not produce reusable values), so we must
303289 # generate a fully dense array with a freshly drawn value for each
304290 # entry.
305-
306- # This could legitimately be a xp.empty, but the performance gains
307- # for that are likely marginal, so there's really not much point
308- # risking undefined behaviour shenanigans.
309- result = self .xp .zeros (self .array_size , dtype = self .dtype )
310-
311- if self .unique :
312- seen = set ()
313- elems = st .lists (
291+ elems = data .draw (
292+ st .lists (
314293 self .elements_strategy ,
315294 min_size = self .array_size ,
316295 max_size = self .array_size ,
317- unique = True ,
296+ unique = self . unique ,
318297 )
319- for i , v in enumerate (data .draw (elems )):
320- self .set_value (result , i , v )
321- else :
322- for i in range (self .array_size ):
323- val = data .draw (self .elements_strategy )
324- self .set_value (result , i , val )
298+ )
299+ try :
300+ result = self .xp .asarray (elems , dtype = self .dtype )
301+ except Exception as e :
302+ if len (elems ) <= 6 :
303+ f_elems = str (elems )
304+ else :
305+ f_elems = f"[{ elems [0 ]} , { elems [1 ]} , ..., { elems [- 2 ]} , { elems [- 1 ]} ]"
306+ types = tuple (
307+ sorted ({type (e ) for e in elems }, key = lambda t : t .__name__ )
308+ )
309+ f_types = f"type { types [0 ]} " if len (types ) == 1 else f"types { types } "
310+ raise InvalidArgument (
311+ f"Generated elements { f_elems } from strategy "
312+ f"{ self .elements_strategy } could not be converted "
313+ f"to array of dtype { self .dtype } . "
314+ f"Consider if elements of { f_types } "
315+ f"are compatible with { self .dtype } ."
316+ ) from e
317+ for i in range (self .array_size ):
318+ self .check_set_value (elems [i ], result [i ], self .elements_strategy )
325319 else :
326320 # We draw arrays as "sparse with an offset". We assume not every
327321 # element will be assigned and so first draw a single value from our
@@ -338,7 +332,7 @@ def do_draw(self, data):
338332 f"with fill value { fill_val !r} "
339333 ) from e
340334 sample = result [0 ]
341- self .check_set_value (fill_val , sample , strategy = self .fill )
335+ self .check_set_value (fill_val , sample , self .fill )
342336 if self .unique and not self .xp .all (self .xp .isnan (result )):
343337 raise InvalidArgument (
344338 f"Array module { self .xp .__name__ } did not recognise fill "
@@ -371,7 +365,14 @@ def do_draw(self, data):
371365 continue
372366 else :
373367 seen .add (val )
374- self .set_value (result , i , val )
368+ try :
369+ result [i ] = val
370+ except Exception as e :
371+ raise InvalidArgument (
372+ f"Could not add generated array element { val !r} "
373+ f"of type { type (val )} to array of dtype { result .dtype } ."
374+ ) from e
375+ self .check_set_value (val , result [i ], self .elements_strategy )
375376 assigned .add (i )
376377
377378 result = self .xp .reshape (result , self .shape )
@@ -459,8 +460,9 @@ def _arrays(
459460 hundreds or more elements, having a fill value is essential if you want
460461 your tests to run in reasonable time.
461462 """
462-
463- check_xp_attributes (xp , ["zeros" , "full" , "all" , "isnan" , "isfinite" , "reshape" ])
463+ check_xp_attributes (
464+ xp , ["asarray" , "zeros" , "full" , "all" , "isnan" , "isfinite" , "reshape" ]
465+ )
464466
465467 if isinstance (dtype , st .SearchStrategy ):
466468 return dtype .flatmap (
0 commit comments