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

Code before:

        elif isinstance(dtype, ExtensionDtype):
            return super().astype(dtype, copy=copy)
        elif dtype.kind in "iu":
            # we deliberately ignore int32 vs. int64 here.
            # See https://github.com/pandas-dev/pandas/issues/24381 for more.
            values = self.asi8
            if dtype != np.int64:
                raise TypeError(
                    f"Converting from {self.dtype} to {dtype} is not supported. "
                    "Do obj.astype('int64').astype(dtype) instead"
                )

            if copy:
                values = values.copy()
            return values
        elif (dtype.kind in "mM" and self.dtype != dtype) or dtype.kind == "f":
            # disallow conversion between datetime/timedelta,
            # and conversions for any datetimelike to float
            msg = f"Cannot cast {type(self).__name__} to dtype {dtype}"
            raise TypeError(msg)
        else:
            return np.asarray(self, dtype=dtype)

    @overload
    def view(self) -> Self: ...

    @overload
    def view(self, dtype: Literal["M8[ns]"]) -> DatetimeArray: ...

    @overload
    def view(self, dtype: Literal["m8[ns]"]) -> TimedeltaArray: ...

    @overload
    def view(self, dtype: Dtype | None = ...) -> ArrayLike: ...

    def view(self, dtype: Dtype | None = None) -> ArrayLike:
        # we need to explicitly call super() method as long as the `@overload`s
        #  are present in this file.
        return super().view(dtype)

    # ------------------------------------------------------------------
    # Validation Methods
    # TODO: try to de-duplicate these, ensure identical behavior

    def _validate_comparison_value(self, other):
        if isinstance(other, str):
            try:
                # GH#18435 strings get a pass from tzawareness compat
                other = self._scalar_from_string(other)
            except (ValueError, IncompatibleFrequency) as err:

Review comment (suggestion):
It's not really clear to me why I need to add this ignore here (since this is exactly the same as in the base class and the mixin base). 

But if not adding it, I get

```
pandas/core/arrays/datetimelike.py:511: error: Signature of "view" incompatible with supertype "pandas.core.arrays._mixins.NDArrayBackedExtensionArray"  [override]
pandas/core/arrays/datetimelike.py:511: note:      Superclass:
pandas/core/arrays/datetimelike.py:511: note:          @overload
pandas/core/arrays/datetimelike.py:511: note:          def view(self) -> DatetimeLikeArrayMixin
pandas/core/arrays/datetimelike.py:511: note:          @overload
pandas/core/arrays/datetimelike.py:511: note:          def view(self, dtype: ExtensionDtype | str | dtype[Any] | type[str] | type[complex] | type[bool] | type[object] | None = ...) -> ExtensionArray | ndarray[Any, Any]
pandas/core/arrays/datetimelike.py:511: note:      Subclass:
pandas/core/arrays/datetimelike.py:511: note:          @overload
pandas/core/arrays/datetimelike.py:511: note:          def view(self) -> DatetimeLikeArrayMixin
pandas/core/arrays/datetimelike.py:511: note:          @overload
pandas/core/arrays/datetimelike.py:511: note:          def view(self, dtype: Literal['M8[ns]']) -> DatetimeArray
pandas/core/arrays/datetimelike.py:511: note:          @overload
pandas/core/arrays/datetimelike.py:511: note:          def view(self, dtype: Literal['m8[ns]']) -> TimedeltaArray
pandas/core/arrays/datetimelike.py:511: note:          @overload
pandas/core/arrays/datetimelike.py:511: note:          def view(self, dtype: ExtensionDtype | str | dtype[Any] | type[str] | type[complex] | type[bool] | type[object] | None = ...) -> ExtensionArray | ndarray[Any, Any]
pandas/core/arrays/datetimelike.py:511: error: Signature of "view" incompatible with supertype "pandas.core.arrays.base.ExtensionArray"  [override]
pandas/core/arrays/datetimelike.py:511: note:      Superclass:
pandas/core/arrays/datetimelike.py:511: note:          @overload
pandas/core/arrays/datetimelike.py:511: note:          def view(self) -> DatetimeLikeArrayMixin
pandas/core/arrays/datetimelike.py:511: note:          @overload
pandas/core/arrays/datetimelike.py:511: note:          def view(self, dtype: ExtensionDtype | str | dtype[Any] | type[str] | type[complex] | type[bool] | type[object] | None = ...) -> ExtensionArray | ndarray[Any, Any]
pandas/core/arrays/datetimelike.py:511: note:      Subclass:
pandas/core/arrays/datetimelike.py:511: note:          @overload
pandas/core/arrays/datetimelike.py:511: note:          def view(self) -> DatetimeLikeArrayMixin
pandas/core/arrays/datetimelike.py:511: note:          @overload
pandas/core/arrays/datetimelike.py:511: note:          def view(self, dtype: Literal['M8[ns]']) -> DatetimeArray
pandas/core/arrays/datetimelike.py:511: note:          @overload
pandas/core/arrays/datetimelike.py:511: note:          def view(self, dtype: Literal['m8[ns]']) -> TimedeltaArray
pandas/core/arrays/datetimelike.py:511: note:          @overload
pandas/core/arrays/datetimelike.py:511: note:          def view(self, dtype: ExtensionDtype | str | dtype[Any] | type[str] | type[complex] | type[bool] | type[object] | None = ...) -> ExtensionArray | ndarray[Any, Any]
```

Code after:

        elif isinstance(dtype, ExtensionDtype):
            return super().astype(dtype, copy=copy)
        elif dtype.kind in "iu":
            # we deliberately ignore int32 vs. int64 here.
            # See https://github.com/pandas-dev/pandas/issues/24381 for more.
            values = self.asi8
            if dtype != np.int64:
                raise TypeError(
                    f"Converting from {self.dtype} to {dtype} is not supported. "
                    "Do obj.astype('int64').astype(dtype) instead"
                )

            if copy:
                values = values.copy()
            return values
        elif (dtype.kind in "mM" and self.dtype != dtype) or dtype.kind == "f":
            # disallow conversion between datetime/timedelta,
            # and conversions for any datetimelike to float
            msg = f"Cannot cast {type(self).__name__} to dtype {dtype}"
            raise TypeError(msg)
        else:
            return np.asarray(self, dtype=dtype)

    @overload  # type: ignore[override]
    def view(self) -> Self: ...

    @overload
    def view(self, dtype: Literal["M8[ns]"]) -> DatetimeArray: ...

    @overload
    def view(self, dtype: Literal["m8[ns]"]) -> TimedeltaArray: ...

    @overload
    def view(self, dtype: Dtype | None = ...) -> ArrayLike: ...

    def view(self, dtype: Dtype | None = None) -> ArrayLike:
        # we need to explicitly call super() method as long as the `@overload`s
        #  are present in this file.
        return super().view(dtype)

    # ------------------------------------------------------------------
    # Validation Methods
    # TODO: try to de-duplicate these, ensure identical behavior

    def _validate_comparison_value(self, other):
        if isinstance(other, str):
            try:
                # GH#18435 strings get a pass from tzawareness compat
                other = self._scalar_from_string(other)
            except (ValueError, IncompatibleFrequency) as err: