Skip to content

Commit 19b0e7a

Browse files
authored
TYP: Simple type fixes for ExtensionArray (#41255)
1 parent e8a2287 commit 19b0e7a

File tree

4 files changed

+42
-23
lines changed

4 files changed

+42
-23
lines changed

pandas/_typing.py

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
# https://mypy.readthedocs.io/en/latest/common_issues.html#import-cycles
3838
if TYPE_CHECKING:
3939
from typing import (
40+
Literal,
4041
TypedDict,
4142
final,
4243
)
@@ -189,6 +190,12 @@
189190
str, int, Sequence[Union[str, int]], Mapping[Hashable, Union[str, int]]
190191
]
191192

193+
# Arguments for fillna()
194+
if TYPE_CHECKING:
195+
FillnaOptions = Literal["backfill", "bfill", "ffill", "pad"]
196+
else:
197+
FillnaOptions = str
198+
192199
# internals
193200
Manager = Union[
194201
"ArrayManager", "SingleArrayManager", "BlockManager", "SingleBlockManager"

pandas/core/arrays/base.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
TYPE_CHECKING,
1414
Any,
1515
Callable,
16+
Iterator,
1617
Sequence,
1718
TypeVar,
1819
cast,
@@ -24,6 +25,7 @@
2425
from pandas._typing import (
2526
ArrayLike,
2627
Dtype,
28+
FillnaOptions,
2729
PositionalIndexer,
2830
Shape,
2931
)
@@ -69,6 +71,7 @@
6971
)
7072

7173
if TYPE_CHECKING:
74+
from typing import Literal
7275

7376
class ExtensionArraySupportsAnyAll("ExtensionArray"):
7477
def any(self, *, skipna: bool = True) -> bool:
@@ -375,7 +378,7 @@ def __len__(self) -> int:
375378
"""
376379
raise AbstractMethodError(self)
377380

378-
def __iter__(self):
381+
def __iter__(self) -> Iterator[Any]:
379382
"""
380383
Iterate over elements of the array.
381384
"""
@@ -385,7 +388,7 @@ def __iter__(self):
385388
for i in range(len(self)):
386389
yield self[i]
387390

388-
def __contains__(self, item) -> bool | np.bool_:
391+
def __contains__(self, item: object) -> bool | np.bool_:
389392
"""
390393
Return for `item in self`.
391394
"""
@@ -400,7 +403,9 @@ def __contains__(self, item) -> bool | np.bool_:
400403
else:
401404
return False
402405
else:
403-
return (item == self).any()
406+
# error: Item "ExtensionArray" of "Union[ExtensionArray, ndarray]" has no
407+
# attribute "any"
408+
return (item == self).any() # type: ignore[union-attr]
404409

405410
# error: Signature of "__eq__" incompatible with supertype "object"
406411
def __eq__(self, other: Any) -> ArrayLike: # type: ignore[override]
@@ -678,7 +683,12 @@ def argmax(self, skipna: bool = True) -> int:
678683
raise NotImplementedError
679684
return nargminmax(self, "argmax")
680685

681-
def fillna(self, value=None, method=None, limit=None):
686+
def fillna(
687+
self,
688+
value: object | ArrayLike | None = None,
689+
method: FillnaOptions | None = None,
690+
limit: int | None = None,
691+
):
682692
"""
683693
Fill NA/NaN values using the specified method.
684694
@@ -1205,7 +1215,7 @@ def _formatter(self, boxed: bool = False) -> Callable[[Any], str | None]:
12051215
# Reshaping
12061216
# ------------------------------------------------------------------------
12071217

1208-
def transpose(self, *axes) -> ExtensionArray:
1218+
def transpose(self, *axes: int) -> ExtensionArray:
12091219
"""
12101220
Return a transposed view on this array.
12111221
@@ -1218,7 +1228,7 @@ def transpose(self, *axes) -> ExtensionArray:
12181228
def T(self) -> ExtensionArray:
12191229
return self.transpose()
12201230

1221-
def ravel(self, order="C") -> ExtensionArray:
1231+
def ravel(self, order: Literal["C", "F", "A", "K"] | None = "C") -> ExtensionArray:
12221232
"""
12231233
Return a flattened view on this array.
12241234
@@ -1292,7 +1302,7 @@ def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
12921302
"""
12931303
raise TypeError(f"cannot perform {name} with type {self.dtype}")
12941304

1295-
def __hash__(self):
1305+
def __hash__(self) -> int:
12961306
raise TypeError(f"unhashable type: {repr(type(self).__name__)}")
12971307

12981308
# ------------------------------------------------------------------------

pandas/core/frame.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
CompressionOptions,
5555
Dtype,
5656
FilePathOrBuffer,
57+
FillnaOptions,
5758
FloatFormatType,
5859
FormattersType,
5960
FrameOrSeriesUnion,
@@ -5015,7 +5016,7 @@ def rename(
50155016
def fillna(
50165017
self,
50175018
value=...,
5018-
method: str | None = ...,
5019+
method: FillnaOptions | None = ...,
50195020
axis: Axis | None = ...,
50205021
inplace: Literal[False] = ...,
50215022
limit=...,
@@ -5027,7 +5028,7 @@ def fillna(
50275028
def fillna(
50285029
self,
50295030
value,
5030-
method: str | None,
5031+
method: FillnaOptions | None,
50315032
axis: Axis | None,
50325033
inplace: Literal[True],
50335034
limit=...,
@@ -5060,7 +5061,7 @@ def fillna(
50605061
def fillna(
50615062
self,
50625063
*,
5063-
method: str | None,
5064+
method: FillnaOptions | None,
50645065
inplace: Literal[True],
50655066
limit=...,
50665067
downcast=...,
@@ -5082,7 +5083,7 @@ def fillna(
50825083
def fillna(
50835084
self,
50845085
*,
5085-
method: str | None,
5086+
method: FillnaOptions | None,
50865087
axis: Axis | None,
50875088
inplace: Literal[True],
50885089
limit=...,
@@ -5106,7 +5107,7 @@ def fillna(
51065107
def fillna(
51075108
self,
51085109
value,
5109-
method: str | None,
5110+
method: FillnaOptions | None,
51105111
*,
51115112
inplace: Literal[True],
51125113
limit=...,
@@ -5118,7 +5119,7 @@ def fillna(
51185119
def fillna(
51195120
self,
51205121
value=...,
5121-
method: str | None = ...,
5122+
method: FillnaOptions | None = ...,
51225123
axis: Axis | None = ...,
51235124
inplace: bool = ...,
51245125
limit=...,
@@ -5129,8 +5130,8 @@ def fillna(
51295130
@doc(NDFrame.fillna, **_shared_doc_kwargs)
51305131
def fillna(
51315132
self,
5132-
value=None,
5133-
method: str | None = None,
5133+
value: object | ArrayLike | None = None,
5134+
method: FillnaOptions | None = None,
51345135
axis: Axis | None = None,
51355136
inplace: bool = False,
51365137
limit=None,

pandas/core/series.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
Axis,
3939
Dtype,
4040
DtypeObj,
41+
FillnaOptions,
4142
FrameOrSeriesUnion,
4243
IndexKeyFunc,
4344
NpDtype,
@@ -4594,7 +4595,7 @@ def drop(
45944595
def fillna(
45954596
self,
45964597
value=...,
4597-
method: str | None = ...,
4598+
method: FillnaOptions | None = ...,
45984599
axis: Axis | None = ...,
45994600
inplace: Literal[False] = ...,
46004601
limit=...,
@@ -4606,7 +4607,7 @@ def fillna(
46064607
def fillna(
46074608
self,
46084609
value,
4609-
method: str | None,
4610+
method: FillnaOptions | None,
46104611
axis: Axis | None,
46114612
inplace: Literal[True],
46124613
limit=...,
@@ -4639,7 +4640,7 @@ def fillna(
46394640
def fillna(
46404641
self,
46414642
*,
4642-
method: str | None,
4643+
method: FillnaOptions | None,
46434644
inplace: Literal[True],
46444645
limit=...,
46454646
downcast=...,
@@ -4661,7 +4662,7 @@ def fillna(
46614662
def fillna(
46624663
self,
46634664
*,
4664-
method: str | None,
4665+
method: FillnaOptions | None,
46654666
axis: Axis | None,
46664667
inplace: Literal[True],
46674668
limit=...,
@@ -4685,7 +4686,7 @@ def fillna(
46854686
def fillna(
46864687
self,
46874688
value,
4688-
method: str | None,
4689+
method: FillnaOptions | None,
46894690
*,
46904691
inplace: Literal[True],
46914692
limit=...,
@@ -4697,7 +4698,7 @@ def fillna(
46974698
def fillna(
46984699
self,
46994700
value=...,
4700-
method: str | None = ...,
4701+
method: FillnaOptions | None = ...,
47014702
axis: Axis | None = ...,
47024703
inplace: bool = ...,
47034704
limit=...,
@@ -4709,8 +4710,8 @@ def fillna(
47094710
@doc(NDFrame.fillna, **_shared_doc_kwargs) # type: ignore[has-type]
47104711
def fillna(
47114712
self,
4712-
value=None,
4713-
method=None,
4713+
value: object | ArrayLike | None = None,
4714+
method: FillnaOptions | None = None,
47144715
axis=None,
47154716
inplace=False,
47164717
limit=None,

0 commit comments

Comments
 (0)