Skip to content

Conversation

PTUsumit
Copy link
Contributor

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

@charris charris changed the title fixing static interp TYP: fix "np.interp" typing Nov 27, 2024
@charris
Copy link
Member

charris commented Nov 27, 2024

@jorenham ping.

@jorenham jorenham assigned jorenham and unassigned jorenham Nov 27, 2024
@jorenham jorenham self-requested a review November 27, 2024 14:42
@jorenham jorenham changed the title TYP: fix "np.interp" typing TYP: fix np.interp typing Nov 27, 2024
Copy link
Member

@jorenham jorenham left a 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 💪🏻.

@jorenham jorenham changed the title TYP: fix np.interp typing TYP: Fix np.interp signature for scalar types Nov 27, 2024
@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Nov 27, 2024
@jorenham jorenham linked an issue Nov 27, 2024 that may be closed by this pull request
@charris charris added this to the 2.2.1 release milestone Dec 9, 2024
@charris charris modified the milestones: 2.2.1 release, 2.2.2 release Dec 21, 2024
@charris charris removed this from the 2.2.2 release milestone Dec 30, 2024
@jorenham jorenham self-assigned this Jan 8, 2025
Co-authored-by: PTUsumit <2301109104@ptuniv.edu.in>
@jorenham
Copy link
Member

jorenham commented Jan 9, 2025

@jorenham
Copy link
Member

jorenham commented Jan 9, 2025

ready for review @charris

left: None | _FloatLike_co = ...,
right: None | _FloatLike_co = ...,
period: None | _FloatLike_co = ...,
left: _FloatLike_co | 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.

Curious about this usage of None = None.

Copy link
Member

@jorenham jorenham Jan 9, 2025

Choose a reason for hiding this comment

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

Two things changed here:

  1. The None should always be the last type within a union (according to the typing spec style guide, ruff, and flak8-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 😅).
  2. 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's stubgen (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,
Copy link
Member

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.

Copy link
Member

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.

@charris charris merged commit 7f93cf4 into numpy:main Jan 9, 2025
69 checks passed
@charris
Copy link
Member

charris commented Jan 9, 2025

Thanks @PTUsumit, Joren.

@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

TYP: Wrong type hint for interp
3 participants