Skip to content

TYP: Annotated pandas/core/indexing.py #29908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def _has_valid_tuple(self, key: Tuple):
"[{types}] types".format(types=self._valid_types)
)

def _is_nested_tuple_indexer(self, tup: Tuple):
def _is_nested_tuple_indexer(self, tup: Tuple) -> bool:
if any(isinstance(ax, ABCMultiIndex) for ax in self.obj.axes):
return any(is_nested_tuple(tup, ax) for ax in self.obj.axes)
return False
Expand Down Expand Up @@ -273,10 +273,10 @@ def _convert_slice_indexer(self, key: slice, axis: int):
ax = self.obj._get_axis(min(axis, self.ndim - 1))
return ax._convert_slice_indexer(key, kind=self.name)

def _has_valid_setitem_indexer(self, indexer):
def _has_valid_setitem_indexer(self, indexer) -> bool:
return True

def _has_valid_positional_setitem_indexer(self, indexer):
def _has_valid_positional_setitem_indexer(self, indexer) -> bool:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like this always either raises or returns True; is that accurate? if so, maybe get rid of the return value altogether (might need to update usages)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel confident enough in reformatting this function, I'll leave it for someone else who will probably won't break the package.

""" validate that an positional indexer cannot enlarge its target
will raise if needed, does not modify the indexer externally
"""
Expand Down Expand Up @@ -1318,7 +1318,7 @@ def __init__(self, name, obj):
super().__init__(name, obj)

@Appender(_NDFrameIndexer._validate_key.__doc__)
def _validate_key(self, key, axis: int):
def _validate_key(self, key, axis: int) -> bool:
if isinstance(key, slice):
return True

Expand Down Expand Up @@ -1689,7 +1689,7 @@ def _validate_key(self, key, axis: int):
if not is_list_like_indexer(key):
self._convert_scalar_indexer(key, axis)

def _is_scalar_access(self, key: Tuple):
def _is_scalar_access(self, key: Tuple) -> bool:
# this is a shortcut accessor to both .loc and .iloc
# that provide the equivalent access of .at and .iat
# a) avoid getting things via sections and (to minimize dtype changes)
Expand Down Expand Up @@ -2002,7 +2002,7 @@ def _validate_key(self, key, axis: int):
def _has_valid_setitem_indexer(self, indexer):
self._has_valid_positional_setitem_indexer(indexer)

def _is_scalar_access(self, key: Tuple):
def _is_scalar_access(self, key: Tuple) -> bool:
# this is a shortcut accessor to both .loc and .iloc
# that provide the equivalent access of .at and .iat
# a) avoid getting things via sections and (to minimize dtype changes)
Expand All @@ -2026,7 +2026,7 @@ def _getitem_scalar(self, key):
values = self.obj._get_value(*key, takeable=True)
return values

def _validate_integer(self, key: int, axis: int):
def _validate_integer(self, key: int, axis: int) -> None:
"""
Check that 'key' is a valid position in the desired axis.

Expand Down Expand Up @@ -2452,7 +2452,7 @@ def maybe_convert_ix(*args):
return args


def is_nested_tuple(tup, labels):
def is_nested_tuple(tup, labels) -> bool:
# check for a compatible nested tuple and multiindexes among the axes
if not isinstance(tup, tuple):
return False
Expand All @@ -2465,12 +2465,12 @@ def is_nested_tuple(tup, labels):
return False


def is_label_like(key):
def is_label_like(key) -> bool:
# select a label or row
return not isinstance(key, slice) and not is_list_like_indexer(key)


def need_slice(obj):
def need_slice(obj) -> bool:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like obj will be a slice?

return (
obj.start is not None
or obj.stop is not None
Expand All @@ -2491,7 +2491,7 @@ def _non_reducing_slice(slice_):
if isinstance(slice_, kinds):
slice_ = IndexSlice[:, slice_]

def pred(part):
def pred(part) -> bool:
# true when slice does *not* reduce, False when part is a tuple,
# i.e. MultiIndex slice
return (isinstance(part, slice) or is_list_like(part)) and not isinstance(
Expand Down Expand Up @@ -2523,7 +2523,7 @@ def _maybe_numeric_slice(df, slice_, include_bool=False):
return slice_


def _can_do_equal_len(labels, value, plane_indexer, lplane_indexer, obj):
def _can_do_equal_len(labels, value, plane_indexer, lplane_indexer, obj) -> bool:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can any of the args here be typed?

""" return True if we have an equal len settable """
if not len(labels) == 1 or not np.iterable(value) or is_scalar(plane_indexer[0]):
return False
Expand Down