Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ def _validate_scalar(self, value):

# ------------------------------------------------------------------------

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

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

def view(self, dtype: Dtype | None = None) -> ArrayLike:
# We handle datetime64, datetime64tz, timedelta64, and period
# dtypes here. Everything else we pass through to the underlying
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,12 @@ def copy(self) -> Self:
"""
raise AbstractMethodError(self)

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

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

def view(self, dtype: Dtype | None = None) -> ArrayLike:
"""
Return a view on the array.
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def astype(self, dtype, copy: bool = True):
else:
return np.asarray(self, dtype=dtype)

@overload
@overload # type: ignore[override]
def view(self) -> Self: ...
Comment on lines -511 to 512
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]


@overload
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,10 @@ def _str_map_nan_semantics(
else:
return self._str_map_str_or_object(dtype, na_value, arr, f, mask)

def view(self, dtype: Dtype | None = None) -> ArrayLike:
def view(self, dtype: Dtype | None = None) -> Self:
if dtype is not None:
raise TypeError("Cannot change data-type for string array.")
return super().view(dtype=dtype)
return super().view()


# error: Definition of "_concat_same_type" in base class "NDArrayBacked" is
Expand Down
Loading