Skip to content

Commit ae5722b

Browse files
Add typing to core
1 parent 9606d11 commit ae5722b

26 files changed

+193
-100
lines changed

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=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-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from functools import partial
33
import operator
44
from shutil import get_terminal_size
5-
from typing import Dict, Hashable, List, Sequence, Type, TypeVar, Union, cast
5+
from typing import Dict, Hashable, List, Optional, Sequence, Type, TypeVar, Union, cast
66
from warnings import warn
77

88
import numpy as np
@@ -303,7 +303,7 @@ def __init__(
303303
values,
304304
categories=None,
305305
ordered=None,
306-
dtype=None,
306+
dtype: Optional[Dtype] = None,
307307
fastpath=False,
308308
copy: bool = True,
309309
):
@@ -405,7 +405,7 @@ def _constructor(self) -> Type["Categorical"]:
405405
return Categorical
406406

407407
@classmethod
408-
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
408+
def _from_sequence(cls, scalars, *, dtype: Optional[Dtype] = None, copy=False):
409409
return Categorical(scalars, dtype=dtype, copy=copy)
410410

411411
def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
@@ -540,7 +540,9 @@ def _from_inferred_categories(
540540
return cls(codes, dtype=dtype, fastpath=True)
541541

542542
@classmethod
543-
def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
543+
def from_codes(
544+
cls, codes, categories=None, ordered=None, dtype: Optional[Dtype] = None
545+
):
544546
"""
545547
Make a Categorical type from codes and categories or dtype.
546548
@@ -1628,7 +1630,7 @@ def _values_for_rank(self):
16281630
)
16291631
return values
16301632

1631-
def view(self, dtype=None):
1633+
def view(self, dtype: Optional[Dtype] = None):
16321634
if dtype is not None:
16331635
raise NotImplementedError(dtype)
16341636
return self._from_backing_data(self._ndarray)
@@ -2323,7 +2325,7 @@ def replace(self, to_replace, value, inplace: bool = False):
23232325

23242326
# ------------------------------------------------------------------------
23252327
# String methods interface
2326-
def _str_map(self, f, na_value=np.nan, dtype=np.dtype(object)):
2328+
def _str_map(self, f, na_value=np.nan, dtype: Optional[Dtype] = np.dtype(object)):
23272329
# Optimization to apply the callable `f` to the categories once
23282330
# and rebuild the result by `take`ing from the result with the codes.
23292331
# Returns the same type as the object-dtype implementation though.

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
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[Dtype] = 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/datetimes.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
to_offset,
2323
tzconversion,
2424
)
25+
from pandas._typing import Dtype
2526
from pandas.errors import PerformanceWarning
2627

2728
from pandas.core.dtypes.cast import astype_dt64_to_dt64tz
@@ -216,7 +217,9 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps):
216217
_dtype: Union[np.dtype, DatetimeTZDtype]
217218
_freq = None
218219

219-
def __init__(self, values, dtype=DT64NS_DTYPE, freq=None, copy=False):
220+
def __init__(
221+
self, values, dtype: Optional[Dtype] = DT64NS_DTYPE, freq=None, copy=False
222+
):
220223
if isinstance(values, (ABCSeries, ABCIndex)):
221224
values = values._values
222225

@@ -290,7 +293,10 @@ def __init__(self, values, dtype=DT64NS_DTYPE, freq=None, copy=False):
290293

291294
@classmethod
292295
def _simple_new(
293-
cls, values, freq: Optional[BaseOffset] = None, dtype=DT64NS_DTYPE
296+
cls,
297+
values,
298+
freq: Optional[BaseOffset] = None,
299+
dtype: Optional[Dtype] = DT64NS_DTYPE,
294300
) -> "DatetimeArray":
295301
assert isinstance(values, np.ndarray)
296302
if values.dtype != DT64NS_DTYPE:
@@ -304,14 +310,16 @@ def _simple_new(
304310
return result
305311

306312
@classmethod
307-
def _from_sequence(cls, scalars, *, dtype=None, copy: bool = False):
313+
def _from_sequence(
314+
cls, scalars, *, dtype: Optional[Dtype] = None, copy: bool = False
315+
):
308316
return cls._from_sequence_not_strict(scalars, dtype=dtype, copy=copy)
309317

310318
@classmethod
311319
def _from_sequence_not_strict(
312320
cls,
313321
data,
314-
dtype=None,
322+
dtype: Optional[Dtype] = None,
315323
copy=False,
316324
tz=None,
317325
freq=lib.no_default,
@@ -549,7 +557,7 @@ def _resolution_obj(self) -> Resolution:
549557
# ----------------------------------------------------------------
550558
# Array-Like / EA-Interface Methods
551559

552-
def __array__(self, dtype=None) -> np.ndarray:
560+
def __array__(self, dtype: Optional[Dtype] = None) -> np.ndarray:
553561
if dtype is None and self.tz:
554562
# The default for tz-aware is object, to preserve tz info
555563
dtype = object
@@ -1861,7 +1869,7 @@ def to_julian_date(self):
18611869
def std(
18621870
self,
18631871
axis=None,
1864-
dtype=None,
1872+
dtype: Optional[Dtype] = None,
18651873
out=None,
18661874
ddof: int = 1,
18671875
keepdims: bool = False,
@@ -1884,7 +1892,7 @@ def std(
18841892

18851893
def sequence_to_dt64ns(
18861894
data,
1887-
dtype=None,
1895+
dtype: Optional[Dtype] = None,
18881896
copy=False,
18891897
tz=None,
18901898
dayfirst=False,

pandas/core/arrays/interval.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import operator
22
from operator import le, lt
33
import textwrap
4-
from typing import Sequence, Type, TypeVar
4+
from typing import Optional, Sequence, Type, TypeVar
55

66
import numpy as np
77

@@ -14,7 +14,7 @@
1414
intervals_to_interval_bounds,
1515
)
1616
from pandas._libs.missing import NA
17-
from pandas._typing import ArrayLike
17+
from pandas._typing import ArrayLike, Dtype
1818
from pandas.compat.numpy import function as nv
1919
from pandas.util._decorators import Appender
2020

@@ -170,7 +170,7 @@ def __new__(
170170
cls,
171171
data,
172172
closed=None,
173-
dtype=None,
173+
dtype: Optional[Dtype] = None,
174174
copy: bool = False,
175175
verify_integrity: bool = True,
176176
):
@@ -212,7 +212,13 @@ def __new__(
212212

213213
@classmethod
214214
def _simple_new(
215-
cls, left, right, closed=None, copy=False, dtype=None, verify_integrity=True
215+
cls,
216+
left,
217+
right,
218+
closed=None,
219+
copy=False,
220+
dtype: Optional[Dtype] = None,
221+
verify_integrity=True,
216222
):
217223
result = IntervalMixin.__new__(cls)
218224

@@ -279,7 +285,7 @@ def _simple_new(
279285
return result
280286

281287
@classmethod
282-
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
288+
def _from_sequence(cls, scalars, *, dtype: Optional[Dtype] = None, copy=False):
283289
return cls(scalars, dtype=dtype, copy=copy)
284290

285291
@classmethod
@@ -338,7 +344,9 @@ def _from_factorized(cls, values, original):
338344
),
339345
}
340346
)
341-
def from_breaks(cls, breaks, closed="right", copy=False, dtype=None):
347+
def from_breaks(
348+
cls, breaks, closed="right", copy=False, dtype: Optional[Dtype] = None
349+
):
342350
breaks = maybe_convert_platform_interval(breaks)
343351

344352
return cls.from_arrays(breaks[:-1], breaks[1:], closed, copy=copy, dtype=dtype)
@@ -407,7 +415,9 @@ def from_breaks(cls, breaks, closed="right", copy=False, dtype=None):
407415
),
408416
}
409417
)
410-
def from_arrays(cls, left, right, closed="right", copy=False, dtype=None):
418+
def from_arrays(
419+
cls, left, right, closed="right", copy=False, dtype: Optional[Dtype] = None
420+
):
411421
left = maybe_convert_platform_interval(left)
412422
right = maybe_convert_platform_interval(right)
413423

@@ -464,7 +474,9 @@ def from_arrays(cls, left, right, closed="right", copy=False, dtype=None):
464474
),
465475
}
466476
)
467-
def from_tuples(cls, data, closed="right", copy=False, dtype=None):
477+
def from_tuples(
478+
cls, data, closed="right", copy=False, dtype: Optional[Dtype] = None
479+
):
468480
if len(data):
469481
left, right = [], []
470482
else:

pandas/core/arrays/numpy_.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import numbers
2-
from typing import Tuple, Type, Union
2+
from typing import Optional, Tuple, Type, Union
33

44
import numpy as np
55
from numpy.lib.mixins import NDArrayOperatorsMixin
66

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

1111
from pandas.core.dtypes.dtypes import ExtensionDtype
@@ -172,7 +172,7 @@ def __init__(self, values: Union[np.ndarray, "PandasArray"], copy: bool = False)
172172

173173
@classmethod
174174
def _from_sequence(
175-
cls, scalars, *, dtype=None, copy: bool = False
175+
cls, scalars, *, dtype: Optional[Dtype] = None, copy: bool = False
176176
) -> "PandasArray":
177177
if isinstance(dtype, PandasDtype):
178178
dtype = dtype._dtype

0 commit comments

Comments
 (0)