Skip to content

TYP: Added type hint to Index.repeat #54739

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
Aug 29, 2023
Merged

Conversation

mecopur
Copy link
Contributor

@mecopur mecopur commented Aug 24, 2023

@noatamir @phofl

@mroeschke mroeschke added the Sprints Sprint Pull Requests label Aug 24, 2023
@@ -1205,7 +1206,7 @@ def _maybe_disallow_fill(self, allow_fill: bool, fill_value, indices) -> bool:
"""

@Appender(_index_shared_docs["repeat"] % _index_doc_kwargs)
def repeat(self, repeats, axis=None):
def repeat(self, repeats, axis: AxisInt | None = None):
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to change it to axis: None = None as the docs say:

Must be None. Has no effect but is accepted for compatibility with numpy.

Copy link
Member

Choose a reason for hiding this comment

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

Same applies to the other two changes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, no issues when I change it to axis: None = None in repeat. However, I got the error below from type validation when I changed it in argmin:

pandas/core/indexes/base.py:7276: error: Argument 1 of "argmin" is incompatible with 
supertype "IndexOpsMixin"; supertype defines the argument type as "int | None"  [override]

Same for argmax. Should I change it to axis: int | None = None for these two then?

Copy link
Member

Choose a reason for hiding this comment

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

The doc-string in IndexOpsMixin.argmin/max also says that it is only there for compatibility. So I would be tempted to change even IndexOpsMixin.argmin/max to axis: None = None. One could also make the argument that the type should be int | None as both are valid :) Let's make the change for at least repeat to be in sync with the doc-string but the other cases are up to you.

@mroeschke IndexOpsMixin.argmin "uses" axis exactly once - nv.validate_minmax_axis(axis). Testing an unused argument seems not very helpful but removing it probably changes the runtime behavior when people expect to get an error.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah ideally this should still error the same if a non None axis was passed

Copy link
Contributor Author

@mecopur mecopur Aug 29, 2023

Choose a reason for hiding this comment

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

When I change IndexOpsMixin.argmin to axis: None = None, I get the error below from type validation:

pandas/core/series.py:2557: error: Argument 1 to "argmin" of "IndexOpsMixin" has 
incompatible type "int"; expected "None"  [arg-type]

Series.idxmin is using IndexOpsMixin.argmin.

If nv.validate_minmax_axis(axis) function is staying as it is, I could work on the type hints related to this separately.

I realised it is also used in other functions for compatibility and the adopted type hint is axis: AxisInt | None = None (e.g. SparseArray.min, IntervalArray.min, etc.) .

Currently, there is no type hint for axis in the following functions:

Index.argmax
Index.argmin
Index.max
Index.min
RangeIndex.max
RangeIndex.min

@twoertwein twoertwein added the Typing type annotations, mypy/pyright type checking label Aug 28, 2023
@mecopur mecopur changed the title Added type hints to Index.repeat, Index.argmin and Index.argmax Added type hints to Index.repeat Aug 28, 2023
@mecopur mecopur changed the title Added type hints to Index.repeat Added type hint to Index.repeat Aug 28, 2023
@mecopur mecopur changed the title Added type hint to Index.repeat TYP: Added type hint to Index.repeat Aug 28, 2023
Copy link
Member

@twoertwein twoertwein left a comment

Choose a reason for hiding this comment

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

You can still keep the other changes, no need to remove them :)

@mroeschke mroeschke added this to the 2.2 milestone Aug 29, 2023
@mroeschke mroeschke merged commit 4f1a086 into pandas-dev:main Aug 29, 2023
@mroeschke
Copy link
Member

Thanks @mecopur

mroeschke pushed a commit to mroeschke/pandas that referenced this pull request Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Sprints Sprint Pull Requests Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants