Skip to content

TYP: Adjust typing for Blockplacement.__getitem__ #51036

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 3 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion pandas/_libs/internals.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class BlockPlacement:
@property
def is_slice_like(self) -> bool: ...
@overload
def __getitem__(self, loc: slice | Sequence[int]) -> BlockPlacement: ...
def __getitem__(
Copy link
Member

Choose a reason for hiding this comment

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

can the ndarray be made more specifically np.intp? in particular im thinking ruling out bool

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, updated

self, loc: slice | Sequence[int] | np.ndarray
) -> BlockPlacement: ...
@overload
def __getitem__(self, loc: int) -> int: ...
def __iter__(self) -> Iterator[int]: ...
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,7 @@ def getitem_block(self, slicer: slice | npt.NDArray[np.intp]) -> Block:
# is from internals.concat, and we can verify that never happens
# with 1-column blocks, i.e. never for ExtensionBlock.

# Invalid index type "Union[slice, ndarray[Any, dtype[signedinteger[Any]]]]"
# for "BlockPlacement"; expected type "Union[slice, Sequence[int]]"
new_mgr_locs = self._mgr_locs[slicer] # type: ignore[index]
new_mgr_locs = self._mgr_locs[slicer]

new_values = self._slice(slicer)

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1320,11 +1320,11 @@ def _iset_split_block(

nbs_tup = tuple(blk.delete(blk_locs))
if value is not None:
# error: No overload variant of "__getitem__" of "BlockPlacement" matches
# argument type "ndarray[Any, Any]" [call-overload]
# Argument 1 to "BlockPlacement" has incompatible type "BlockPlacement";
# expected "Union[int, slice, ndarray[Any, Any]]"
first_nb = new_block_2d(
value,
BlockPlacement(blk.mgr_locs[blk_locs]), # type: ignore[call-overload]
BlockPlacement(blk.mgr_locs[blk_locs]), # type: ignore[arg-type]
)
else:
first_nb = nbs_tup[0]
Expand Down