-
-
Notifications
You must be signed in to change notification settings - Fork 24
🏷️ sparse: improved init stubs of sparse arrays with shape as positional argument.
#704
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
Changes from 1 commit
2fe7a0d
0ec1442
b3d1ee0
f2d57e8
c34349a
7796ded
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -103,10 +103,53 @@ class bsr_array(_bsr_base[_ScalarT_co], sparray[_ScalarT_co, tuple[int, int]], G | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self: bsr_array[np.float64], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| arg1: ToShape2D, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| shape: None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| shape: ToShape2D | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dtype: None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| copy: bool = False, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| blocksize: tuple[int, int] | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| *, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| maxprint: int | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> None: ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @overload # 2-d shape-like, dtype: type[bool] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self: bsr_array[np.bool_], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| arg1: ToShape2D, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| shape: ToShape2D | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dtype: onp.AnyBoolDType | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| copy: bool = False, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| *, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| maxprint: int | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> None: ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @overload # 2-d shape-like, dtype: type[bool] | |
| def __init__( | |
| self: bsr_array[np.bool_], | |
| /, | |
| arg1: ToShape2D, | |
| shape: ToShape2D | None = None, | |
| dtype: onp.AnyBoolDType | None = None, | |
| copy: bool = False, | |
| *, | |
| maxprint: int | None = None, | |
| ) -> None: ... | |
| @overload # 2-d shape-like, dtype: bool-like (keyword) | |
| def __init__( | |
| self: bsr_array[np.bool_], | |
| /, | |
| arg1: ToShape2D, | |
| shape: ToShape2D | None = None, | |
| *, | |
| dtype: onp.AnyBoolDType, | |
| copy: bool = False, | |
| maxprint: int | None = None, | |
| ) -> None: ... |
and if we also cover the second form, that'd be
| @overload # 2-d shape-like, dtype: type[bool] | |
| def __init__( | |
| self: bsr_array[np.bool_], | |
| /, | |
| arg1: ToShape2D, | |
| shape: ToShape2D | None = None, | |
| dtype: onp.AnyBoolDType | None = None, | |
| copy: bool = False, | |
| *, | |
| maxprint: int | None = None, | |
| ) -> None: ... | |
| @overload # 2-d shape-like, dtype: bool-like (keyword) | |
| def __init__( | |
| self: bsr_array[np.bool_], | |
| /, | |
| arg1: ToShape2D, | |
| shape: ToShape2D | None = None, | |
| *, | |
| dtype: onp.AnyBoolDType, | |
| copy: bool = False, | |
| maxprint: int | None = None, | |
| ) -> None: ... | |
| @overload # 2-d shape-like, dtype: bool-like (positional) | |
| def __init__( | |
| self: bsr_array[np.bool_], | |
| /, | |
| arg1: ToShape2D, | |
| shape: ToShape2D | None, | |
| dtype: onp.AnyBoolDType, | |
| copy: bool = False, | |
| maxprint: int | None = None, | |
| ) -> None: ... |
I'm fine with either option, as long as you consistently apply it for all 3 dtypes, 7 sparse formats, and >=1 shape types (so >21 overloads).
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 understand that the | None option shouldn't be there for the types other than np.float64, but in practice these overloads will never be picked, since now the np.float64 one is at the top and will be opted for first.
At the same time, the current implementation (with | None for each dtype) covers all cases.
So, I think it's at most a bit "hacky" to put | None after each dtype, but it does the job and avoids 21+ extra overloads.
What do you think? Could be I missed something :)
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.
but in practice these overloads will never be picked,
that is, unless you pass a dtype that is actually annotated as e.g. type[bool] | None :)
def create_sparse_array(dtype: bool | None = None):
return scipy.sparse.bsr_array((2, 2), dtype=dtype)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.
Aha, I see.. I'll look into the overloads to resolve this then.
Outdated
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.
| @overload # 2-d shape-like, dtype: type[float] | |
| def __init__( | |
| self: bsr_array[np.float64], | |
| /, | |
| arg1: ToShape2D, | |
| shape: ToShape2D | None = None, | |
| dtype: onp.AnyFloat64DType | None = None, | |
| copy: bool = False, | |
| *, | |
| maxprint: int | None = 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.
This overload could be merged with the one at line 134:
I believe that the same can be said for the other sparse formats.
Uh oh!
There was an error while loading. Please reload this page.
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.
So, I included a default overload with just the
Noneoption to cover the base case (where the dtype isnp.float64). If I remove and merge it with the specificnp.float64overload, another overload (np.bool_in this case) gets selected first when I do not specify a dtype.Would you rather I put the
np.float64one at the top of the overloads, so that it gets selected first, or I keep the current overloads?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.
Ah, I think you are alluding to the first option in the suggestions below, so I'll stick to that one!