Skip to content
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

signal.windows: Add type stubs for _windows.pyi. #153

Merged
merged 13 commits into from
Nov 3, 2024

Conversation

pavyamsiri
Copy link
Contributor

Contributes to completing #99.

This PR adds type stubs for the module scipy.signal.windows._windows.

The function overloads are bit dodgy though and I could have maybe used AnyInt instead of int for some of the arguments.

The reason I didn't use AnyInt as of now is because the input arguments M go through a private function _len_guards which don't allow np.bool_ as a valid input argument type I believe, as in it just crashes if passed such an arg.

@jorenham jorenham self-requested a review November 2, 2024 07:52
@jorenham
Copy link
Owner

jorenham commented Nov 2, 2024

There are some merge conflicts

Copy link
Owner

@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 this; it's good to see some shape-typing in action 🎉.

I noticed that the sym parameters are currently typed as a bool, just like the docs say it should be. But I believe that in practice, it could be anything that can be used within an if statements, or passed as argument to a bool.
So in similar cases like these, I've recently been using optype.CanBool as annotation (implementation). And while theoretically any object should be acceptable (object doesn't implement __bool__), I thought that annotating them as _: CanBool would be a more informative than _: object, given the name and all 🤷🏻.
In cases where I had to overload on "truthy" and "falsy" values, I usually went for Literal[0, False] and Literal[1, True], as there seems to be a trend within the scipy codebase of using 0 instead of False, and 1 instead of True.

scipy-stubs/signal/windows/_windows.pyi Outdated Show resolved Hide resolved
scipy-stubs/signal/windows/_windows.pyi Outdated Show resolved Hide resolved
scipy-stubs/signal/windows/_windows.pyi Outdated Show resolved Hide resolved
scipy-stubs/signal/windows/_windows.pyi Outdated Show resolved Hide resolved
scipy-stubs/signal/windows/_windows.pyi Outdated Show resolved Hide resolved
scipy-stubs/signal/windows/_windows.pyi Outdated Show resolved Hide resolved
scipy-stubs/signal/windows/_windows.pyi Outdated Show resolved Hide resolved
scipy-stubs/signal/windows/_windows.pyi Outdated Show resolved Hide resolved
scipy-stubs/signal/windows/_windows.pyi Outdated Show resolved Hide resolved
scipy-stubs/signal/windows/_windows.pyi Outdated Show resolved Hide resolved
@pavyamsiri
Copy link
Contributor Author

There are some merge conflicts

I am actually really confused about this. My point of my commit was to change this file so of course it would conflict, its what I am trying to change!

Must be a git quirk or I just forgot something about how git works.

@jorenham
Copy link
Owner

jorenham commented Nov 2, 2024

The last change made to _windows.pyi was two weeks ago, so I suspect you forgot to update your fork before starting this PR then

@pavyamsiri
Copy link
Contributor Author

The last change made to _windows.pyi was two weeks ago, so I suspect you forgot to update your fork before starting this PR then

Okay that makes sense. I think I messed up my branch creation command because I meant to branch off the upstream master using something like git switch -c my_branch upstream-master. I thought I had updated my local copy of the upstream master but I guess I didn't.

@jorenham
Copy link
Owner

jorenham commented Nov 2, 2024

I often mess it up with git as well; I mostly rely on some vscode plugins for that kinda stuff haha

@pavyamsiri
Copy link
Contributor Author

I think I addressed everything except not so sure about the overload semantics for the dpss function.

scipy-stubs/signal/windows/_windows.pyi Outdated Show resolved Hide resolved
scipy-stubs/signal/windows/_windows.pyi Outdated Show resolved Hide resolved
@jorenham
Copy link
Owner

jorenham commented Nov 2, 2024

It might be helpful to write a simple type-test for dpss, so that you can see the output in each of the main ways that you could call it.
See https://github.com/jorenham/scipy-stubs/tree/master/tests

`dpss`.

In `_windows.pyi` and the `dpss` function. Allow callers to use
positional or keyword argument passing for `Kmax` and `return_ratios`.
This function has a few different overloads so it is nice to have tests
for them.
@pavyamsiri
Copy link
Contributor Author

@jorenham Thanks! The explanation on overloads made a lot of sense. Hopefully the overloads make sense now.

Added some tests for dpss as well, like you suggested.

@jorenham jorenham self-requested a review November 3, 2024 06:03
Copy link
Owner

@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.

apart from one suggestion, this looks about ready to merge

scipy-stubs/signal/windows/_windows.pyi Outdated Show resolved Hide resolved
In `_windows.pyi`. This is because `ndarray` is not a `Sequence`.
Comment on lines 40 to 45
_Weights: TypeAlias = (
Sequence[AnyReal]
| np.ndarray[tuple[int], np.dtype[np.floating[Any]]]
| np.ndarray[tuple[int], np.dtype[np.integer[Any]]]
| np.ndarray[tuple[int], np.dtype[np.bool_]]
)
Copy link
Owner

Choose a reason for hiding this comment

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

As much as I am a fan of shape-typed arrays, I'm afraid that we can't require specific shapes for input (for now), because numpy has very little support for it.
So for example even simple things like np.array([1]) currently return np.ndarray[tuple[int, ...]] (although I believe that I fixed that, but that'll be in the upcoming 2.2.0 release, and we currently have to support numpy>=1.24).

tldr; for output it's perfectly fine to use shape-typing, but for input it's too restrictive at the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay got it I will change it.

@jorenham jorenham merged commit 818150c into jorenham:master Nov 3, 2024
2 checks passed
@jorenham
Copy link
Owner

jorenham commented Nov 3, 2024

thanks @pavyamsiri !

@pavyamsiri pavyamsiri deleted the improve/windows branch November 3, 2024 23:43
@jorenham jorenham added this to the 1.14.1.3 milestone Nov 4, 2024
@jorenham jorenham added the stubs: improvement Improve or refactor existing annotations label Nov 25, 2024
@jorenham jorenham mentioned this pull request Dec 8, 2024
21 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scipy.signal stubs: improvement Improve or refactor existing annotations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants