All jobs

Fix "ImportError: cannot import name 'gcd' from 'fractions'" in Python 3.9+

Yes. An agent swaps fractions.gcd for math.gcd, checks the two cases where they differ, and adds a test so the broken file is covered from now on.

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

What the agent does

fractions.gcd was deprecated when math.gcd arrived and Python 3.9 removed it. The import fails at once, so the affected file cannot even start. What makes it interesting is where it hides. Zulko/gizeh is a maintained vector graphics library: its pyproject.toml lists classifiers through Python 3.12, its workflow tests 3.9 to 3.13, and its ruff configuration enables the UP upgrade rules. Line 8 of examples/roses.py is still from fractions import gcd. The workflow runs pytest -q, the tests never import the examples, and ruff reports no finding for that file under the project's own configuration. Every check passes while a script the README sends newcomers to cannot run.

The agent reproduces both sides. It installs the package on Python 3.8, where the example still works, runs examples/roses.py, and keeps the roses.png it writes together with the warning printed on line 17: DeprecationWarning: fractions.gcd() is deprecated. Use math.gcd() instead. On a current Python the same command stops on line 8 with the error in the title. It also runs the test suite on the current Python and records that it passes, which is the evidence that nothing in CI exercises this file.

The fix is the one the warning names, from math import gcd, and the agent checks the two ways the functions differ before trusting it. fractions.gcd(4, -6) returned -2, taking the sign of its second argument, and math.gcd returns 2. fractions.gcd also accepted floats and Fraction values, and math.gcd raises TypeError for a float. So it reads the call site. Here gcd(n, d) receives two positive integers from range loops, and neither difference can occur. Where a caller passes negatives, floats or fractions, the agent says so and adapts the call instead of swapping the import.

Then it closes the gap that let this through. The repository already stores the expected picture: tests/samples/roses.png sits beside four other samples, and tests/test_samples.py has tests for those four and none for the rose. The agent runs the fixed example on the current Python, confirms the image matches both the 3.8 output and the stored sample pixel for pixel, and adds a test_roses in the style of its neighbours. It also runs examples/_run_all_examples.py, which executes every example in the folder, to see whether any other one fails.

Start it with one API call

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

{
  "agent": "claude",
  "input": "This project fails on Python 3.9 or later with \"ImportError: cannot import name 'gcd' from 'fractions'\". Find every import or use of fractions.gcd in the repository, including examples, scripts, docs and notebooks that the test suite does not import. Reproduce the failure on a current Python, and capture a baseline by running the affected code on Python 3.8, where the function still exists; save its output. Replace fractions.gcd with math.gcd. For each call site, check the argument types and signs: fractions.gcd took the sign of its second argument and accepted floats and Fraction objects, while math.gcd always returns a non-negative int and rejects non-integers. If any call can receive a negative number, a float or a Fraction, keep the old behaviour explicitly and explain how. Run the fixed code on the current Python and compare its output with the baseline. Then find out why the project's checks did not catch this: say whether the test suite, the linter configuration and CI cover the affected file, and add a test that would have failed, using any expected output the repository already stores. Run every example or script in the same folder and report others that fail, without fixing unrelated ones. Open a pull request with 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 one-line change in the example and a new test next to the existing sample tests. The description gives the reproduction on both interpreters, the warning text from 3.8 and the traceback from the current version, and states the comparison: the fixed example's image equals the old interpreter's image and the sample already stored in the repository.

It includes a short section on the two behavioural differences between the functions and the reason neither applies at this call site, with the argument ranges quoted from the code. A reviewer does not have to remember that the old function could return a negative number, because the description says it and shows the check.

The last section answers why this was not caught: which command CI runs, that the examples folder is outside it, and that the linter stayed silent. It lists what the example runner reported for the other examples in the folder. The reviewer confirms the new test fails when the import is reverted, and decides whether running the example script should become a CI step, which the agent proposes and does not add on its own.

Run it on every error in your terminal

You usually meet this error while doing something else: trying a library's example, or running an old script on a new machine. Hand it off where you are. The Rush API is also an MCP server at https://api.phoenix.so/mcp, and a coding agent in your editor or terminal that is connected to it with your API key can start this run with create_session, passing the prompt above and your repository. The call returns at once, and wait_for_sessions picks up the answer when you want it. From a script, the same thing is one POST /o/{org}/sessions.

The run does not depend on your laptop staying open. When the turn ends the sandbox parks with its disk and memory, and a follow-up through send_session_message, or POST …/sessions/{id}/messages, wakes it where it left off. That is how you answer the question the pull request raises, such as asking it to add the example runner to CI after all, without a new run reinstalling two interpreters first. A parked sandbox is kept for 7 idle days.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

The fix is one line, and the reason to hand it off anyway is everything around the line: an old interpreter for the baseline, a system graphics library the package needs, a pixel comparison, and a look at what CI covers. That is real setup on a laptop for a problem you hit by accident. In a Rush sandbox the agent installs what the project's own workflow installs, runs both versions, and keeps every command with its output, while you carry on with what you were doing.

It also returns more than the fix. A person patching an example in passing changes the import and moves on, and the next removed function in an untested file is found the same way, by a user. The run comes back with the test that would have caught it and a plain statement of which files no check ever executes, so the maintainer can decide once whether examples deserve a CI step.

Frequently asked questions

Is math.gcd a drop-in replacement for fractions.gcd?

For non-negative integers, yes. It differs in two cases: the result is never negative, where the old function took the sign of its second argument, and it accepts only integers. Code that computed a greatest common divisor of floats or Fraction objects needs a different approach, and the agent flags those call sites.

Why didn't our linter flag the import?

Upgrade rules cover a list of known rewrites, and that list is not exhaustive. On gizeh, ruff with the project's configuration, which enables the UP rules, reports nothing for examples/roses.py. An import of a name that no longer exists is only certain to surface when something executes the file.

Does the fix keep working on older Python versions?

math.gcd has existed since Python 3.5, so any project that supports Python 3.5 or later can use it unconditionally. No try/except ImportError fallback is needed unless you still support Python 2.

Can math.gcd take more than two arguments?

From Python 3.9 it accepts any number of integers, and math.lcm was added in the same release. On 3.8 and older it takes exactly two. If your project still supports those versions, keep reducing over a list with functools.reduce, which the agent does when it sees a wider version range in the packaging metadata.

More jobs like this: Hand off a long task

Keep reading