Skip to content

Commit b420c0e

Browse files
committed
refactor after separating dtype args
1 parent 74b5325 commit b420c0e

File tree

5 files changed

+86
-111
lines changed

5 files changed

+86
-111
lines changed

pandas-stubs/core/construction.pyi

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ from pandas.core.arrays.numpy_ import NumpyExtensionArray
2121
from pandas.core.arrays.period import PeriodArray
2222
from pandas.core.arrays.sparse.array import SparseArray
2323
from pandas.core.arrays.string_ import StringArray
24+
from pandas.core.arrays.string_arrow import ArrowStringArray
2425
from pandas.core.arrays.timedeltas import TimedeltaArray
2526
from pandas.core.indexes.base import Index
2627
from pandas.core.indexes.category import CategoricalIndex
@@ -39,16 +40,22 @@ from pandas._libs.tslibs.period import Period
3940
from pandas._libs.tslibs.timedeltas import Timedelta
4041
from pandas._libs.tslibs.timestamps import Timestamp
4142
from pandas._typing import (
42-
BooleanDtypeArg,
43+
BuiltinBooleanDtypeArg,
44+
BuiltinFloatDtypeArg,
45+
BuiltinIntDtypeArg,
46+
BuiltinStrDtypeArg,
4347
CategoryDtypeArg,
44-
FloatDtypeArg,
45-
IntDtypeArg,
4648
IntervalT,
49+
NumpyDtypeArg,
50+
PandasBooleanDtypeArg,
51+
PandasFloatDtypeArg,
52+
PandasIntDtypeArg,
53+
PandasStrDtypeArg,
54+
PandasTimestampDtypeArg,
55+
PandasUIntDtypeArg,
56+
PyArrowNotStrDtypeArg,
57+
PyArrowStrDtypeArg,
4758
SequenceNotStr,
48-
StrDtypeArg,
49-
TimedeltaDtypeArg,
50-
TimestampDtypeArg,
51-
UIntDtypeArg,
5259
np_ndarray,
5360
np_ndarray_anyint,
5461
np_ndarray_bool,
@@ -64,36 +71,64 @@ from pandas.core.dtypes.dtypes import (
6471
PeriodDtype,
6572
)
6673

74+
# @overload
75+
# def array(
76+
# data: SequenceNotStr[NAType | None],
77+
# dtype: None = None,
78+
# copy: bool = True
79+
# ) -> NumpyExtensionArray: ...
6780
@overload
6881
def array( # type: ignore[overload-overlap]
6982
data: SequenceNotStr[Any] | np_ndarray | ExtensionArray | Index | Series,
7083
dtype: CategoryDtypeArg,
7184
copy: bool = True,
7285
) -> Categorical: ...
7386
@overload
87+
def array(
88+
# TODO: Categorical Series pandas-dev/pandas-stubs#1415
89+
data: Categorical | CategoricalIndex,
90+
dtype: CategoryDtypeArg | None = None,
91+
copy: bool = True,
92+
) -> Categorical: ...
93+
@overload
7494
def array( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
75-
data: Sequence[NAType | None],
76-
dtype: str | np.dtype | ExtensionDtype | None = None,
95+
data: (
96+
Sequence[Period | NaTType | None] | PeriodArray | PeriodIndex | Series[Period]
97+
),
98+
dtype: PeriodDtype | None = None,
7799
copy: bool = True,
78-
) -> NumpyExtensionArray: ...
100+
) -> PeriodArray: ...
79101
@overload
80102
def array( # type: ignore[overload-overlap]
103+
# float("nan") also works, but I don't know how to put it in
104+
data: Sequence[IntervalT | None] | IntervalArray | IntervalIndex | Series[Interval],
105+
dtype: IntervalDtype | None = None,
106+
copy: bool = True,
107+
) -> IntervalArray: ...
108+
@overload
109+
def array(
110+
data: SparseArray | SparseIndex,
111+
dtype: str | np.dtype | ExtensionDtype | None = None,
112+
copy: bool = True,
113+
) -> SparseArray: ...
114+
@overload
115+
def array( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
81116
data: Sequence[bool | np.bool | NAType | None] | np_ndarray_bool | BooleanArray,
82-
dtype: BooleanDtypeArg | None = None,
117+
dtype: BuiltinBooleanDtypeArg | PandasBooleanDtypeArg | None = None,
83118
copy: bool = True,
84119
) -> BooleanArray: ...
85120
@overload
86121
def array( # type: ignore[overload-overlap]
87122
data: Sequence[int | np.integer | NAType | None] | np_ndarray_anyint | IntegerArray,
88-
dtype: IntDtypeArg | UIntDtypeArg | None = None,
123+
dtype: BuiltinIntDtypeArg | PandasIntDtypeArg | PandasUIntDtypeArg | None = None,
89124
copy: bool = True,
90125
) -> IntegerArray: ...
91126
@overload
92127
def array( # type: ignore[overload-overlap]
93128
data: (
94129
Sequence[float | np.floating | NAType | None] | np_ndarray_float | FloatingArray
95130
),
96-
dtype: FloatDtypeArg | None = None,
131+
dtype: BuiltinFloatDtypeArg | PandasFloatDtypeArg | None = None,
97132
copy: bool = True,
98133
) -> FloatingArray: ...
99134
@overload
@@ -106,75 +141,54 @@ def array( # type: ignore[overload-overlap]
106141
| DatetimeIndex
107142
| Series[Timestamp]
108143
),
109-
dtype: TimestampDtypeArg | None = None,
144+
dtype: PandasTimestampDtypeArg | None = None,
110145
copy: bool = True,
111146
) -> DatetimeArray: ...
112147
@overload
113-
def array( # type: ignore[overload-overlap]
148+
def array(
114149
data: (
115150
Sequence[timedelta | np.timedelta64 | NaTType | None]
116151
| np_ndarray_td
117152
| TimedeltaArray
118153
| TimedeltaIndex
119154
| Series[Timedelta]
120155
),
121-
dtype: TimedeltaDtypeArg | None = None,
156+
dtype: None = None,
122157
copy: bool = True,
123158
) -> TimedeltaArray: ...
124159
@overload
125-
def array(
160+
def array( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
126161
data: SequenceNotStr[str | np.str_ | NAType | None] | np_ndarray_str | StringArray,
127-
dtype: StrDtypeArg | None = None,
162+
dtype: BuiltinStrDtypeArg | PandasStrDtypeArg | None = None,
128163
copy: bool = True,
129164
) -> StringArray: ...
130165
@overload
131166
def array( # type: ignore[overload-overlap]
132167
data: (
133-
Sequence[Period | NaTType | None] | PeriodArray | PeriodIndex | Series[Period]
168+
SequenceNotStr[str | np.str_ | NAType | None]
169+
| np_ndarray_str
170+
| StringArray
171+
| ArrowStringArray
134172
),
135-
dtype: PeriodDtype | None = None,
136-
copy: bool = True,
137-
) -> PeriodArray: ...
138-
@overload
139-
def array(
140-
# float("nan") also works, but I don't know how to put it in
141-
data: Sequence[IntervalT | None] | IntervalArray | IntervalIndex | Series[Interval],
142-
dtype: IntervalDtype | None = None,
173+
dtype: PyArrowStrDtypeArg | None = None,
143174
copy: bool = True,
144-
) -> IntervalArray: ...
145-
@overload
146-
def array(
147-
# TODO: Categorical Series pandas-dev/pandas-stubs#1415
148-
data: Categorical | CategoricalIndex,
149-
dtype: CategoryDtypeArg | None = None,
150-
copy: bool = True,
151-
) -> Categorical: ...
175+
) -> ArrowStringArray: ...
152176
@overload
153177
def array(
154-
data: (
155-
SequenceNotStr[object]
156-
| np.typing.NDArray[np.object_]
157-
| NumpyExtensionArray
158-
| RangeIndex
159-
),
160-
dtype: str | np.dtype | ExtensionDtype | None = None,
178+
data: SequenceNotStr[object] | np_ndarray | NumpyExtensionArray | RangeIndex,
179+
dtype: NumpyDtypeArg | None = None,
161180
copy: bool = True,
162181
) -> NumpyExtensionArray: ...
163182
@overload
164-
def array(
165-
data: SparseArray | SparseIndex,
166-
dtype: str | np.dtype | ExtensionDtype | None = None,
167-
copy: bool = True,
168-
) -> SparseArray: ...
169-
@overload
170183
def array(
171184
data: ArrowExtensionArray,
172-
dtype: str | np.dtype | ExtensionDtype | None = None,
185+
dtype: PyArrowNotStrDtypeArg | None = None,
173186
copy: bool = True,
174187
) -> ArrowExtensionArray: ...
175-
@overload
176-
def array(
177-
data: SequenceNotStr[Any] | np_ndarray | ExtensionArray | Index | Series,
178-
dtype: str | np.dtype | ExtensionDtype | None = None,
179-
copy: bool = True,
180-
) -> ExtensionArray: ...
188+
189+
# @overload
190+
# def array(
191+
# data: SequenceNotStr[Any] | np_ndarray | ExtensionArray | Index | Series,
192+
# dtype: str | np.dtype | ExtensionDtype | None = None,
193+
# copy: bool = True,
194+
# ) -> ExtensionArray: ...

tests/arrays/test_categorical.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
def test_constructor() -> None:
1010
check(assert_type(pd.array(["🐼"], dtype="category"), Categorical), Categorical)
1111
check(
12-
assert_type( # type: ignore[assert-type] # I do not understand
13-
pd.array(np.array(["🐼"]), dtype="category"), Categorical
14-
),
12+
assert_type(pd.array(np.array(["🐼"]), dtype="category"), Categorical),
1513
Categorical,
1614
)
1715
check(
@@ -32,9 +30,7 @@ def test_constructor() -> None:
3230
Categorical,
3331
)
3432
check(
35-
assert_type( # type: ignore[assert-type] # I do not understand
36-
pd.array(pd.Index(["🐼"], dtype="category")), Categorical
37-
),
33+
assert_type(pd.array(pd.Index(["🐼"], dtype="category")), Categorical),
3834
Categorical,
3935
)
4036
# TODO: Categorical Series pandas-dev/pandas-stubs#1415

tests/arrays/test_interval_array.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,10 @@
77

88
def test_constructor() -> None:
99
itv = pd.Interval(0, 1)
10-
check(
11-
assert_type( # type: ignore[assert-type] # I do not understand
12-
pd.array([itv]), IntervalArray
13-
),
14-
IntervalArray,
15-
)
16-
check(
17-
assert_type( # type: ignore[assert-type] # I do not understand
18-
pd.array([itv, None]), IntervalArray
19-
),
20-
IntervalArray,
21-
)
10+
check(assert_type(pd.array([itv]), IntervalArray), IntervalArray)
11+
check(assert_type(pd.array([itv, None]), IntervalArray), IntervalArray)
2212

23-
check(
24-
assert_type( # type: ignore[assert-type] # I do not understand
25-
pd.array(pd.array([itv])), IntervalArray
26-
),
27-
IntervalArray,
28-
)
13+
check(assert_type(pd.array(pd.array([itv])), IntervalArray), IntervalArray)
2914

3015
check(assert_type(pd.array(pd.Index([itv])), IntervalArray), IntervalArray)
3116

tests/arrays/test_numpy_extension_array.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88

99
def test_constructor() -> None:
10-
check(
11-
assert_type(pd.array([pd.NA, None]), NumpyExtensionArray), NumpyExtensionArray
12-
)
10+
# check(
11+
# assert_type(pd.array([pd.NA, None]), NumpyExtensionArray), NumpyExtensionArray
12+
# )
1313

1414
check(
1515
assert_type( # type: ignore[assert-type] # I do not understand
@@ -18,15 +18,15 @@ def test_constructor() -> None:
1818
NumpyExtensionArray,
1919
)
2020
check(
21-
assert_type( # type: ignore[assert-type] # I do not understand, mypy must have problem with two Generic Variables somehow
21+
assert_type( # type: ignore[assert-type] # I do not understand
2222
pd.array(np.array([1, "🐼"], np.object_)), NumpyExtensionArray
2323
),
2424
NumpyExtensionArray,
2525
)
26-
check(
27-
assert_type(pd.array(pd.array([pd.NA, None])), NumpyExtensionArray),
28-
NumpyExtensionArray,
29-
)
26+
# check(
27+
# assert_type(pd.array(pd.array([pd.NA, None])), NumpyExtensionArray),
28+
# NumpyExtensionArray,
29+
# )
3030
check(
3131
assert_type(pd.array(pd.RangeIndex(0, 1)), NumpyExtensionArray),
3232
NumpyExtensionArray,

tests/arrays/test_period_array.py

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,11 @@
77

88
def test_constructor() -> None:
99
prd = pd.Period("2023-01-01")
10-
check(
11-
assert_type( # type: ignore[assert-type] # I do not understand
12-
pd.array([prd]), PeriodArray
13-
),
14-
PeriodArray,
15-
)
16-
check(
17-
assert_type( # type: ignore[assert-type] # I do not understand
18-
pd.array([prd, None]), PeriodArray
19-
),
20-
PeriodArray,
21-
)
22-
check(
23-
assert_type( # type: ignore[assert-type] # I do not understand
24-
pd.array([prd, pd.NaT, None]), PeriodArray
25-
),
26-
PeriodArray,
27-
)
10+
check(assert_type(pd.array([prd]), PeriodArray), PeriodArray)
11+
check(assert_type(pd.array([prd, None]), PeriodArray), PeriodArray)
12+
check(assert_type(pd.array([prd, pd.NaT, None]), PeriodArray), PeriodArray)
2813

29-
check(
30-
assert_type( # type: ignore[assert-type] # I do not understand
31-
pd.array(pd.array([prd])), PeriodArray
32-
),
33-
PeriodArray,
34-
)
14+
check(assert_type(pd.array(pd.array([prd])), PeriodArray), PeriodArray)
3515

3616
check(assert_type(pd.array(pd.Index([prd])), PeriodArray), PeriodArray)
3717

0 commit comments

Comments
 (0)