Skip to content

Commit 9b3c301

Browse files
authored
TYP: misc return types (#57285)
1 parent 99e3afe commit 9b3c301

File tree

18 files changed

+67
-37
lines changed

18 files changed

+67
-37
lines changed

pandas/_typing.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@
4646

4747
from pandas.core.dtypes.dtypes import ExtensionDtype
4848

49-
from pandas import Interval
49+
from pandas import (
50+
DatetimeIndex,
51+
Interval,
52+
PeriodIndex,
53+
TimedeltaIndex,
54+
)
5055
from pandas.arrays import (
5156
DatetimeArray,
5257
TimedeltaArray,
@@ -190,6 +195,7 @@ def __reversed__(self) -> Iterator[_T_co]:
190195
NDFrameT = TypeVar("NDFrameT", bound="NDFrame")
191196

192197
IndexT = TypeVar("IndexT", bound="Index")
198+
FreqIndexT = TypeVar("FreqIndexT", "DatetimeIndex", "PeriodIndex", "TimedeltaIndex")
193199
NumpyIndexT = TypeVar("NumpyIndexT", np.ndarray, "Index")
194200

195201
AxisInt = int

pandas/core/_numba/extensions.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from contextlib import contextmanager
1414
import operator
15+
from typing import TYPE_CHECKING
1516

1617
import numba
1718
from numba import types
@@ -40,6 +41,9 @@
4041
from pandas.core.internals import SingleBlockManager
4142
from pandas.core.series import Series
4243

44+
if TYPE_CHECKING:
45+
from pandas._typing import Self
46+
4347

4448
# Helper function to hack around fact that Index casts numpy string dtype to object
4549
#
@@ -84,7 +88,7 @@ def key(self):
8488
def as_array(self):
8589
return types.Array(self.dtype, 1, self.layout)
8690

87-
def copy(self, dtype=None, ndim: int = 1, layout=None):
91+
def copy(self, dtype=None, ndim: int = 1, layout=None) -> Self:
8892
assert ndim == 1
8993
if dtype is None:
9094
dtype = self.dtype
@@ -114,7 +118,7 @@ def key(self):
114118
def as_array(self):
115119
return self.values
116120

117-
def copy(self, dtype=None, ndim: int = 1, layout: str = "C"):
121+
def copy(self, dtype=None, ndim: int = 1, layout: str = "C") -> Self:
118122
assert ndim == 1
119123
assert layout == "C"
120124
if dtype is None:
@@ -123,7 +127,7 @@ def copy(self, dtype=None, ndim: int = 1, layout: str = "C"):
123127

124128

125129
@typeof_impl.register(Index)
126-
def typeof_index(val, c):
130+
def typeof_index(val, c) -> IndexType:
127131
"""
128132
This will assume that only strings are in object dtype
129133
index.
@@ -136,7 +140,7 @@ def typeof_index(val, c):
136140

137141

138142
@typeof_impl.register(Series)
139-
def typeof_series(val, c):
143+
def typeof_series(val, c) -> SeriesType:
140144
index = typeof_impl(val.index, c)
141145
arrty = typeof_impl(val.values, c)
142146
namety = typeof_impl(val.name, c)
@@ -532,7 +536,7 @@ def key(self):
532536

533537

534538
@typeof_impl.register(_iLocIndexer)
535-
def typeof_iloc(val, c):
539+
def typeof_iloc(val, c) -> IlocType:
536540
objtype = typeof_impl(val.obj, c)
537541
return IlocType(objtype)
538542

pandas/core/arrays/_mixins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def searchsorted(
250250
return self._ndarray.searchsorted(npvalue, side=side, sorter=sorter)
251251

252252
@doc(ExtensionArray.shift)
253-
def shift(self, periods: int = 1, fill_value=None):
253+
def shift(self, periods: int = 1, fill_value=None) -> Self:
254254
# NB: shift is always along axis=0
255255
axis = 0
256256
fill_value = self._validate_scalar(fill_value)

pandas/core/arrays/arrow/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def floordiv_compat(
204204
from pandas.core.arrays.timedeltas import TimedeltaArray
205205

206206

207-
def get_unit_from_pa_dtype(pa_dtype):
207+
def get_unit_from_pa_dtype(pa_dtype) -> str:
208208
# https://github.com/pandas-dev/pandas/pull/50998#discussion_r1100344804
209209
if pa_version_under11p0:
210210
unit = str(pa_dtype).split("[", 1)[-1][:-1]
@@ -1966,7 +1966,7 @@ def _rank(
19661966
na_option: str = "keep",
19671967
ascending: bool = True,
19681968
pct: bool = False,
1969-
):
1969+
) -> Self:
19701970
"""
19711971
See Series.rank.__doc__.
19721972
"""

pandas/core/dtypes/dtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2337,7 +2337,7 @@ def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
23372337
except NotImplementedError:
23382338
return None
23392339

2340-
def __from_arrow__(self, array: pa.Array | pa.ChunkedArray):
2340+
def __from_arrow__(self, array: pa.Array | pa.ChunkedArray) -> ArrowExtensionArray:
23412341
"""
23422342
Construct IntegerArray/FloatingArray from pyarrow Array/ChunkedArray.
23432343
"""

pandas/core/dtypes/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
# define abstract base classes to enable isinstance type checking on our
3535
# objects
36-
def create_pandas_abc_type(name, attr, comp):
36+
def create_pandas_abc_type(name, attr, comp) -> type:
3737
def _check(inst) -> bool:
3838
return getattr(inst, attr, "_typ") in comp
3939

pandas/core/groupby/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,7 @@ def _choose_path(self, fast_path: Callable, slow_path: Callable, group: DataFram
17881788

17891789
return path, res
17901790

1791-
def filter(self, func, dropna: bool = True, *args, **kwargs):
1791+
def filter(self, func, dropna: bool = True, *args, **kwargs) -> DataFrame:
17921792
"""
17931793
Filter elements from groups that don't satisfy a criterion.
17941794

pandas/core/indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1750,7 +1750,7 @@ def _get_slice_axis(self, slice_obj: slice, axis: AxisInt):
17501750
labels._validate_positional_slice(slice_obj)
17511751
return self.obj._slice(slice_obj, axis=axis)
17521752

1753-
def _convert_to_indexer(self, key, axis: AxisInt):
1753+
def _convert_to_indexer(self, key: T, axis: AxisInt) -> T:
17541754
"""
17551755
Much simpler as we only have to deal with our valid types.
17561756
"""

pandas/core/interchange/from_dataframe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def from_dataframe(df, allow_copy: bool = True) -> pd.DataFrame:
7575
)
7676

7777

78-
def _from_dataframe(df: DataFrameXchg, allow_copy: bool = True):
78+
def _from_dataframe(df: DataFrameXchg, allow_copy: bool = True) -> pd.DataFrame:
7979
"""
8080
Build a ``pd.DataFrame`` from the DataFrame interchange object.
8181

pandas/core/internals/blocks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2537,7 +2537,7 @@ def get_block_type(dtype: DtypeObj) -> type[Block]:
25372537

25382538
def new_block_2d(
25392539
values: ArrayLike, placement: BlockPlacement, refs: BlockValuesRefs | None = None
2540-
):
2540+
) -> Block:
25412541
# new_block specialized to case with
25422542
# ndim=2
25432543
# isinstance(placement, BlockPlacement)

0 commit comments

Comments
 (0)