Repository: numpy/numpy
File: numpy/ma/tests/test_core.py (Python)

Code before:

    def test_check_on_scalar(self):
        # Test _check_fill_value set to valid and invalid values
        _check_fill_value = np.ma.core._check_fill_value

        fval = _check_fill_value(0, int)
        assert_equal(fval, 0)
        fval = _check_fill_value(None, int)
        assert_equal(fval, default_fill_value(0))

        fval = _check_fill_value(0, "|S3")
        assert_equal(fval, b"0")
        fval = _check_fill_value(None, "|S3")
        assert_equal(fval, default_fill_value(b"camelot!"))
        assert_raises(TypeError, _check_fill_value, 1e+20, int)
        assert_raises(TypeError, _check_fill_value, 'stuff', int)

    def test_fill_value_datetime_structured(self):
        # gh-29818
        rec = np.array([(dt.date(2025, 4, 1),)], dtype=[('foo', '<M8[D]')])
        ma = np.ma.masked_array(rec)
        np.sort(ma)
        res = np.ma.minimum_fill_value(ma)
        assert isinstance(res['foo'], np.datetime64)

    def test_check_on_fields(self):
        # Tests _check_fill_value with records
        _check_fill_value = np.ma.core._check_fill_value
        ndtype = [('a', int), ('b', float), ('c', "|S3")]
        # A check on a list should return a single record
        fval = _check_fill_value([-999, -12345678.9, "???"], ndtype)
        assert_(isinstance(fval, ndarray))
        assert_equal(fval.item(), [-999, -12345678.9, b"???"])
        # A check on None should output the defaults
        fval = _check_fill_value(None, ndtype)
        assert_(isinstance(fval, ndarray))
        assert_equal(fval.item(), [default_fill_value(0),
                                   default_fill_value(0.),
                                   asbytes(default_fill_value("0"))])
        #.....Using a structured type as fill_value should work
        fill_val = np.array((-999, -12345678.9, "???"), dtype=ndtype)
        fval = _check_fill_value(fill_val, ndtype)
        assert_(isinstance(fval, ndarray))
        assert_equal(fval.item(), [-999, -12345678.9, b"???"])

        #.....Using a flexible type w/ a different type shouldn't matter
        # BEHAVIOR in 1.5 and earlier, and 1.13 and later: match structured
        # types by position
        fill_val = np.array((-999, -12345678.9, "???"),
                            dtype=[("A", int), ("B", float), ("C", "|S3")])
        fval = _check_fill_value(fill_val, ndtype)

Review comment (suggestion):
The added special-casing for `int | np.integer` could also use a test or two, and the same goes for `dt.datetime`.

Code after:

class TestFillingValues:

    def test_check_on_scalar(self):
        # Test _check_fill_value set to valid and invalid values
        _check_fill_value = np.ma.core._check_fill_value

        fval = _check_fill_value(0, int)
        assert_equal(fval, 0)
        fval = _check_fill_value(None, int)
        assert_equal(fval, default_fill_value(0))

        fval = _check_fill_value(0, "|S3")
        assert_equal(fval, b"0")
        fval = _check_fill_value(None, "|S3")
        assert_equal(fval, default_fill_value(b"camelot!"))
        assert_raises(TypeError, _check_fill_value, 1e+20, int)
        assert_raises(TypeError, _check_fill_value, 'stuff', int)

    def test_fill_value_datetime_structured(self):
        # gh-29818
        rec = np.array([(dt.date(2025, 4, 1),)], dtype=[('foo', '<M8[D]')])
        ma = np.ma.masked_array(rec)
        np.sort(ma)
        res = np.ma.minimum_fill_value(ma)
        assert isinstance(res['foo'], np.datetime64)

    def test_fill_value_datetime_structured_datetime(self):
        # gh-29818
        rec = np.array([(dt.datetime(2025, 4, 1, 12, 0),)],
                       dtype=[('foo', '<M8[ms]')])
        ma = np.ma.masked_array(rec)
        res = np.ma.minimum_fill_value(ma)
        assert isinstance(res['foo'], np.datetime64)

    def test_fill_value_datetime_structured_integer(self):
        # gh-29818
        dtype = np.dtype([('foo', '<M8[D]')])

        def fill_func(dt):
            return 42
        res = np.ma.core._recursive_fill_value(dtype, fill_func)
        assert isinstance(res['foo'], np.datetime64)
        assert res['foo'] == np.datetime64(42, 'D')

    def test_check_on_fields(self):
        # Tests _check_fill_value with records
        _check_fill_value = np.ma.core._check_fill_value
        ndtype = [('a', int), ('b', float), ('c', "|S3")]
        # A check on a list should return a single record
        fval = _check_fill_value([-999, -12345678.9, "???"], ndtype)