-
-
Notifications
You must be signed in to change notification settings - Fork 11.3k
TYP: Fix np.interp
signature for scalar types
#27869
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
Conversation
@jorenham ping. |
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.
Thanks for submitting this! I left some notes on how to improve this. These are related to quite subtle and often-overlooked issues, so let me know if you don't understand my explanation.
But it's no surprise that you ran into them; these issues are currently all over the place in the numpy stubs, some of which I'm guilty of myself 😅. But even so, I think it's important to not repeat the mistakes of the past, otherwise it'll never get better 💪🏻.
np.interp
typingnp.interp
signature for scalar types
Co-authored-by: PTUsumit <2301109104@ptuniv.edu.in>
61f9844
to
bef5cf1
Compare
mypy_primer shows no diff: |
ready for review @charris |
left: None | _FloatLike_co = ..., | ||
right: None | _FloatLike_co = ..., | ||
period: None | _FloatLike_co = ..., | ||
left: _FloatLike_co | None = None, |
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.
Curious about this usage of None = None
.
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.
Two things changed here:
- The
None
should always be the last type within a union (according to the typing spec style guide, ruff, andflak8-pyi
), so I tend to correct that in the places where I change things in the stubs (not the best for the diffs, but I can't really help myself 😅). - You noted before that annotations are a form of documentation. And when documenting functions, you also document the defaults. Using
= ...
in stubs was done by mypy'sstubgen
(for unknown reasons), and was therefore assumed to be the way to go. But for documentation, and also for things like IDE suggestions and type-checker error messages, it's way more helpful if the actual default value is used, instead of a...
. I believe the typing spec recently also started recommending doing this.
from numpy import ( | ||
vectorize as vectorize, |
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.
Remind me of why vectorize as vectorize
makes sense. I've forgotten.
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.
In this case it doesn't, but if there's no __all__
, an import spatula from spatula
will implicitly export spatula
as a public member of that module. It's almost always better to use __all__
, but mypy's stubgen
tool used to (since last month) generate stubs with such implicit exports.
Thanks @PTUsumit, Joren. |
Fix static interpolation handling
Description
The type hints for interp assert that the return dtype is a numpy array, but when the function is called with a scaler it gives a float. This raises some mypy errors.
#27811
Changes made on _function_base_impl.pyi and test_function_base.py