Skip to content

TYP: remove #type: ignore for pd.array constructor #33706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
return self
return self._set_dtype(dtype)
if is_extension_array_dtype(dtype):
return array(self, dtype=dtype, copy=copy) # type: ignore # GH 28770
return array(self, dtype=dtype, copy=copy)
if is_integer_dtype(dtype) and self.isna().any():
raise ValueError("Cannot convert float NaN to integer")
return np.array(self, dtype=dtype, copy=copy)
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime, timedelta
import operator
from typing import Any, Sequence, Type, Union, cast
from typing import Any, Sequence, Type, TypeVar, Union, cast
import warnings

import numpy as np
Expand Down Expand Up @@ -437,6 +437,9 @@ def _with_freq(self, freq):
return self


DatetimeLikeArrayT = TypeVar("DatetimeLikeArrayT", bound="DatetimeLikeArrayMixin")


class DatetimeLikeArrayMixin(
ExtensionOpsMixin, AttributesMixin, NDArrayBackedExtensionArray
):
Expand Down Expand Up @@ -679,7 +682,7 @@ def _concat_same_type(cls, to_concat, axis: int = 0):

return cls._simple_new(values, dtype=dtype, freq=new_freq)

def copy(self):
def copy(self: DatetimeLikeArrayT) -> DatetimeLikeArrayT:
values = self.asi8.copy()
return type(self)._simple_new(values, dtype=self.dtype, freq=self.freq)

Expand Down
10 changes: 5 additions & 5 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import timedelta
import operator
from typing import Any, Callable, List, Optional, Sequence, Union
from typing import Any, Callable, List, Optional, Sequence, Type, Union

import numpy as np

Expand All @@ -20,6 +20,7 @@
period_asfreq_arr,
)
from pandas._libs.tslibs.timedeltas import Timedelta, delta_to_nanoseconds
from pandas._typing import AnyArrayLike
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -172,8 +173,8 @@ def _simple_new(cls, values: np.ndarray, freq=None, **kwargs) -> "PeriodArray":

@classmethod
def _from_sequence(
cls,
scalars: Sequence[Optional[Period]],
cls: Type["PeriodArray"],
scalars: Union[Sequence[Optional[Period]], AnyArrayLike],
dtype: Optional[PeriodDtype] = None,
copy: bool = False,
) -> "PeriodArray":
Expand All @@ -186,7 +187,6 @@ def _from_sequence(
validate_dtype_freq(scalars.dtype, freq)
if copy:
scalars = scalars.copy()
assert isinstance(scalars, PeriodArray) # for mypy
return scalars

periods = np.asarray(scalars, dtype=object)
Expand Down Expand Up @@ -772,7 +772,7 @@ def raise_on_incompatible(left, right):


def period_array(
data: Sequence[Optional[Period]],
data: Union[Sequence[Optional[Period]], AnyArrayLike],
freq: Optional[Union[str, Tick]] = None,
copy: bool = False,
) -> PeriodArray:
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from pandas._libs import lib
from pandas._libs.tslibs import IncompatibleFrequency, OutOfBoundsDatetime
from pandas._typing import ArrayLike, Dtype, DtypeObj
from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj

from pandas.core.dtypes.cast import (
construct_1d_arraylike_from_scalar,
Expand Down Expand Up @@ -54,7 +54,9 @@


def array(
data: Sequence[object], dtype: Optional[Dtype] = None, copy: bool = True,
data: Union[Sequence[object], AnyArrayLike],
dtype: Optional[Dtype] = None,
copy: bool = True,
) -> "ExtensionArray":
"""
Create an array.
Expand Down