docstr: Test that in most cases:
     * `np.array(scalar, dtype=dtype)`
     * `np.empty((), dtype=dtype)[()] = scalar`
     * `np.array(scalar).astype(dtype)`
  should behave the same.  The only exceptions are parametric dtypes
  (mainly datetime/timedelta without unit) and void without fields.
docstr_tok: ["Test","that","in","most","cases",":","*","`","np.array","(","scalar",",","dtype=dtype",")","`","*","`","np.empty","(","(",")",",","dtype=dtype",")","[","(",")","]","=","scalar","`","*","`","np.array","(","scalar",")",".astype","(","dtype",")","`","should","behave","the","same",".","The","only","exceptions","are","parametric","dtypes","(","mainly","datetime\/timedelta","without","unit",")","and","void","without","fields","."]
code: def test_scalar_coercion_same_as_cast_and_assignment(self, cast_to):
      """
      Test that in most cases:
         * `np.array(scalar, dtype=dtype)`
         * `np.empty((), dtype=dtype)[()] = scalar`
         * `np.array(scalar).astype(dtype)`
      should behave the same.  The only exceptions are parametric dtypes
      (mainly datetime/timedelta without unit) and void without fields.
      """
      dtype = cast_to.dtype  # use to parametrize only the target dtype
  
      for scalar in scalar_instances(times=False):
          scalar = scalar.values[0]
  
          if dtype.type == np.void:
              if (
                  scalar.dtype.fields is not None
                  and dtype.fields is None
              ):
                  # Here, coercion to "V6" works, but the cast fails.
                  # Since the types are identical, SETITEM takes care of
                  # this, but has different rules than the cast.
                  with pytest.raises(TypeError):
                      np.array(scalar).astype(dtype)
                  np.array(scalar, dtype=dtype)
                  np.array([scalar], dtype=dtype)
                  continue
  
          # The main test, we first try to use casting and if it succeeds
          # continue below testing that things are the same, otherwise
          # test that the alternative paths at least also fail.
          try:
              cast = np.array(scalar).astype(dtype)
          except (TypeError, ValueError, RuntimeError):
              # coercion should also raise (error type may change)
              with pytest.raises(Exception):
                  np.array(scalar, dtype=dtype)
  
              if isinstance(scalar, rational) and np.issubdtype(
                  dtype, np.signedinteger
              ):
                  return
  
              with pytest.raises(Exception):
                  np.array([scalar], dtype=dtype)
              # assignment should also raise
              res = np.zeros((), dtype=dtype)
              with pytest.raises(Exception):
                  res[()] = scalar
  
              return
  
          # Non error path:
          arr = np.array(scalar, dtype=dtype)
          assert_array_equal(arr, cast)
          # assignment behaves the same
          ass = np.zeros((), dtype=dtype)
          ass[()] = scalar
          assert_array_equal(ass, cast)
  
imports: from itertools import permutations, product
  import pytest
  from pytest import param
  import numpy
  from numpy.core._rational_tests import rational
  from numpy.core._multiarray_umath import _discover_array_parameters
  from numpy.testing import assert_array_equal, assert_warns, IS_PYPY
label_desc: Use image node_id 8 for calling the TestScalarDiscovery obj's underlying member method code with example usage: obj.test_scalar_coercion_same_as_cast_and_assignment(cast_to) without return types