Repository: pandas-dev/pandas
File: pandas/core/construction.py (Python)

Code before:
        IntegerArray,
        NumpyExtensionArray,
        TimedeltaArray,
    )
    from pandas.core.arrays.string_ import StringDtype

    if lib.is_scalar(data):
        msg = f"Cannot pass scalar '{data}' to 'pandas.array'."
        raise ValueError(msg)
    elif isinstance(data, ABCDataFrame):
        raise TypeError("Cannot pass DataFrame to 'pandas.array'")

    if dtype is None and isinstance(data, (ABCSeries, ABCIndex, ExtensionArray)):
        # Note: we exclude np.ndarray here, will do type inference on it
        dtype = data.dtype

    data = extract_array(data, extract_numpy=True)

    # Handle numpy masked arrays: convert masked values to NA
    # GH#63879
    if isinstance(data, ma.MaskedArray):
        if data.dtype.names is not None:
            data = np.asarray(data)
        else:
            mask = ma.getmaskarray(data)
            if mask.any():
                na_dtype = ensure_dtype_can_hold_na(data.dtype)
                if na_dtype.char in "SU":
                    na_dtype = np.dtype("object")
                data = cast(ma.MaskedArray, data.astype(na_dtype)).filled(np.nan)
            else:
                # No mask, convert to regular array
                data = np.asarray(data)

    # this returns None for not-found dtypes.
    if dtype is not None:
        dtype = pandas_dtype(dtype)

    if isinstance(data, ExtensionArray) and (dtype is None or data.dtype == dtype):
        # e.g. TimedeltaArray[s], avoid casting to NumpyExtensionArray
        if copy:
            return data.copy()
        return data

    if isinstance(dtype, ExtensionDtype):
        cls = dtype.construct_array_type()
        return cls._from_sequence(data, dtype=dtype, copy=copy)

    if dtype is None:
        was_ndarray = isinstance(data, np.ndarray)
        # error: Item "Sequence[object]" of "Sequence[object] | ExtensionArray |

Review comment (suggestion):
Avoid materializing an array when possible.

```suggestion
            if data.mask is not np.False_ and ma.getmaskarray(data).any():
```

Code after:
        IntegerArray,
        NumpyExtensionArray,
        TimedeltaArray,
    )
    from pandas.core.arrays.string_ import StringDtype

    if lib.is_scalar(data):
        msg = f"Cannot pass scalar '{data}' to 'pandas.array'."
        raise ValueError(msg)
    elif isinstance(data, ABCDataFrame):
        raise TypeError("Cannot pass DataFrame to 'pandas.array'")

    if dtype is None and isinstance(data, (ABCSeries, ABCIndex, ExtensionArray)):
        # Note: we exclude np.ndarray here, will do type inference on it
        dtype = data.dtype

    data = extract_array(data, extract_numpy=True)

    # Handle numpy masked arrays: convert masked values to NA
    # GH#63879
    if isinstance(data, ma.MaskedArray):
        if data.dtype.names is not None:
            raise ValueError(
                "Cannot construct an array from an ndarray with compound dtype. "
                "Use DataFrame instead."
            )
        elif data.mask is not np.False_ and ma.getmaskarray(data).any():
            na_dtype = ensure_dtype_can_hold_na(data.dtype)
            if na_dtype.char in "SU":
                na_dtype = np.dtype("object")
            data = cast(ma.MaskedArray, data.astype(na_dtype)).filled(np.nan)
        else:
            # No mask, convert to regular array
            data = np.asarray(data)

    # this returns None for not-found dtypes.
    if dtype is not None:
        dtype = pandas_dtype(dtype)

    if isinstance(data, ExtensionArray) and (dtype is None or data.dtype == dtype):
        # e.g. TimedeltaArray[s], avoid casting to NumpyExtensionArray
        if copy:
            return data.copy()
        return data

    if isinstance(dtype, ExtensionDtype):
        cls = dtype.construct_array_type()
        return cls._from_sequence(data, dtype=dtype, copy=copy)

    if dtype is None:
        was_ndarray = isinstance(data, np.ndarray)