All jobs

Fix "AttributeError: module 'numpy' has no attribute 'float'" on NumPy 1.24 and Later

An agent replaces np.float, np.int, np.bool and np.object with the type each call meant, runs every script on a current NumPy, and pins what the project needs.

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

What the agent does

np.float was never a NumPy type. It was a second name for Python's built-in float, kept for historical reasons along with np.int, np.bool, np.object, np.complex and np.str. NumPy 1.20 began warning about them and 1.24 removed them, so dtype=np.float now raises AttributeError: module 'numpy' has no attribute 'float', with a message that suggests float or np.float64. For np.float the two are the same 64-bit type and the rename is safe. The agent's job is to make the rename everywhere it is needed, to notice the aliases where it is not that simple, and to show the code runs.

graykode/distribution-is-all-you-need is a set of 14 standalone scripts, one per probability distribution, each drawing a chart. Four of them build their x axis with the alias: beta.py has np.arange(0, 1, 0.001, dtype=np.float), and chi-squared.py, exponential.py and gamma.py have the same call over other ranges. The agent changes each to dtype=float. np.arange with float arguments already returns float64, so the agent notes that the argument was redundant, and leaves it in place because removing it is a style choice for the maintainer.

It then looks at why the error reaches users. The repository has no requirements.txt, setup.py or pyproject.toml, so a reader who runs pip install numpy matplotlib today gets a NumPy without the aliases, and four of the scripts fail on their first loop. The other ten never touch the alias and keep working, which makes the breakage look random to someone trying scripts one at a time. The agent adds a requirements.txt with lower bounds it has tested and no upper cap, and says in the pull request that this file is new.

To prove the fix it runs all 14 scripts, including the ten it did not edit. The scripts end with plt.savefig('graph/<name>.png') and plt.show(), so the agent sets MPLBACKEND=Agg to stop show() from waiting for a window. Running them overwrites the PNG files committed under graph/, and the agent restores those with git checkout -- graph before committing so the pull request contains code only. For the four changed scripts it prints the dtype and the first and last values of x before and after, on an old NumPy where the alias still exists and on the current one, to show the arrays are identical.

Start it with one API call

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

{
  "agent": "claude",
  "input": "This project fails on NumPy 1.24 or later with \"AttributeError: module 'numpy' has no attribute 'float'\" (or 'int', 'bool', 'object', 'complex', 'str', 'long', 'unicode'). Find every use of these removed aliases in Python files, notebooks and doc examples, including uses through another import name such as numpy.float or from numpy import float. Replace each according to what the call meant: np.float with float, np.complex with complex, np.object with object, np.str with str, np.bool with bool. For np.int do not replace blindly: if the value sizes an array of indices or counts, use int; if the code relies on a fixed width, on overflow behaviour, or passes data to C, Cython or a file format, use np.int64 or np.int32 explicitly and say why. List each replacement with file, line, old and new text. Then run every script and every test in the repository on the newest NumPy, not only the files you edited; use a non-interactive matplotlib backend so plotting calls do not block. If running a script rewrites files that are committed to the repository, restore them before committing. For each edited call, show that the resulting array has the same dtype and values on NumPy 1.23 and on the newest release. If the project declares no dependencies, add a requirements file with tested lower bounds and no upper bounds, and state that it is new. Report any other error the newest NumPy or matplotlib raises, with the traceback, without fixing it unless it stops a script you edited. Open a pull request with the table, the commands and their output.",
  "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 a table of replacements. For the repository above it has four rows, one per script, each dtype=np.float to dtype=float, plus one new file listing numpy and matplotlib. Under the table is the run log: 14 script names, the exit status of each, and for the four edited ones the dtype and endpoints of x under both NumPy versions.

A reviewer checks that the diff holds no image files, since the scripts regenerate their charts as a side effect and a careless commit would carry a binary change for each chart. They read the list of other errors, which is where problems that have nothing to do with the alias appear, such as a matplotlib argument that has since been renamed. On a larger codebase the rows to read closely are the np.int ones. There the agent has written either int or a sized type, with a sentence giving the reason, and a wrong choice changes behaviour on Windows, where NumPy before 2.0 made the default integer 32 bits wide.

Run it on every weekday morning

Research groups and course repositories collect scripts like these across many folders, and nobody learns which ones still run until a student tries. Create an automation in the Project with the prompt above and a schedule trigger, for example the cron 0 6 * * 1-5 with your timezone. Each fire starts a run that installs the newest NumPy, executes everything and opens a pull request only when something fails. Add that condition to the saved prompt in a sentence, along with an instruction to add to an open pull request from an earlier fire instead of opening a second one.

For many repositories, send the request from a script instead, one call per repository with its URL in resources. A request_id that is a UUID v5 of the repository name and the date keeps a rerun of the script from starting the same repository twice in a day.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

Showing that a rename changes nothing takes two environments: one with a NumPy old enough to still have the alias, and one without it. The older NumPy needs an older Python as well, since it publishes no wheels for the newest interpreters. The agent sets up both in its sandbox, runs the same scripts in each and compares the arrays. The commands and their printed output stay in the session transcript, so the table in the pull request can be traced to what was run.

These scripts also write files and open windows. On a laptop that means charts popping up 14 times and a working tree full of modified PNGs to clean up. In the sandbox the backend is headless, the overwritten images are discarded before the commit, and a script that hangs or loops costs nothing but that run's time.

Frequently asked questions

Should I write float or np.float64?

Either. np.float was the built-in float, and NumPy maps that to float64 when it is used as a dtype, so dtype=float and dtype=np.float64 create the same array. Use np.float64 when you want the width to be visible in the code, and float when you want the shortest diff.

Why is np.int not a simple rename like np.float?

np.int was Python's int, and as a dtype that meant the platform's default integer: 64 bits on Linux and macOS, 32 bits on Windows before NumPy 2.0. Code that happened to work with one width can overflow with the other. The agent reads how each value is used before choosing int, np.int64 or np.int32.

Can I pin numpy<1.24 instead?

It works until something else in your environment needs a newer NumPy, and old releases stop getting wheels for new Python versions. The aliases were deprecated in 1.20, so the replacement code runs on every NumPy you could reasonably pin. The agent makes the code change and treats a pin as a stopgap only when the alias is inside a dependency you cannot upgrade yet.

The error is raised inside a library, not my script. What then?

The traceback names the package. The agent checks the installed version against the release that removed the alias use, upgrades the package if your code runs with the newer one, and reports the versions before and after. It does not edit files under site-packages.

I thought np.bool came back?

It did, in NumPy 2.0, as the name of NumPy's own boolean type. That leaves a gap: on 1.24 through 1.26 np.bool raises this error. If your project supports those versions, bool as a dtype works on all of them, which is what the agent writes.

More NumPy jobs

More jobs like this: Fix a bug from an alert

Keep reading