Skip to content

type datetimeindex constructor #1120

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 7 commits into from
Feb 18, 2025

Conversation

MarcoGorelli
Copy link
Member

  • Closes #xxxx (Replace xxxx with the Github issue number)
  • Tests added: Please use assert_type() to assert the type of any return value

@MarcoGorelli MarcoGorelli marked this pull request as ready for review February 14, 2025 18:11
Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

caught one issue regarding MultiIndex

@@ -44,7 +44,7 @@ class MultiIndex(Index[Any]):
names=...,
dtype=...,
copy=...,
name=...,
name: Hashable = ...,
verify_integrity: bool = ...,
_set_identity: bool = ...,
) -> None: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

We have an opportunity for cleanup here. No need to have both __new__() and __init__(). We should just have __init__(). We use __new__() instead of __init__() in the stubs when we want to return different subclasses based on the arguments. But for MultiIndex, that isn't the case. So we can delete __new__().

Ideally, we'd have types for all of the arguments, which will have to be based on the 3.0 docs, because the docs did get updated to correspond to what is in the code (which doesn't have _set_identity.

For this PR, I'm OK if you don't do them all, BUT names and name need to be SequenceNotStr[Hashable]

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks Irv! sure, I corrected name for multiindex, sorry for not having spotted that

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 we might need MultiIndex.__new__ too, else the test I added fails pyright with

  /home/marcogorelli/pandas-stubs-dev/tests/test_indexes.py:70:62 - error: Argument of type "list[str]" cannot be assigned to parameter "name" of type "Hashable" in function "__new__"
    "list[str]" is incompatible with protocol "Hashable"

Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting. I guess because the parent class uses __new__(), then the subclass has to do it as well. So for MultiIndex, we should provide __new__() and not provide __init__()

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

The only thing left to do is to remove __init__() from MultiIndex

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

thanks @MarcoGorelli

@Dr-Irv Dr-Irv merged commit 76f0f0e into pandas-dev:main Feb 18, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants