Skip to content

np.searchsorted documentation and typing missing that single values are allowed #19160

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

Closed
Dr-Irv opened this issue Jun 3, 2021 · 5 comments · Fixed by #19165
Closed

np.searchsorted documentation and typing missing that single values are allowed #19160

Dr-Irv opened this issue Jun 3, 2021 · 5 comments · Fixed by #19165

Comments

@Dr-Irv
Copy link

Dr-Irv commented Jun 3, 2021

Documentation

The documentation (and associated typing in the pyi files) for np.searchsorted is missing that you can pass a single argument. and get a single integer as the return type.

https://numpy.org/doc/stable/reference/generated/numpy.searchsorted.html

Example code that illustrates this:

import numpy as np
a = np.array([1, 3, 5])
# reveal_type(a)
s = a.searchsorted(4)
# reveal_type(s)
print(s, type(s), isinstance(s, np.ndarray))

Result of running this is:

2 <class 'numpy.int64'> False

If I uncomment the reveal_type lines, the types of a and s are both np.ndarray.

So the docs should be updated to reflect that you can pass a single value, and, as a result a single integer is returned. In addition, the typing signature should be fixed as well.

@BvB93
Copy link
Member

BvB93 commented Jun 3, 2021

Hi @Dr-Irv, the necessary ndarray.searchsorted overload was added back in #19005,
so the typing aspect of this issue will be resolved once NumPy 1.21 goes live.
The first release candidate is already up on PyPi, so that's also an option in case you're interested.

@Dr-Irv
Copy link
Author

Dr-Irv commented Jun 3, 2021

@BvB93 That's good news on the typing. But the docs should be fixed as well.

@BvB93
Copy link
Member

BvB93 commented Jun 3, 2021

I agree, yes. A documentation fix is currently up at #19165.

@belm0
Copy link
Contributor

belm0 commented Jun 4, 2021

The typing issue can cause mypy errors as a result. This is a regression from earlier numpy versions.

E.g.:

index = np.searchsorted(foo, 2)
baz = bar[:index]
error: Slice index must be an integer or None  [misc]

@BvB93
Copy link
Member

BvB93 commented Jun 4, 2021

The typing issue can cause mypy errors as a result. This is a regression from earlier numpy versions.

That would make sense, as numpy versions prior to 1.20 didn't contain any type annotations (i.e. index: Any).

As for the provided example, I'm afraid that the issue you're looking at is mypy being overly strict (xref python/mypy#2410).
Despite the slice class-constructor being annotated as taking any object (ref), mypy nevertheless demands
either None or builtins.int for the explicit slicing syntax (:).

In contrast, np.searchsorted, and many numpy functions in general, return either np.intp or an array thereof, neither of which directly inherits from builtins.int.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants