-
-
Notifications
You must be signed in to change notification settings - Fork 16
🏷️ sparse
: overhauled stubs for diags_array
, eye_array
, and their matrix equivalents
#733
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
🏷️ sparse
: overhauled stubs for diags_array
, eye_array
, and their matrix equivalents
#733
Conversation
That |
1770af8
to
df6a50d
Compare
Ok, good to hear that you share the same sentiment. |
Ah, one more thing. I struggled with the following problem: I wanted to support an input of type Any idea on how to elegantly solve this issue? |
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.
this is pretty awesome 🤯
I really wish we could do something like def f[T: str](x: T) -> Result[T]: ...
with
case type Result["a"] = A
case type Result["b"] = B
...
but the typing committee didn't seem very enthusiastic when I and others have proposed similar ideas like this before (even though it could directly replace overloads in a DRY and easier-to-specify way) 🤷🏻
I think the issue is the invariance of |
If you think it can help other VSCode users (like me) as well, feel free to open a separate PR for it. |
I used |
I see that you merged the change to |
I'll open a separate PR that you can rebase onto after it's merged |
#737 has been merged :) |
44c98b8
to
215b980
Compare
I think I found a way to differentiate between float-type and complex-type sequences. That being said, I get other errors that I can't really explain and are leading me to believe there might be a bug in basedpyright, but I want to check with you to be certain. Maybe some context: |
I forgot to mention it earlier, but you can use the
If you can make it reproducible without scipy-stubs, then https://basedpyright.com/ is a nice way. Otherwise, you could push it to a new branch on your fork.
Coincidentally, I just ran into a very strange case of pyright-specific "spooky action at a distance" myself: #741 (comment) |
I want to try to enable the input of mixed arrays like
I'll try that tomorrow :)
Seems different, since it's happening in the same file as the tests are in, but you never know... |
de18dda
to
df1d125
Compare
Another issue: |
I fixed it by writing the type out, instead of the alias. |
As can be seen from the commit message, the Python list typings are causing too many headaches. I noticed different behavior between All tests pass now, but the local issue with |
I'll have another look at this tomorrow, sorry for the delay |
I think so too |
I have no clue why this error is now triggered... Why is overload 2 not checked, which has |
That being said, I restructured all functions in |
I'll have to work on the other comments later, since I'm going on vacation! |
So the pyright false positives weren't there before the big shuffle?
Ah good to know; I'll go ahead and release 1.16.0.3 then. Have fun! |
The See #648 and scipy/scipy#23216 for the nasty details surrounding it |
Nope... But they seem to be very sensitive to slight tweaks.
Thanks! I kinda figured you were waiting for this PR to release the next version haha, sorry for the delay. On the other hand, I still want to fix the other functions in |
It must be a quantum bug: I've seen it causing spooky action at a distance before, and this sounds like a case of decoherence. So putting it in the (dilution) fridge sounds like a good idea to me 👌🏻. |
Then I'd prefer that in a PR, so that it shows up nicely in the release notes. It'll make it easier to review as well 😇. |
Turns out I'm just an idiot (accidentally removed some overloads during refactoring + added some |
That's quite impressive then
So then I shouldn't merge this yet :p ? |
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.
Pull Request Overview
This pull request significantly overhauls the type stubs for sparse matrix/array construction functions in SciPy's sparse
module. The changes improve type safety by providing more precise return types based on the format
parameter and enhance support for different sparse formats.
Key changes include:
- Comprehensive overload definitions for all sparse formats ("bsr", "coo", "csc", "csr", "dia", "dok", "lil") across multiple functions
- Enhanced type annotations for
diags_array
to better support Python sequences with complex number handling - Systematic refactoring of function signatures to use consistent type aliases and parameter patterns
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
scipy-stubs/sparse/_construct.pyi |
Major overhaul of function stubs with comprehensive format-specific overloads and improved type aliases |
tests/sparse/test_construct.pyi |
Extensive test coverage additions for all sparse formats and edge cases, including complex number handling |
scipy-stubs/sparse/_construct.pyi
Outdated
_Numeric: TypeAlias = np.bool_ | npc.number | ||
|
||
_T = TypeVar("_T") | ||
_SCT = TypeVar("_SCT", bound=_Numeric, default=Any) | ||
_SCT0 = TypeVar("_SCT0", bound=_Numeric) | ||
_ShapeT = TypeVar("_ShapeT", bound=tuple[int, *tuple[int, ...]], default=tuple[Any, ...]) | ||
|
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.
The _Numeric
type alias is redefined when it was already defined below. The order should be consistent with the existing codebase pattern.
_Numeric: TypeAlias = np.bool_ | npc.number | |
_T = TypeVar("_T") | |
_SCT = TypeVar("_SCT", bound=_Numeric, default=Any) | |
_SCT0 = TypeVar("_SCT0", bound=_Numeric) | |
_ShapeT = TypeVar("_ShapeT", bound=tuple[int, *tuple[int, ...]], default=tuple[Any, ...]) | |
_T = TypeVar("_T") | |
_SCT = TypeVar("_SCT", bound=_Numeric, default=Any) | |
_SCT0 = TypeVar("_SCT0", bound=_Numeric) | |
_ShapeT = TypeVar("_ShapeT", bound=tuple[int, *tuple[int, ...]], default=tuple[Any, ...]) | |
_Numeric: TypeAlias = np.bool_ | npc.number |
Copilot uses AI. Check for mistakes.
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.
What does this even mean @jorenham? Seems counterintuitive to put a variable under its use cases.
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 .pyi
that's not a problem; forward references are fine
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 kinda see how it can be cleaner to keep the typevars and type aliases separated
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.
feel free to ignore it though
You can! I don't know how pressing these unit tests are. Maybe it can be marked as a TODO with an issue? |
Mr. Copilot found a missing comma! The robot did forget how to count (there are only 56 overloads for |
An inline
Haha, that's exactly the thing you'd expect AI's to be good at; counting |
Anyway, let me know when I can press the button |
I added TODOs and moved |
Thanks Jul! |
Hello!
I improved support for the different sparse formats in
diags
,spdiags
,diags_array
,identity
,eye
, andeye_array
.Additionally, I made the stubs of the
diags_array
function more versatile, so that it supports Python sequences a little better (see the extra tests). To achieve this I needed theoptype.numpy.AnyFloat64DType
, but without theNone
option, which I reimplemented in the_construct.pyi
file.Is there already such a type? Alternatively, is it too late to remove
None
and alter all uses to include| None
? I saw that this is the case in most places already.Let me know!
Kind regards,
Jul