Skip to content

Commit 1d24301

Browse files
avinashpanchamluckyvs1
authored andcommitted
CLN: add typing to dtype arg in selection of files in core/arrays (GH38808) (pandas-dev#38826)
1 parent e328ac0 commit 1d24301

File tree

8 files changed

+38
-29
lines changed

8 files changed

+38
-29
lines changed

pandas/_typing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@
9494
Axes = Collection
9595

9696
# dtypes
97+
NpDtype = Union[str, np.dtype]
9798
Dtype = Union[
98-
"ExtensionDtype", str, np.dtype, Type[Union[str, float, int, complex, bool, object]]
99+
"ExtensionDtype", NpDtype, Type[Union[str, float, int, complex, bool, object]]
99100
]
100101
# DtypeArg specifies all allowable dtypes in a functions its dtype argument
101102
DtypeArg = Union[Dtype, Dict[Label, Dtype]]

pandas/core/arrays/base.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import numpy as np
2626

2727
from pandas._libs import lib
28-
from pandas._typing import ArrayLike, Shape
28+
from pandas._typing import ArrayLike, Dtype, Shape
2929
from pandas.compat import set_function_name
3030
from pandas.compat.numpy import function as nv
3131
from pandas.errors import AbstractMethodError
@@ -189,7 +189,7 @@ class ExtensionArray:
189189
# ------------------------------------------------------------------------
190190

191191
@classmethod
192-
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
192+
def _from_sequence(cls, scalars, *, dtype: Optional[Dtype] = None, copy=False):
193193
"""
194194
Construct a new ExtensionArray from a sequence of scalars.
195195
@@ -211,7 +211,9 @@ def _from_sequence(cls, scalars, *, dtype=None, copy=False):
211211
raise AbstractMethodError(cls)
212212

213213
@classmethod
214-
def _from_sequence_of_strings(cls, strings, *, dtype=None, copy=False):
214+
def _from_sequence_of_strings(
215+
cls, strings, *, dtype: Optional[Dtype] = None, copy=False
216+
):
215217
"""
216218
Construct a new ExtensionArray from a sequence of strings.
217219
@@ -391,7 +393,10 @@ def __ne__(self, other: Any) -> ArrayLike:
391393
return ~(self == other)
392394

393395
def to_numpy(
394-
self, dtype=None, copy: bool = False, na_value=lib.no_default
396+
self,
397+
dtype: Optional[Dtype] = None,
398+
copy: bool = False,
399+
na_value=lib.no_default,
395400
) -> np.ndarray:
396401
"""
397402
Convert to a NumPy ndarray.
@@ -1065,7 +1070,7 @@ def copy(self: ExtensionArrayT) -> ExtensionArrayT:
10651070
"""
10661071
raise AbstractMethodError(self)
10671072

1068-
def view(self, dtype=None) -> ArrayLike:
1073+
def view(self, dtype: Optional[Dtype] = None) -> ArrayLike:
10691074
"""
10701075
Return a view on the array.
10711076

pandas/core/arrays/boolean.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import numbers
2-
from typing import TYPE_CHECKING, List, Tuple, Type, Union
2+
from typing import TYPE_CHECKING, List, Optional, Tuple, Type, Union
33
import warnings
44

55
import numpy as np
66

77
from pandas._libs import lib, missing as libmissing
8-
from pandas._typing import ArrayLike
8+
from pandas._typing import ArrayLike, Dtype
99
from pandas.compat.numpy import function as nv
1010

1111
from pandas.core.dtypes.common import (
@@ -273,7 +273,7 @@ def dtype(self) -> BooleanDtype:
273273

274274
@classmethod
275275
def _from_sequence(
276-
cls, scalars, *, dtype=None, copy: bool = False
276+
cls, scalars, *, dtype: Optional[Dtype] = None, copy: bool = False
277277
) -> "BooleanArray":
278278
if dtype:
279279
assert dtype == "boolean"
@@ -282,7 +282,7 @@ def _from_sequence(
282282

283283
@classmethod
284284
def _from_sequence_of_strings(
285-
cls, strings: List[str], *, dtype=None, copy: bool = False
285+
cls, strings: List[str], *, dtype: Optional[Dtype] = None, copy: bool = False
286286
) -> "BooleanArray":
287287
def map_string(s):
288288
if isna(s):

pandas/core/arrays/categorical.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Dict,
88
Hashable,
99
List,
10+
Optional,
1011
Sequence,
1112
Type,
1213
TypeVar,
@@ -21,7 +22,7 @@
2122

2223
from pandas._libs import NaT, algos as libalgos, hashtable as htable
2324
from pandas._libs.lib import no_default
24-
from pandas._typing import ArrayLike, Dtype, Ordered, Scalar
25+
from pandas._typing import ArrayLike, Dtype, NpDtype, Ordered, Scalar
2526
from pandas.compat.numpy import function as nv
2627
from pandas.util._decorators import cache_readonly, deprecate_kwarg
2728
from pandas.util._validators import validate_bool_kwarg, validate_fillna_kwargs
@@ -318,7 +319,7 @@ def __init__(
318319
values,
319320
categories=None,
320321
ordered=None,
321-
dtype=None,
322+
dtype: Optional[Dtype] = None,
322323
fastpath=False,
323324
copy: bool = True,
324325
):
@@ -423,7 +424,7 @@ def _constructor(self) -> Type["Categorical"]:
423424
return Categorical
424425

425426
@classmethod
426-
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
427+
def _from_sequence(cls, scalars, *, dtype: Optional[Dtype] = None, copy=False):
427428
return Categorical(scalars, dtype=dtype, copy=copy)
428429

429430
def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
@@ -558,7 +559,9 @@ def _from_inferred_categories(
558559
return cls(codes, dtype=dtype, fastpath=True)
559560

560561
@classmethod
561-
def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
562+
def from_codes(
563+
cls, codes, categories=None, ordered=None, dtype: Optional[Dtype] = None
564+
):
562565
"""
563566
Make a Categorical type from codes and categories or dtype.
564567
@@ -1294,7 +1297,7 @@ def _validate_fill_value(self, fill_value):
12941297

12951298
# -------------------------------------------------------------
12961299

1297-
def __array__(self, dtype=None) -> np.ndarray:
1300+
def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
12981301
"""
12991302
The numpy array interface.
13001303

pandas/core/arrays/datetimelike.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
integer_op_not_supported,
3737
round_nsint64,
3838
)
39-
from pandas._typing import DatetimeLikeScalar, DtypeObj
39+
from pandas._typing import DatetimeLikeScalar, Dtype, DtypeObj, NpDtype
4040
from pandas.compat.numpy import function as nv
4141
from pandas.errors import AbstractMethodError, NullFrequencyError, PerformanceWarning
4242
from pandas.util._decorators import Appender, Substitution, cache_readonly
@@ -107,15 +107,15 @@ class DatetimeLikeArrayMixin(OpsMixin, NDArrayBackedExtensionArray):
107107
_recognized_scalars: Tuple[Type, ...]
108108
_data: np.ndarray
109109

110-
def __init__(self, data, dtype=None, freq=None, copy=False):
110+
def __init__(self, data, dtype: Optional[Dtype] = None, freq=None, copy=False):
111111
raise AbstractMethodError(self)
112112

113113
@classmethod
114114
def _simple_new(
115115
cls: Type[DatetimeLikeArrayT],
116116
values: np.ndarray,
117117
freq: Optional[BaseOffset] = None,
118-
dtype=None,
118+
dtype: Optional[Dtype] = None,
119119
) -> DatetimeLikeArrayT:
120120
raise AbstractMethodError(cls)
121121

@@ -265,7 +265,7 @@ def _formatter(self, boxed=False):
265265
# ----------------------------------------------------------------
266266
# Array-Like / EA-Interface Methods
267267

268-
def __array__(self, dtype=None) -> np.ndarray:
268+
def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
269269
# used for Timedelta/DatetimeArray, overwritten by PeriodArray
270270
if is_object_dtype(dtype):
271271
return np.array(list(self), dtype=object)
@@ -383,7 +383,7 @@ def astype(self, dtype, copy=True):
383383
else:
384384
return np.asarray(self, dtype=dtype)
385385

386-
def view(self, dtype=None):
386+
def view(self, dtype: Optional[Dtype] = None):
387387
if dtype is None or dtype is self.dtype:
388388
return type(self)(self._ndarray, dtype=self.dtype)
389389
return self._ndarray.view(dtype=dtype)

pandas/core/arrays/integer.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66

77
from pandas._libs import iNaT, lib, missing as libmissing
8-
from pandas._typing import ArrayLike, DtypeObj
8+
from pandas._typing import ArrayLike, Dtype, DtypeObj
99
from pandas.compat.numpy import function as nv
1010
from pandas.util._decorators import cache_readonly
1111

@@ -304,14 +304,14 @@ def __abs__(self):
304304

305305
@classmethod
306306
def _from_sequence(
307-
cls, scalars, *, dtype=None, copy: bool = False
307+
cls, scalars, *, dtype: Optional[Dtype] = None, copy: bool = False
308308
) -> "IntegerArray":
309309
values, mask = coerce_to_array(scalars, dtype=dtype, copy=copy)
310310
return IntegerArray(values, mask)
311311

312312
@classmethod
313313
def _from_sequence_of_strings(
314-
cls, strings, *, dtype=None, copy: bool = False
314+
cls, strings, *, dtype: Optional[Dtype] = None, copy: bool = False
315315
) -> "IntegerArray":
316316
scalars = to_numeric(strings, errors="raise")
317317
return cls._from_sequence(scalars, dtype=dtype, copy=copy)

pandas/core/arrays/period.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
get_period_field_arr,
2727
period_asfreq_arr,
2828
)
29-
from pandas._typing import AnyArrayLike
29+
from pandas._typing import AnyArrayLike, Dtype
3030
from pandas.util._decorators import cache_readonly, doc
3131

3232
from pandas.core.dtypes.common import (
@@ -198,10 +198,10 @@ def _from_sequence(
198198
cls: Type["PeriodArray"],
199199
scalars: Union[Sequence[Optional[Period]], AnyArrayLike],
200200
*,
201-
dtype: Optional[PeriodDtype] = None,
201+
dtype: Optional[Dtype] = None,
202202
copy: bool = False,
203203
) -> "PeriodArray":
204-
if dtype:
204+
if dtype and isinstance(dtype, PeriodDtype):
205205
freq = dtype.freq
206206
else:
207207
freq = None

pandas/core/dtypes/dtypes.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from pandas._libs.interval import Interval
2323
from pandas._libs.tslibs import NaT, Period, Timestamp, dtypes, timezones, to_offset
2424
from pandas._libs.tslibs.offsets import BaseOffset
25-
from pandas._typing import DtypeObj, Ordered
25+
from pandas._typing import Dtype, DtypeObj, Ordered
2626

2727
from pandas.core.dtypes.base import ExtensionDtype, register_extension_dtype
2828
from pandas.core.dtypes.generic import ABCCategoricalIndex, ABCIndex
@@ -185,7 +185,7 @@ def _from_values_or_dtype(
185185
values=None,
186186
categories=None,
187187
ordered: Optional[bool] = None,
188-
dtype: Optional["CategoricalDtype"] = None,
188+
dtype: Optional[Dtype] = None,
189189
) -> "CategoricalDtype":
190190
"""
191191
Construct dtype from the input parameters used in :class:`Categorical`.
@@ -272,7 +272,7 @@ def _from_values_or_dtype(
272272
# ordered=None.
273273
dtype = CategoricalDtype(categories, ordered)
274274

275-
return dtype
275+
return cast(CategoricalDtype, dtype)
276276

277277
@classmethod
278278
def construct_from_string(cls, string: str_type) -> "CategoricalDtype":

0 commit comments

Comments
 (0)