Skip to content

Commit 3161e3f

Browse files
authored
TYP: Use Self for type checking (remaining locations) (pandas-dev#51524)
* TYP: Use Self for type checking (pandas/core/internals/) * TYP: Use Self for type checking (remaining locations/)
1 parent 3135c62 commit 3161e3f

File tree

4 files changed

+16
-24
lines changed

4 files changed

+16
-24
lines changed

pandas/core/base.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
Hashable,
1313
Iterator,
1414
Literal,
15-
TypeVar,
1615
cast,
1716
final,
1817
overload,
@@ -29,6 +28,7 @@
2928
DtypeObj,
3029
IndexLabel,
3130
NDFrameT,
31+
Self,
3232
Shape,
3333
npt,
3434
)
@@ -91,8 +91,6 @@
9191
"duplicated": "IndexOpsMixin",
9292
}
9393

94-
_T = TypeVar("_T", bound="IndexOpsMixin")
95-
9694

9795
class PandasObject(DirNamesMixin):
9896
"""
@@ -285,7 +283,7 @@ def _values(self) -> ExtensionArray | np.ndarray:
285283
raise AbstractMethodError(self)
286284

287285
@final
288-
def transpose(self: _T, *args, **kwargs) -> _T:
286+
def transpose(self, *args, **kwargs) -> Self:
289287
"""
290288
Return the transpose, which is by definition self.
291289

pandas/core/dtypes/base.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
if TYPE_CHECKING:
2727
from pandas._typing import (
2828
DtypeObj,
29+
Self,
2930
Shape,
3031
npt,
3132
type_t,
@@ -228,9 +229,7 @@ def empty(self, shape: Shape) -> type_t[ExtensionArray]:
228229
return cls._empty(shape, dtype=self)
229230

230231
@classmethod
231-
def construct_from_string(
232-
cls: type_t[ExtensionDtypeT], string: str
233-
) -> ExtensionDtypeT:
232+
def construct_from_string(cls, string: str) -> Self:
234233
r"""
235234
Construct this type from a string.
236235

pandas/core/indexing.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
TYPE_CHECKING,
77
Hashable,
88
Sequence,
9-
TypeVar,
109
cast,
1110
final,
1211
)
@@ -78,15 +77,14 @@
7877
from pandas._typing import (
7978
Axis,
8079
AxisInt,
80+
Self,
8181
)
8282

8383
from pandas import (
8484
DataFrame,
8585
Series,
8686
)
8787

88-
_LocationIndexerT = TypeVar("_LocationIndexerT", bound="_LocationIndexer")
89-
9088
# "null slice"
9189
_NS = slice(None, None)
9290
_one_ellipsis_message = "indexer may only contain one '...' entry"
@@ -669,9 +667,7 @@ class _LocationIndexer(NDFrameIndexerBase):
669667
_takeable: bool
670668

671669
@final
672-
def __call__(
673-
self: _LocationIndexerT, axis: Axis | None = None
674-
) -> _LocationIndexerT:
670+
def __call__(self, axis: Axis | None = None) -> Self:
675671
# we need to return a copy of ourselves
676672
new_self = type(self)(self.name, self.obj)
677673

pandas/core/internals/base.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from typing import (
88
TYPE_CHECKING,
99
Literal,
10-
TypeVar,
1110
final,
1211
)
1312

@@ -31,9 +30,9 @@
3130
ArrayLike,
3231
AxisInt,
3332
DtypeObj,
33+
Self,
3434
Shape,
3535
)
36-
T = TypeVar("T", bound="DataManager")
3736

3837

3938
class DataManager(PandasObject):
@@ -75,25 +74,25 @@ def _validate_set_axis(self, axis: AxisInt, new_labels: Index) -> None:
7574
)
7675

7776
def reindex_indexer(
78-
self: T,
77+
self,
7978
new_axis,
8079
indexer,
8180
axis: AxisInt,
8281
fill_value=None,
8382
allow_dups: bool = False,
8483
copy: bool = True,
8584
only_slice: bool = False,
86-
) -> T:
85+
) -> Self:
8786
raise AbstractMethodError(self)
8887

8988
@final
9089
def reindex_axis(
91-
self: T,
90+
self,
9291
new_index: Index,
9392
axis: AxisInt,
9493
fill_value=None,
9594
only_slice: bool = False,
96-
) -> T:
95+
) -> Self:
9796
"""
9897
Conform data manager to new index.
9998
"""
@@ -108,7 +107,7 @@ def reindex_axis(
108107
only_slice=only_slice,
109108
)
110109

111-
def _equal_values(self: T, other: T) -> bool:
110+
def _equal_values(self, other: Self) -> bool:
112111
"""
113112
To be implemented by the subclasses. Only check the column values
114113
assuming shape and indexes have already been checked.
@@ -132,15 +131,15 @@ def equals(self, other: object) -> bool:
132131
return self._equal_values(other)
133132

134133
def apply(
135-
self: T,
134+
self,
136135
f,
137136
align_keys: list[str] | None = None,
138137
**kwargs,
139-
) -> T:
138+
) -> Self:
140139
raise AbstractMethodError(self)
141140

142141
@final
143-
def isna(self: T, func) -> T:
142+
def isna(self, func) -> Self:
144143
return self.apply("apply", func=func)
145144

146145
# --------------------------------------------------------------------
@@ -149,7 +148,7 @@ def isna(self: T, func) -> T:
149148
def is_consolidated(self) -> bool:
150149
return True
151150

152-
def consolidate(self: T) -> T:
151+
def consolidate(self) -> Self:
153152
return self
154153

155154
def _consolidate_inplace(self) -> None:

0 commit comments

Comments
 (0)