Skip to content

Commit e5227c4

Browse files
jbrockmendeljreback
authored andcommitted
TYP: annotations in indexes (#31181)
1 parent 399c413 commit e5227c4

File tree

5 files changed

+77
-64
lines changed

5 files changed

+77
-64
lines changed

pandas/core/base.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
import builtins
55
import textwrap
6-
from typing import Dict, FrozenSet, List, Optional
6+
from typing import Dict, FrozenSet, List, Optional, Union
77

88
import numpy as np
99

@@ -110,7 +110,7 @@ def _freeze(self):
110110
object.__setattr__(self, "__frozen", True)
111111

112112
# prevent adding any attribute via s.xxx.new_attribute = ...
113-
def __setattr__(self, key, value):
113+
def __setattr__(self, key: str, value):
114114
# _cache is used by a decorator
115115
# We need to check both 1.) cls.__dict__ and 2.) getattr(self, key)
116116
# because
@@ -239,7 +239,7 @@ def __getitem__(self, key):
239239
raise KeyError(f"Column not found: {key}")
240240
return self._gotitem(key, ndim=1)
241241

242-
def _gotitem(self, key, ndim, subset=None):
242+
def _gotitem(self, key, ndim: int, subset=None):
243243
"""
244244
sub-classes to define
245245
return a sliced object
@@ -605,6 +605,11 @@ class IndexOpsMixin:
605605
["tolist"] # tolist is not deprecated, just suppressed in the __dir__
606606
)
607607

608+
@property
609+
def _values(self) -> Union[ExtensionArray, np.ndarray]:
610+
# must be defined here as a property for mypy
611+
raise AbstractMethodError(self)
612+
608613
def transpose(self, *args, **kwargs):
609614
"""
610615
Return the transpose, which is by definition self.
@@ -630,6 +635,10 @@ def shape(self):
630635
"""
631636
return self._values.shape
632637

638+
def __len__(self) -> int:
639+
# We need this defined here for mypy
640+
raise AbstractMethodError(self)
641+
633642
@property
634643
def ndim(self) -> int:
635644
"""
@@ -664,14 +673,14 @@ def item(self):
664673
raise ValueError("can only convert an array of size 1 to a Python scalar")
665674

666675
@property
667-
def nbytes(self):
676+
def nbytes(self) -> int:
668677
"""
669678
Return the number of bytes in the underlying data.
670679
"""
671680
return self._values.nbytes
672681

673682
@property
674-
def size(self):
683+
def size(self) -> int:
675684
"""
676685
Return the number of elements in the underlying data.
677686
"""
@@ -1058,7 +1067,14 @@ def hasnans(self):
10581067
return bool(isna(self).any())
10591068

10601069
def _reduce(
1061-
self, op, name, axis=0, skipna=True, numeric_only=None, filter_type=None, **kwds
1070+
self,
1071+
op,
1072+
name: str,
1073+
axis=0,
1074+
skipna=True,
1075+
numeric_only=None,
1076+
filter_type=None,
1077+
**kwds,
10621078
):
10631079
""" perform the reduction type operation if we can """
10641080
func = getattr(self, name, None)
@@ -1254,7 +1270,7 @@ def unique(self):
12541270

12551271
return result
12561272

1257-
def nunique(self, dropna=True):
1273+
def nunique(self, dropna: bool = True) -> int:
12581274
"""
12591275
Return number of unique elements in the object.
12601276
@@ -1295,7 +1311,7 @@ def nunique(self, dropna=True):
12951311
return n
12961312

12971313
@property
1298-
def is_unique(self):
1314+
def is_unique(self) -> bool:
12991315
"""
13001316
Return boolean if values in the object are unique.
13011317
@@ -1306,7 +1322,7 @@ def is_unique(self):
13061322
return self.nunique(dropna=False) == len(self)
13071323

13081324
@property
1309-
def is_monotonic(self):
1325+
def is_monotonic(self) -> bool:
13101326
"""
13111327
Return boolean if values in the object are
13121328
monotonic_increasing.
@@ -1319,7 +1335,11 @@ def is_monotonic(self):
13191335

13201336
return Index(self).is_monotonic
13211337

1322-
is_monotonic_increasing = is_monotonic
1338+
@property
1339+
def is_monotonic_increasing(self) -> bool:
1340+
"""alias for is_monotonic"""
1341+
# mypy complains if we alias directly
1342+
return self.is_monotonic
13231343

13241344
@property
13251345
def is_monotonic_decreasing(self) -> bool:
@@ -1472,7 +1492,7 @@ def factorize(self, sort=False, na_sentinel=-1):
14721492

14731493
@Substitution(klass="Index")
14741494
@Appender(_shared_docs["searchsorted"])
1475-
def searchsorted(self, value, side="left", sorter=None):
1495+
def searchsorted(self, value, side="left", sorter=None) -> np.ndarray:
14761496
return algorithms.searchsorted(self._values, value, side=side, sorter=sorter)
14771497

14781498
def drop_duplicates(self, keep="first", inplace=False):

pandas/core/common.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,35 +178,35 @@ def not_none(*args):
178178
return (arg for arg in args if arg is not None)
179179

180180

181-
def any_none(*args):
181+
def any_none(*args) -> bool:
182182
"""
183183
Returns a boolean indicating if any argument is None.
184184
"""
185185
return any(arg is None for arg in args)
186186

187187

188-
def all_none(*args):
188+
def all_none(*args) -> bool:
189189
"""
190190
Returns a boolean indicating if all arguments are None.
191191
"""
192192
return all(arg is None for arg in args)
193193

194194

195-
def any_not_none(*args):
195+
def any_not_none(*args) -> bool:
196196
"""
197197
Returns a boolean indicating if any argument is not None.
198198
"""
199199
return any(arg is not None for arg in args)
200200

201201

202-
def all_not_none(*args):
202+
def all_not_none(*args) -> bool:
203203
"""
204204
Returns a boolean indicating if all arguments are not None.
205205
"""
206206
return all(arg is not None for arg in args)
207207

208208

209-
def count_not_none(*args):
209+
def count_not_none(*args) -> int:
210210
"""
211211
Returns the count of arguments that are not None.
212212
"""
@@ -286,7 +286,7 @@ def maybe_iterable_to_list(obj: Union[Iterable[T], T]) -> Union[Collection[T], T
286286
return obj
287287

288288

289-
def is_null_slice(obj):
289+
def is_null_slice(obj) -> bool:
290290
"""
291291
We have a null slice.
292292
"""
@@ -306,7 +306,7 @@ def is_true_slices(l):
306306

307307

308308
# TODO: used only once in indexing; belongs elsewhere?
309-
def is_full_slice(obj, l):
309+
def is_full_slice(obj, l) -> bool:
310310
"""
311311
We have a full length slice.
312312
"""

0 commit comments

Comments
 (0)