Skip to content

Commit 1843952

Browse files
authored
Use np array aliases in tests (#1515)
1 parent 085653b commit 1843952

35 files changed

+210
-111
lines changed

pandas-stubs/_typing.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ TimeNonexistent: TypeAlias = (
827827
DropKeep: TypeAlias = Literal["first", "last", False]
828828
CorrelationMethod: TypeAlias = (
829829
Literal["pearson", "kendall", "spearman"]
830-
| Callable[[np.typing.NDArray[Any], np.typing.NDArray[Any]], float]
830+
| Callable[[npt.NDArray[Any], npt.NDArray[Any]], float]
831831
)
832832
AlignJoin: TypeAlias = Literal["outer", "inner", "left", "right"]
833833
DtypeBackend: TypeAlias = Literal["pyarrow", "numpy_nullable"]

pandas-stubs/core/base.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ from typing import (
1818

1919
from _typeshed import _T_contra
2020
import numpy as np
21+
import numpy.typing as npt
2122
from pandas.core.arraylike import OpsMixin
2223
from pandas.core.arrays import ExtensionArray
2324
from pandas.core.arrays.categorical import Categorical
@@ -222,7 +223,7 @@ ScalarArrayIndexReal: TypeAlias = (
222223
float
223224
| Sequence[float | NumpyRealScalar]
224225
| NumpyRealScalar
225-
| np.typing.NDArray[NumpyRealScalar]
226+
| npt.NDArray[NumpyRealScalar]
226227
| ExtensionArray
227228
| IndexReal
228229
)
@@ -235,7 +236,7 @@ ScalarArrayIndexComplex: TypeAlias = (
235236
complex
236237
| Sequence[complex | NumpyComplexScalar]
237238
| NumpyComplexScalar
238-
| np.typing.NDArray[NumpyComplexScalar]
239+
| npt.NDArray[NumpyComplexScalar]
239240
| ExtensionArray
240241
| IndexComplex
241242
)

pandas-stubs/core/indexes/accessors.pyi

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ from typing import (
1212
type_check_only,
1313
)
1414

15-
import numpy as np
1615
from pandas.core.accessor import PandasDelegate
1716
from pandas.core.arrays.base import ExtensionArray
1817
from pandas.core.arrays.categorical import Categorical
@@ -45,8 +44,8 @@ from pandas._typing import (
4544
TimestampConvention,
4645
TimeUnit,
4746
TimeZones,
48-
np_1darray,
4947
np_1darray_bool,
48+
np_1darray_object,
5049
np_ndarray_bool,
5150
)
5251

@@ -135,10 +134,10 @@ class _DatetimeObjectOps(
135134
): ...
136135

137136
_DTOtherOpsDateReturnType = TypeVar(
138-
"_DTOtherOpsDateReturnType", bound=Series[date] | np_1darray[np.object_]
137+
"_DTOtherOpsDateReturnType", bound=Series[date] | np_1darray_object
139138
)
140139
_DTOtherOpsTimeReturnType = TypeVar(
141-
"_DTOtherOpsTimeReturnType", bound=Series[time] | np_1darray[np.object_]
140+
"_DTOtherOpsTimeReturnType", bound=Series[time] | np_1darray_object
142141
)
143142

144143
class _DatetimeOtherOps(Generic[_DTOtherOpsDateReturnType, _DTOtherOpsTimeReturnType]):
@@ -293,7 +292,7 @@ class DatetimeProperties(
293292
_DTToPeriodReturnType,
294293
],
295294
):
296-
def to_pydatetime(self) -> np_1darray[np.object_]: ...
295+
def to_pydatetime(self) -> np_1darray_object: ...
297296
def isocalendar(self) -> DataFrame: ...
298297
@property
299298
def unit(self) -> TimeUnit: ...
@@ -309,7 +308,7 @@ _TDTotalSecondsReturnType = TypeVar(
309308
class _TimedeltaPropertiesNoRounding(
310309
Generic[_TDNoRoundingMethodReturnType, _TDTotalSecondsReturnType]
311310
):
312-
def to_pytimedelta(self) -> np_1darray[np.object_]: ...
311+
def to_pytimedelta(self) -> np_1darray_object: ...
313312
@property
314313
def components(self) -> DataFrame: ...
315314
@property
@@ -418,8 +417,8 @@ class DatetimeIndexProperties(
418417
Index[int],
419418
np_1darray_bool,
420419
DatetimeIndex,
421-
np_1darray[np.object_],
422-
np_1darray[np.object_],
420+
np_1darray_object,
421+
np_1darray_object,
423422
BaseOffset,
424423
DatetimeIndex,
425424
Index,
@@ -431,7 +430,7 @@ class DatetimeIndexProperties(
431430
def is_normalized(self) -> bool: ...
432431
@property
433432
def tzinfo(self) -> _tzinfo | None: ...
434-
def to_pydatetime(self) -> np_1darray[np.object_]: ...
433+
def to_pydatetime(self) -> np_1darray_object: ...
435434
def std(
436435
self, axis: int | None = ..., ddof: int = ..., skipna: bool = ...
437436
) -> Timedelta: ...

pandas-stubs/core/indexes/period.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ from pandas._typing import (
2222
AxesData,
2323
Dtype,
2424
Frequency,
25-
np_1darray,
2625
np_1darray_intp,
26+
np_1darray_object,
2727
np_ndarray_bool,
2828
)
2929

@@ -37,7 +37,7 @@ class PeriodIndex(DatetimeIndexOpsMixin[pd.Period, np.object_], PeriodIndexField
3737
name: Hashable | None = None,
3838
) -> Self: ...
3939
@property
40-
def values(self) -> np_1darray[np.object_]: ...
40+
def values(self) -> np_1darray_object: ...
4141
def __add__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
4242
self, other: datetime.timedelta
4343
) -> Self: ...

tests/indexes/arithmetic/bool/test_add.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
import pandas as pd
44
from typing_extensions import assert_type
55

6-
from tests import check
7-
from tests._typing import np_ndarray_int64
6+
from tests import (
7+
check,
8+
)
9+
from tests._typing import (
10+
np_ndarray_bool,
11+
np_ndarray_int64,
12+
)
813

914
# left operand
1015
left = pd.Index([True, True, False])
@@ -55,7 +60,7 @@ def test_add_numpy_array() -> None:
5560
# `numpy` typing gives the corresponding `ndarray`s in the static type
5661
# checking, where our `__radd__` cannot override. At runtime, they return
5762
# `Index`es with the correct element type.
58-
check(assert_type(b + left, "npt.NDArray[np.bool_]"), pd.Index, np.bool_)
63+
check(assert_type(b + left, np_ndarray_bool), pd.Index, np.bool_)
5964
check(assert_type(i + left, np_ndarray_int64), pd.Index, np.integer)
6065
check(assert_type(f + left, "npt.NDArray[np.float64]"), pd.Index, np.floating)
6166
check(

tests/indexes/arithmetic/bool/test_mul.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
TYPE_CHECKING_INVALID_USAGE,
1818
check,
1919
)
20-
from tests._typing import np_ndarray_int64
20+
from tests._typing import (
21+
np_ndarray_bool,
22+
np_ndarray_int64,
23+
np_ndarray_td,
24+
)
2125

2226

2327
@pytest.fixture
@@ -92,7 +96,7 @@ def test_mul_numpy_array(left: "pd.Index[bool]") -> None:
9296
# `numpy` typing gives the corresponding `ndarray`s in the static type
9397
# checking, where our `__rmul__` cannot override. At runtime, they return
9498
# `Index` with the correct element type.
95-
check(assert_type(b * left, "npt.NDArray[np.bool_]"), pd.Index, np.bool_)
99+
check(assert_type(b * left, np_ndarray_bool), pd.Index, np.bool_)
96100
check(assert_type(i * left, np_ndarray_int64), pd.Index, np.integer)
97101
check(assert_type(f * left, "npt.NDArray[np.float64]"), pd.Index, np.floating)
98102
check(
@@ -102,7 +106,7 @@ def test_mul_numpy_array(left: "pd.Index[bool]") -> None:
102106
)
103107
if TYPE_CHECKING_INVALID_USAGE:
104108
assert_type(s * left, Any)
105-
assert_type(d * left, "npt.NDArray[np.timedelta64]")
109+
assert_type(d * left, np_ndarray_td)
106110

107111

108112
def test_mul_pd_index(left: "pd.Index[bool]") -> None:

tests/indexes/arithmetic/complex/test_add.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
from typing_extensions import assert_type
55

66
from tests import check
7-
from tests._typing import np_ndarray_int64
7+
from tests._typing import (
8+
np_ndarray_bool,
9+
np_ndarray_int64,
10+
)
811

912
# left operand
1013
left = pd.Index([1j, 2j, 3j])
@@ -55,7 +58,7 @@ def test_add_numpy_array() -> None:
5558
# `numpy` typing gives the corresponding `ndarray`s in the static type
5659
# checking, where our `__radd__` cannot override. At runtime, they return
5760
# `Index`es with the correct element type.
58-
check(assert_type(b + left, "npt.NDArray[np.bool_]"), pd.Index, np.complexfloating)
61+
check(assert_type(b + left, np_ndarray_bool), pd.Index, np.complexfloating)
5962
check(assert_type(i + left, np_ndarray_int64), pd.Index, np.complexfloating)
6063
check(
6164
assert_type(f + left, "npt.NDArray[np.float64]"), pd.Index, np.complexfloating

tests/indexes/arithmetic/complex/test_mul.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
TYPE_CHECKING_INVALID_USAGE,
1818
check,
1919
)
20-
from tests._typing import np_ndarray_int64
20+
from tests._typing import (
21+
np_ndarray_bool,
22+
np_ndarray_int64,
23+
np_ndarray_td,
24+
)
2125

2226

2327
@pytest.fixture
@@ -92,7 +96,7 @@ def test_mul_numpy_array(left: "pd.Index[complex]") -> None:
9296
# `numpy` typing gives the corresponding `ndarray`s in the static type
9397
# checking, where our `__rmul__` cannot override. At runtime, they return
9498
# `Index` with the correct element type.
95-
check(assert_type(b * left, "npt.NDArray[np.bool_]"), pd.Index, np.complexfloating)
99+
check(assert_type(b * left, np_ndarray_bool), pd.Index, np.complexfloating)
96100
check(assert_type(i * left, np_ndarray_int64), pd.Index, np.complexfloating)
97101
check(
98102
assert_type(f * left, "npt.NDArray[np.float64]"), pd.Index, np.complexfloating
@@ -104,7 +108,7 @@ def test_mul_numpy_array(left: "pd.Index[complex]") -> None:
104108
)
105109
if TYPE_CHECKING_INVALID_USAGE:
106110
assert_type(s * left, Any)
107-
assert_type(d * left, "npt.NDArray[np.timedelta64]")
111+
assert_type(d * left, np_ndarray_td)
108112

109113

110114
def test_mul_pd_index(left: "pd.Index[complex]") -> None:

tests/indexes/arithmetic/float/test_add.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
import pandas as pd
44
from typing_extensions import assert_type
55

6-
from tests import check
7-
from tests._typing import np_ndarray_int64
6+
from tests import (
7+
check,
8+
)
9+
from tests._typing import (
10+
np_ndarray_bool,
11+
np_ndarray_int64,
12+
)
813

914
# left operand
1015
left = pd.Index([1.0, 2.0, 3.0])
@@ -55,7 +60,7 @@ def test_add_numpy_array() -> None:
5560
# `numpy` typing gives the corresponding `ndarray`s in the static type
5661
# checking, where our `__radd__` cannot override. At runtime, they return
5762
# `Index`es with the correct element type.
58-
check(assert_type(b + left, "npt.NDArray[np.bool_]"), pd.Index, np.floating)
63+
check(assert_type(b + left, np_ndarray_bool), pd.Index, np.floating)
5964
check(assert_type(i + left, np_ndarray_int64), pd.Index, np.floating)
6065
check(assert_type(f + left, "npt.NDArray[np.float64]"), pd.Index, np.floating)
6166
check(

tests/indexes/arithmetic/float/test_floordiv.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Any
66

77
import numpy as np
8+
import numpy.typing as npt # noqa: F401
89
import pandas as pd
910
import pytest
1011
from typing_extensions import (
@@ -16,6 +17,9 @@
1617
TYPE_CHECKING_INVALID_USAGE,
1718
check,
1819
)
20+
from tests._typing import (
21+
np_ndarray_int64,
22+
)
1923

2024

2125
@pytest.fixture
@@ -92,16 +96,14 @@ def test_floordiv_numpy_array(left: "pd.Index[float]") -> None:
9296
# `numpy` typing gives the corresponding `ndarray`s in the static type
9397
# checking, where our `__rfloordiv__` cannot override. At runtime, they lead to
9498
# errors or pd.Index.
95-
check(assert_type(b // left, "np.typing.NDArray[np.int8]"), pd.Index, np.floating)
96-
check(assert_type(i // left, "np.typing.NDArray[np.int64]"), pd.Index, np.floating)
97-
check(
98-
assert_type(f // left, "np.typing.NDArray[np.float64]"), pd.Index, np.floating
99-
)
99+
check(assert_type(b // left, "npt.NDArray[np.int8]"), pd.Index, np.floating)
100+
check(assert_type(i // left, np_ndarray_int64), pd.Index, np.floating)
101+
check(assert_type(f // left, "npt.NDArray[np.float64]"), pd.Index, np.floating)
100102
if TYPE_CHECKING_INVALID_USAGE:
101103
assert_type(c // left, Any)
102104
assert_type(s // left, Any)
103105
check(
104-
assert_type(d // left, "np.typing.NDArray[np.int64]"),
106+
assert_type(d // left, np_ndarray_int64),
105107
pd.TimedeltaIndex,
106108
pd.Timedelta,
107109
)

0 commit comments

Comments
 (0)