-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
TYP: get_indexer #40612
TYP: get_indexer #40612
Conversation
@simonjayhawkins gentle ping |
can you rebase |
cc @WillAyd any idea whats going on here? my confusion only deepens when i try to do the same thing for Index.join |
rebased, still no idea whats going on with mypy |
@MarcoGorelli #40197 makes me think you have a better grasp of |
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.
Hey @jbrockmendel ,
Yeah, I'm embarassed by how long I spent making sense of this in order to do #40200 and #40197 (maybe I should write a blog post* about it)
I explain it a bit more clearly in ##40200 (comment) , but in essence, you need an overload for each combination of keyword argument which precedes the one you're overloading
Here, there are no keyword arguments preceding unique
, so you'll just need overloads for:
Literal[True] = ...
Literal[False]
bool = ...
What's confusing here is that the bool
overload is needed - IMO, it shouldn't be, as the union of Literal[True]
and Literal[False]
should be regarded as the same as bool
, but I opened an issue about that in mypy and have had no response: python/mypy#10194
*just put something down while it's still fresh in my mind - and in case it's useful to you: https://medium.com/@m.e.gorelli/making-sense-of-typing-overload-437e6deecade
Indeed, this seems to work: @overload
def _get_indexer_non_comparable(
self, target: Index, method, unique: Literal[True] = ...
) -> np.ndarray:
# returned ndarray is np.intp
...
@overload
def _get_indexer_non_comparable(
self, target: Index, method, unique: Literal[False]
) -> tuple[np.ndarray, np.ndarray]:
# both returned ndarrays are np.intp
...
@overload
def _get_indexer_non_comparable(
self, target: Index, method, unique: bool = ...
) -> Union[tuple[np.ndarray, np.ndarray], np.ndarray]:
... |
It does, but AFAICT it isnt the 3rd |
related: what if the argument for which we need to omit the update: it looks like just pretending in the overloading line that that previous argument is not keyword-only works. but that feels very sketchy |
You need to omit Otherwise, consider what happens if you don't do Check the mypy docs on this:
See what I did for In case you haven't already, see this open issue in mypy about this topic: python/mypy#6580 |
Are any of the remaining comments actionable? |
gentle ping @MarcoGorelli |
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.
As far as I can tell, this is correct (though probably I don't know this part of the codebase well enough to merge)
The only part here I'm not totally comfortable commenting on is the change from missing
to ensure_platform_int(missing)
, as that involves making sense of pxi.in
files, which I've not (yet) done. If that change is correct, then I'm pretty sure the rest is fine
@@ -5217,7 +5221,7 @@ def get_indexer_non_unique(self, target): | |||
tgt_values = target._get_engine_target() | |||
|
|||
indexer, missing = self._engine.get_indexer_non_unique(tgt_values) | |||
return ensure_platform_int(indexer), missing | |||
return ensure_platform_int(indexer), ensure_platform_int(missing) |
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.
perhaps you could just comment on why this change is necessary, and what it does?
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 think this was from before a separate PR ensured that self._engine.get_indexer_non_unique
already returned ndarray[intp], but this makes it a little bit more explicit (and is cheap)
Thanks for explaining - thanks @jbrockmendel |
* TYP: get_indexer * update per discussion in pandas-dev#40612 * one more overload * pre-commit fixup
* TYP: get_indexer * update per discussion in pandas-dev#40612 * one more overload * pre-commit fixup
* TYP: get_indexer * update per discussion in pandas-dev#40612 * one more overload * pre-commit fixup
@simonjayhawkins im at a loss on why im getting a mypy complaint:
AFAICT they should be compatible. Am I missing something obvious?