Skip to content

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

Merged
merged 3 commits into from
Mar 2, 2020

Conversation

jbrockmendel
Copy link
Member

No description provided.

@jbrockmendel jbrockmendel added the Typing type annotations, mypy/pyright type checking label Mar 1, 2020
Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

Thanks @jbrockmendel lgtm. some comments, not blockers.

@@ -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:
Copy link
Member

Choose a reason for hiding this comment

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

labels should be the same as index_like parameter of ensure_index

Copy link
Member Author

Choose a reason for hiding this comment

The 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"

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

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

"you should pass Index, but we're not that strict about it"

mypy will be

Copy link
Member Author

Choose a reason for hiding this comment

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

mypy will be

im skeptical in this case because it goes through cython. This gets called when you do frame.columns = ["foo"]

Copy link
Member

Choose a reason for hiding this comment

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

im skeptical in this case because it goes through cython

fair enough, cython is another pain point for us with type annotations.

@@ -3599,7 +3599,7 @@ def align(
see_also_sub=" or columns",
)
@Appender(NDFrame.set_axis.__doc__)
def set_axis(self, labels, axis=0, inplace=False):
def set_axis(self, labels, axis: Axis = 0, inplace: bool = False):
Copy link
Member

Choose a reason for hiding this comment

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

return type and labels?

Copy link
Member Author

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

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 take it back, return type depends on inplace

Copy link
Member

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

yeah, that is causing us problems without Literal.

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.
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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 ensure_index completely, the comment is to keep track of where it has been done

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

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

is mypy smart enough to enforce this?

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

the pace should be quicker but too much bikeshedding IMO

Fair enough, im happy to defer to you on these

Copy link
Member

Choose a reason for hiding this comment

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

the pace should be quicker but too much bikeshedding IMO

Fair enough, im happy to defer to you on these

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)

Copy link
Member

Choose a reason for hiding this comment

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

(there does need to be some consideration of style, readability, maintenance etc)

and we could start to address those concerns with tools like https://pypi.org/project/flake8-annotations-complexity/

@@ -184,7 +184,7 @@ 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):
Copy link
Member

Choose a reason for hiding this comment

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

return type, mapper and level

Copy link
Member Author

Choose a reason for hiding this comment

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

looks like level may be inconsistent with the caller's signature, so punting for now. xref #32349.

@@ -623,7 +623,7 @@ def comp(s, regex=False):
bm._consolidate_inplace()
return bm

def is_consolidated(self):
def is_consolidated(self) -> bool:
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

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

probably could, would need to do some profiling. xref #32261.

@simonjayhawkins simonjayhawkins added this to the 1.1 milestone Mar 2, 2020
@simonjayhawkins simonjayhawkins merged commit a1f9ae2 into pandas-dev:master Mar 2, 2020
@simonjayhawkins
Copy link
Member

Thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the set_axis branch March 2, 2020 17:32
roberthdevries pushed a commit to roberthdevries/pandas that referenced this pull request Mar 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants