-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TYP: annotations for internals, set_axis #32376
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -520,7 +520,7 @@ def _obj_with_exclusions(self: FrameOrSeries) -> FrameOrSeries: | |
""" internal compat with SelectionMixin """ | ||
return self | ||
|
||
def set_axis(self, labels, axis=0, inplace=False): | ||
def set_axis(self, labels, axis: Axis = 0, inplace: bool = False): | ||
""" | ||
Assign desired index to given axis. | ||
|
||
|
@@ -561,7 +561,8 @@ def set_axis(self, labels, axis=0, inplace=False): | |
obj.set_axis(labels, axis=axis, inplace=True) | ||
return obj | ||
|
||
def _set_axis(self, axis, labels) -> None: | ||
def _set_axis(self, axis: int, labels: Index) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. labels should be the same as index_like parameter of ensure_index There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thoughts on Best Practices here? I'd like to push up the stack where we do ensure_index. so in this case the annotation is kind of "you should pass Index, but we're not that strict about it" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it makes sense to limit the types of internal functions to reduce the number of duplicated checks. maybe add a TODO comment here that the ensure_index added is to be eventually removed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
mypy will be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
im skeptical in this case because it goes through cython. This gets called when you do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
fair enough, cython is another pain point for us with type annotations. |
||
labels = ensure_index(labels) | ||
self._data.set_axis(axis, labels) | ||
self._clear_item_cache() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,15 +164,15 @@ def __nonzero__(self): | |
__bool__ = __nonzero__ | ||
|
||
@property | ||
def shape(self): | ||
def shape(self) -> Tuple[int, ...]: | ||
return tuple(len(ax) for ax in self.axes) | ||
|
||
@property | ||
def ndim(self) -> int: | ||
return len(self.axes) | ||
|
||
def set_axis(self, axis, new_labels): | ||
new_labels = ensure_index(new_labels) | ||
def set_axis(self, axis: int, new_labels: Index): | ||
# Caller is responsible for ensuring we have an Index object. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if this isn't public, the comment isn't needed with type annotations There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is mypy smart enough to enforce this? I'm hoping to ween internals off of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my only concern is keeping comments in-sync with code changes over time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
as we add more type hints throughout the code. (the pace should be quicker but too much bikeshedding IMO) if mypy is green, job done! you can only get so far if the type hints are wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Fair enough, im happy to defer to you on these There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The point is, mypy is the checker, so the reviewer's don't need to be. (there does need to be some consideration of style, readability, maintenance etc) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
and we could start to address those concerns with tools like https://pypi.org/project/flake8-annotations-complexity/ |
||
old_len = len(self.axes[axis]) | ||
new_len = len(new_labels) | ||
|
||
|
@@ -184,7 +184,9 @@ def set_axis(self, axis, new_labels): | |
|
||
self.axes[axis] = new_labels | ||
|
||
def rename_axis(self, mapper, axis, copy: bool = True, level=None): | ||
def rename_axis( | ||
self, mapper, axis: int, copy: bool = True, level=None | ||
) -> "BlockManager": | ||
""" | ||
Rename one of axes. | ||
|
||
|
@@ -193,7 +195,7 @@ def rename_axis(self, mapper, axis, copy: bool = True, level=None): | |
mapper : unary callable | ||
axis : int | ||
copy : bool, default True | ||
level : int, default None | ||
level : int or None, default None | ||
""" | ||
obj = self.copy(deep=copy) | ||
obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level)) | ||
|
@@ -233,7 +235,7 @@ def _rebuild_blknos_and_blklocs(self): | |
self._blklocs = new_blklocs | ||
|
||
@property | ||
def items(self): | ||
def items(self) -> Index: | ||
return self.axes[0] | ||
|
||
def _get_counts(self, f): | ||
|
@@ -623,7 +625,7 @@ def comp(s, regex=False): | |
bm._consolidate_inplace() | ||
return bm | ||
|
||
def is_consolidated(self): | ||
def is_consolidated(self) -> bool: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as an aside, could this be a property? and remove/inline _consolidate_check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably could, would need to do some profiling. xref #32261. |
||
""" | ||
Return True if more than one block with the same dtype | ||
""" | ||
|
@@ -688,7 +690,7 @@ def get_numeric_data(self, copy: bool = False): | |
self._consolidate_inplace() | ||
return self.combine([b for b in self.blocks if b.is_numeric], copy) | ||
|
||
def combine(self, blocks, copy=True): | ||
def combine(self, blocks: List[Block], copy: bool = True) -> "BlockManager": | ||
""" return a new manager with the blocks """ | ||
if len(blocks) == 0: | ||
return self.make_empty() | ||
|
@@ -992,7 +994,6 @@ def delete(self, item): | |
self.blocks = tuple( | ||
b for blkno, b in enumerate(self.blocks) if not is_blk_deleted[blkno] | ||
) | ||
self._shape = None | ||
self._rebuild_blknos_and_blklocs() | ||
|
||
def set(self, item, value): | ||
|
@@ -1160,7 +1161,6 @@ def insert(self, loc: int, item, value, allow_duplicates: bool = False): | |
|
||
self.axes[0] = new_axis | ||
self.blocks += (block,) | ||
self._shape = None | ||
|
||
self._known_consolidated = False | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return type and labels?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return type is easy. for labels I'm ambivalent since it could be listlike, but should be Index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i take it back, return type depends on inplace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, that is causing us problems without Literal.