@@ -544,12 +544,22 @@ def test_subnormal_elements_validation(xp, xps):
544544 check_can_generate_examples (strat )
545545
546546
547- def _array_strategy (xp , xps , dtype ):
548- """An ArrayStrategy for the given dtype, for exercising check_set_value()
549- directly with contrived (val, val_0d) pairs that real array modules are
550- unlikely to produce (e.g. modern NumPy raises rather than silently
551- mismatching on integer overflow)."""
552- return ArrayStrategy (
547+ @pytest .mark .parametrize (
548+ "dtype_name, val, stored, match" ,
549+ [
550+ ("int8" , 5 , 6 , "cannot be represented" ),
551+ ("float32" , 5.0 , 6.0 , "cannot be represented" ),
552+ # a non-numeric element makes the subnormal check itself error, which
553+ # still reports the generic message rather than propagating
554+ ("float32" , "not a float" , 6.0 , "cannot be represented" ),
555+ ("float32" , width_smallest_normals (32 ) / 2 , 0.0 , "Generated subnormal" ),
556+ ],
557+ )
558+ def test_check_set_value_mismatch_errors (xp , xps , dtype_name , val , stored , match ):
559+ """check_set_value() explains elements which don't round-trip through the
560+ array module, with a subnormal-specific message where relevant."""
561+ dtype = getattr (xp , dtype_name )
562+ strategy = ArrayStrategy (
553563 xp = xp ,
554564 api_version = xps .api_version ,
555565 elements_strategy = st .just (0 ),
@@ -558,51 +568,14 @@ def _array_strategy(xp, xps, dtype):
558568 fill = st .nothing (),
559569 unique = False ,
560570 )
561-
562-
563- def test_check_set_value_raises_generic_error_for_non_float_mismatch (xp , xps ):
564- """check_set_value() raises the generic mismatch error (not the
565- subnormal-specific one) for non-float dtypes."""
566- strategy = _array_strategy (xp , xps , xp .int8 )
567- val_0d = xp .asarray (6 , dtype = xp .int8 )
568- with pytest .raises (InvalidArgument , match = "cannot be represented" ):
569- strategy .check_set_value (5 , val_0d , st .just (5 ))
570-
571-
572- def test_check_set_value_raises_generic_error_for_non_subnormal_float_mismatch (xp , xps ):
573- """check_set_value() raises the generic mismatch error for a float
574- mismatch that is not subnormal."""
575- strategy = _array_strategy (xp , xps , xp .float32 )
576- val_0d = xp .asarray (6.0 , dtype = xp .float32 )
577- with pytest .raises (InvalidArgument , match = "cannot be represented" ):
578- strategy .check_set_value (5.0 , val_0d , st .just (5.0 ))
579-
580-
581- def test_check_set_value_raises_generic_error_when_subnormal_check_errors (xp , xps ):
582- """When the generated element doesn't support abs()/comparison (so the
583- subnormal check itself raises), check_set_value() still reports the
584- generic mismatch error rather than propagating the error."""
585- strategy = _array_strategy (xp , xps , xp .float32 )
586- val_0d = xp .asarray (6.0 , dtype = xp .float32 )
587- with pytest .raises (InvalidArgument , match = "cannot be represented" ):
588- strategy .check_set_value ("not a float" , val_0d , st .just ("not a float" ))
589-
590-
591- def test_check_set_value_raises_for_subnormal_mismatch (xp , xps ):
592- """check_set_value() raises a subnormal-specific error when a subnormal
593- float element does not round-trip through the array module."""
594- strategy = _array_strategy (xp , xps , xp .float32 )
595- subnormal = width_smallest_normals (32 ) / 2
596- val_0d = xp .asarray (0.0 , dtype = xp .float32 )
597- with pytest .raises (InvalidArgument , match = "Generated subnormal float" ):
598- strategy .check_set_value (subnormal , val_0d , st .just (subnormal ))
571+ with pytest .raises (InvalidArgument , match = match ):
572+ strategy .check_set_value (val , xp .asarray (stored , dtype = dtype ), st .just (val ))
599573
600574
601575@pytest .mark .parametrize ("shape" , [3 , 10 ])
602576def test_reports_conversion_failure_for_dense_arrays (xp , xps , shape ):
603577 """When elements cannot be converted to the array's dtype in the fully
604- dense (fill=nothing) code path, a helpful error is raised - listing the
605- elements in full for small arrays, and a truncated form for larger ones."""
578+ dense (fill=nothing) code path, a helpful error is raised."""
606579 strat = xps .arrays (
607580 dtype = xp .int8 , shape = shape , elements = st .just (300 ), fill = st .nothing ()
608581 )
0 commit comments