Skip to content

Commit 64d544c

Browse files
TYP: remove #type: ignore for pd.array constructor (#33706)
1 parent 250663a commit 64d544c

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
463463
return self
464464
return self._set_dtype(dtype)
465465
if is_extension_array_dtype(dtype):
466-
return array(self, dtype=dtype, copy=copy) # type: ignore # GH 28770
466+
return array(self, dtype=dtype, copy=copy)
467467
if is_integer_dtype(dtype) and self.isna().any():
468468
raise ValueError("Cannot convert float NaN to integer")
469469
return np.array(self, dtype=dtype, copy=copy)

pandas/core/arrays/datetimelike.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import datetime, timedelta
22
import operator
3-
from typing import Any, Sequence, Type, Union, cast
3+
from typing import Any, Sequence, Type, TypeVar, Union, cast
44
import warnings
55

66
import numpy as np
@@ -438,6 +438,9 @@ def _with_freq(self, freq):
438438
return arr
439439

440440

441+
DatetimeLikeArrayT = TypeVar("DatetimeLikeArrayT", bound="DatetimeLikeArrayMixin")
442+
443+
441444
class DatetimeLikeArrayMixin(
442445
ExtensionOpsMixin, AttributesMixin, NDArrayBackedExtensionArray
443446
):
@@ -680,7 +683,7 @@ def _concat_same_type(cls, to_concat, axis: int = 0):
680683

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

683-
def copy(self):
686+
def copy(self: DatetimeLikeArrayT) -> DatetimeLikeArrayT:
684687
values = self.asi8.copy()
685688
return type(self)._simple_new(values, dtype=self.dtype, freq=self.freq)
686689

pandas/core/arrays/period.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import timedelta
22
import operator
3-
from typing import Any, Callable, List, Optional, Sequence, Union
3+
from typing import Any, Callable, List, Optional, Sequence, Type, Union
44

55
import numpy as np
66

@@ -20,6 +20,7 @@
2020
period_asfreq_arr,
2121
)
2222
from pandas._libs.tslibs.timedeltas import Timedelta, delta_to_nanoseconds
23+
from pandas._typing import AnyArrayLike
2324
from pandas.util._decorators import cache_readonly
2425

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

173174
@classmethod
174175
def _from_sequence(
175-
cls,
176-
scalars: Sequence[Optional[Period]],
176+
cls: Type["PeriodArray"],
177+
scalars: Union[Sequence[Optional[Period]], AnyArrayLike],
177178
dtype: Optional[PeriodDtype] = None,
178179
copy: bool = False,
179180
) -> "PeriodArray":
@@ -186,7 +187,6 @@ def _from_sequence(
186187
validate_dtype_freq(scalars.dtype, freq)
187188
if copy:
188189
scalars = scalars.copy()
189-
assert isinstance(scalars, PeriodArray) # for mypy
190190
return scalars
191191

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

773773

774774
def period_array(
775-
data: Sequence[Optional[Period]],
775+
data: Union[Sequence[Optional[Period]], AnyArrayLike],
776776
freq: Optional[Union[str, Tick]] = None,
777777
copy: bool = False,
778778
) -> PeriodArray:

pandas/core/construction.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from pandas._libs import lib
1515
from pandas._libs.tslibs import IncompatibleFrequency, OutOfBoundsDatetime
16-
from pandas._typing import ArrayLike, Dtype, DtypeObj
16+
from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj
1717

1818
from pandas.core.dtypes.cast import (
1919
construct_1d_arraylike_from_scalar,
@@ -54,7 +54,9 @@
5454

5555

5656
def array(
57-
data: Sequence[object], dtype: Optional[Dtype] = None, copy: bool = True,
57+
data: Union[Sequence[object], AnyArrayLike],
58+
dtype: Optional[Dtype] = None,
59+
copy: bool = True,
5860
) -> "ExtensionArray":
5961
"""
6062
Create an array.

0 commit comments

Comments
 (0)