Skip to content

Conversation

jorenham
Copy link
Contributor

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 in core.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 the spmatrix class has a very limited set of methods and attributes. For example, it has no tocsc() method, so it's invalid to dodata.tocsc().
The Protocol that I've replaced it with here, can be thought of as the proposition "some T s.t. T.shape: tuple[int, int] and T.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.


  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

@jorenham
Copy link
Contributor Author

The CI failure seems unrelated

@mroeschke mroeschke added the Typing type annotations, mypy/pyright type checking label Sep 10, 2025
Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

Thanks @jorenham LGTM

@mroeschke mroeschke added this to the 3.0 milestone Sep 11, 2025
@mroeschke mroeschke merged commit 419d5f1 into pandas-dev:main Sep 11, 2025
42 checks passed
@mroeschke
Copy link
Member

Thanks @jorenham

@jorenham jorenham deleted the fix-scipy-sparse-annotations branch September 11, 2025 16:20
@jorenham jorenham mentioned this pull request Sep 12, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Typing type annotations, mypy/pyright type checking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants