TYP: Typing fixes and improvements related to scipy.sparse
#62218
+34
−23
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The promised sequel of #62211.
With this, all scipy-related squigglies that show up when
scipy-stubs
is installed, are fixed. There's still one issue that remains incore.missing
that's not related to scipy, but rather to (the lack of) a mypy--allow-redefinition
(or--allow-redefinition-new
) flag.The
scipy.sparse.spmatrix
magic that you're seeing works around the fact that thespmatrix
class has a very limited set of methods and attributes. For example, it has notocsc()
method, so it's invalid to dodata.tocsc()
.The
Protocol
that I've replaced it with here, can be thought of as the proposition "someT
s.t.T.shape: tuple[int, int]
andT.tocsc() -> csc_{array,matrix}". This *structural* type (i.e. the protocol) avoids having to write out the union of all 7
{}_matrix*nominal* types. I also took the liberty of sneaking in some unofficial support for the (non-legacy)
scipy.sparse._arraytypes, which in this case are compatible with the legacy
_matrix` types (in the duck-typing sense). I understand if that's considered out-of-scope here, and wouldn't mind reverting in that case.doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.