Skip to content

style: add NPY lints to prevent use of legacy np.random - #4087

Merged
flying-sheep merged 3 commits into
mainfrom
np-lints
Apr 24, 2026
Merged

style: add NPY lints to prevent use of legacy np.random#4087
flying-sheep merged 3 commits into
mainfrom
np-lints

Conversation

@flying-sheep

@flying-sheep flying-sheep commented Apr 24, 2026

Copy link
Copy Markdown
Member

One more for scverse/anndata#1241

Basically to move to np.random.Generator:

  • rand and randn are almost aliases for random and standard_normal, except that they have a worse API (f(*shape) instead of the more common f(shape) or f(..., *, size=shape))
  • randint is renamed to integers
  • the rest has the same name

There were a few leftover np.random.seed calls:

  • some in the tests could just be removed, they did nothing?
  • one was left in the tests (test_notebooks.py::test_pbmc) where it basically tests that the backwards compat defaults work as expected
  • I didn’t port some of the old sim code last time, now I did. I wonder if anyone uses it.
  • Closes #
  • Tests included or not required because:

@flying-sheep flying-sheep changed the title chore: add NPY lints to prevent new code using legacy random style: add NPY lints to prevent new code using legacy random Apr 24, 2026
@flying-sheep flying-sheep added this to the 1.13.0 milestone Apr 24, 2026
@flying-sheep flying-sheep changed the title style: add NPY lints to prevent new code using legacy random style: add NPY lints to prevent use of legacy np.random Apr 24, 2026
@flying-sheep
flying-sheep requested a review from ilan-gold April 24, 2026 14:03
@codecov

codecov Bot commented Apr 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 65.90909% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.23%. Comparing base (44cfc6e) to head (0fcf8ce).
⚠️ Report is 69 commits behind head on main.

Files with missing lines Patch % Lines
src/scanpy/tools/_sim.py 64.28% 10 Missing ⚠️
src/scanpy/_utils/random.py 60.00% 2 Missing ⚠️
src/scanpy/plotting/_tools/__init__.py 66.66% 2 Missing ⚠️
src/scanpy/readwrite.py 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4087      +/-   ##
==========================================
+ Coverage   78.59%   79.23%   +0.63%     
==========================================
  Files         118      119       +1     
  Lines       12745    12669      -76     
==========================================
+ Hits        10017    10038      +21     
+ Misses       2728     2631      -97     
Flag Coverage Δ
hatch-test.low-vers 78.53% <65.90%> (+0.63%) ⬆️
hatch-test.pre 79.10% <65.90%> (+0.62%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/scanpy/external/tl/_phenograph.py 25.64% <ø> (ø)
src/scanpy/readwrite.py 81.23% <80.00%> (-0.06%) ⬇️
src/scanpy/_utils/random.py 92.45% <60.00%> (-0.82%) ⬇️
src/scanpy/plotting/_tools/__init__.py 76.24% <66.66%> (+0.06%) ⬆️
src/scanpy/tools/_sim.py 63.44% <64.28%> (+10.66%) ⬆️

... and 4 files with indirect coverage changes

@flying-sheep flying-sheep self-assigned this Apr 24, 2026

@ilan-gold ilan-gold left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Modulo the failing CI, this seems reasonable

@flying-sheep

flying-sheep commented Apr 24, 2026

Copy link
Copy Markdown
Member Author

That was a bug in _delegate! The only method that was really renamed between RandomState and Generator is randintintegers, and I messed up doing that replacement properly. (the more sensible replacements for rand and randn already exist on RandomState)

Co-authored-by: Copilot <copilot@github.com>
@flying-sheep
flying-sheep merged commit ea8e481 into main Apr 24, 2026
13 of 14 checks passed
@flying-sheep
flying-sheep deleted the np-lints branch April 24, 2026 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants