All jobs

Upgrade Python Code to NumPy 2 (np.product, np.asfarray and np.float Removed)

Yes. An agent finds the NumPy aliases version 2 removed, fixes them, reruns tests and doctests on NumPy 2, and opens a pull request that still works on 1.x.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

NumPy 2.0 removed a long list of aliases and old names, so code that ran for years now fails with AttributeError: module 'numpy' has no attribute ... on a fresh install. Most projects reach that state without choosing to. In a library like lgalke/vec4ir, setup.py lists a bare numpy among its requirements, so a new environment gets NumPy 2 whether the code is ready or not. The agent starts by measuring. It runs Ruff's NumPy 2 rule, ruff check --select NPY201, which here reports three places: np.product in vec4ir/eqlm.py and vec4ir/query_expansion.py, both with an automatic fix to np.prod, and np.asfarray in vec4ir/rank_metrics.py, which Ruff flags but does not rewrite.

The linter is not the whole inventory, and the agent checks what it missed. Ruff also reports that vec4ir/bm25.py cannot be parsed, because one line is indented with a tab among spaces, so nothing inside that file was checked. A plain search finds np.issubdtype(X.dtype, np.float) there, an alias NumPy removed back in 1.24. The package's __init__.py does not import that module, which is why nobody noticed. The agent fixes the indentation and the alias and says in the pull request that the file could not have been imported on Python 3 before.

It applies the two automatic fixes, replaces np.asfarray(r) with np.asarray(r, dtype=float), and then runs the code on NumPy 2, because removed names are only the first kind of breakage. rank_metrics.py documents its functions with doctests, and after the alias fix they still fail, now on formatting: NumPy 2 prints a scalar as np.float64(3.0) where the docstring expects 3.0, and np.True_ where it expects True. The values are right and the text differs. The agent picks one remedy and names it, here returning built-in floats from the metric functions, which also suits callers that serialise the results.

Last, it looks beyond the project's own code. NumPy 2 changed its binary interface, so compiled dependencies must be releases built for it; the agent installs the declared requirements fresh and reports any that refuse to import against NumPy 2. In this repository that step surfaces a separate problem first: the requirements list includes sklearn, a deprecated name on PyPI whose install now stops with an error telling you to use scikit-learn. The agent removes the duplicate entry, since scikit-learn is already listed, and keeps the change apart from the NumPy edits. Every replacement it makes also exists in NumPy 1.x, so it adds no version cap.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Make this project work on NumPy 2 without dropping NumPy 1.x. First report which NumPy version a fresh install of the project's declared requirements selects today, and whether that install succeeds at all. Run ruff check --select NPY201 over the repository and list every finding. Then look for what the linter cannot see: files that fail to parse, removed aliases such as np.float, np.int, np.bool and np.object, names built dynamically with getattr, and NumPy calls inside notebooks or doc examples. Apply fixes that are valid on both NumPy 1.x and 2.x (for example np.prod for np.product, np.asarray with a dtype for np.asfarray, np.nan for np.NaN, np.inf for np.Inf, np.trapezoid only behind a version check). In an environment with the newest NumPy 2, run the test suite and any doctests, and fix what fails, separating three causes in your report: removed names, changed scalar formatting in printed or asserted output, and changed type promotion in arithmetic. Check that each compiled dependency (for example pandas, scipy, scikit-learn, gensim) installs in a version built for NumPy 2 and imports cleanly; report any that does not and the minimum version that does. Run the tests once more on the newest NumPy 1.x to confirm nothing regressed. Do not add an upper bound on NumPy unless something cannot be fixed, and say why if you do. Keep unrelated repairs you had to make in separate commits. Open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request with small code changes and a description that sorts them by cause. For vec4ir the NumPy edits are four lines in four files, plus the doctest remedy in rank_metrics.py. Unrelated repairs, the tab in bm25.py and the sklearn requirement, sit in their own commits so they can be reviewed or dropped independently.

The description reports two test runs, one on NumPy 2 and one on the newest 1.x, with the commands used. It lists each compiled dependency with the version that was installed against NumPy 2. Where the agent found nothing in a category, such as type promotion changes, it says it looked and how, so an empty section is information and not an omission.

A reviewer should read the doctest decision most carefully. Converting return values to built-in floats is a small behaviour change for callers who relied on getting a NumPy scalar. The alternative, updating the expected output, keeps behaviour and ties the docs to NumPy 2's formatting. The pull request states which was chosen and why.

Run it on every Monday morning

After the upgrade, the useful recurring check is whether the project still passes on whatever NumPy released last. Add an automation to the repository's Project with a schedule trigger, a cron of 0 6 * * 1 and your timezone, and save a shorter prompt: install the project with the newest NumPy, run the tests and doctests, and open a pull request only if something fails, with the failure and the fix. On a week with nothing to report the run ends with a one-line answer.

For a group of repositories that all need the first migration, start those from a script, one POST /o/{org}/sessions each with the body above and a request_id that is a UUID v5 of the repository name.

The use case this job follows, with its automation and trigger: Upgrade dependencies weekly.

Why run it on Rush?

The reliable test for NumPy 2 readiness is a clean environment with NumPy 2 in it, and that is the thing a working developer machine tends not to have. An existing virtualenv keeps the NumPy it was built with, and the code keeps passing there. The agent's sandbox starts empty, installs what the manifest declares, and meets the same errors a new user or a rebuilt container would, including ones that have nothing to do with NumPy and block the install before it gets that far.

Running both major versions in one job is the other half. A fix that works on 2.x and breaks 1.x is easy to write, for instance by using a name that only exists in the new release. The agent runs the suite on each and keeps the output in the session transcript, so the claim that the project supports both is backed by two runs a reviewer can read.

Frequently asked questions

How do I find everything NumPy 2 removed in my code?

Run ruff check --select NPY201. It knows the removed and renamed members and can fix many of them with --fix. It only sees files it can parse and names written literally, so follow it with a search for old aliases and a real test run on NumPy 2.

What replaces np.asfarray?

np.asarray(x, dtype=float). NumPy 2's own error message for np.asfarray says to use np.asarray with a proper dtype. The replacement works on NumPy 1.x as well, so no version check is needed.

Why do my doctests fail on NumPy 2 when the numbers are correct?

NumPy 2 changed how scalars are displayed: a float64 prints as np.float64(3.0) in a doctest, not 3.0. Either return built-in Python numbers from your functions, update the expected output, or print values through float() in the examples.

Do I need to pin numpy below 2 while I migrate?

A temporary numpy<2 pin is a reasonable stopgap for an application. For a library it pushes the problem onto users whose other packages require NumPy 2. The fixes for removed names are usually a handful of lines, so doing them is often quicker than managing the pin.

Will the fixed code still run on NumPy 1.x?

Yes, when the replacements are chosen for that. np.prod, np.asarray, np.nan and built-in float exist in every supported NumPy. The agent avoids names that only exist in 2.x unless they are behind a version check, and runs the tests on both to confirm.

More NumPy jobs

More jobs like this: Upgrade dependencies weekly

Keep reading