All jobs

Fix "AttributeError: module 'inspect' has no attribute 'getargspec'" on Python 3.11

Yes. An agent finds each getargspec and formatargspec call, ports it to an API Python 3.11 still has, proves the path that raised, and opens a pull request.

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

What the agent does

Python 3.11 removed inspect.getargspec, which had been deprecated since Python 3.0. The error rarely shows at import time, because the call usually sits inside a helper that only some code paths reach. 0rpc/zerorpc-python is a clear case. In zerorpc/decorators.py, DecoratorBase._zerorpc_args calls inspect.getargspec(self._functor) and, when that raises TypeError, tries inspect.getargspec(self._functor.__call__). Nothing runs that method until a client asks a server to describe itself: _zerorpc_inspect in zerorpc/core.py calls it for every exposed method, and the zerorpc command line does that when given --inspect. So on Python 3.11 a server starts, answers ordinary calls, and fails only on introspection, with the AttributeError carried back to the client as a remote error.

The agent starts by reproducing exactly that, since the test suite will not. The project's tox.ini lists py26,py27,py34,py35 and runs flake8 zerorpc bin and pytest -v. Installed on Python 3.11 with current pyzmq and gevent, the 80 tests in tests/ report 79 passed and one failed, and the failure is unrelated: test_exception_compat_v1 in tests/test_server.py compares against NameError('donotexist',), a repr with a trailing comma that newer Pythons no longer print. No test calls _zerorpc_inspect. The agent writes one that binds a server with a small class, connects a client and asks for the inspection, watches it fail with the title's error, and keeps it as the regression test.

Then it ports the two calls to inspect.getfullargspec. That is close to a rename here, because _format_args_spec reads the result by position, args_spec[0] for the names and args_spec[3] for the defaults, and both tuples keep those positions. It is not a rename everywhere. The server also exposes _zerorpc_args as a remote method that returns the spec itself, and the full spec has seven fields where the old one had four, including a dictionary of annotations that has to survive msgpack. The agent decides that in the open: either trim the value to the first four fields so older clients see what they always saw, or send all seven and say so.

A search for the removed name finds only half the problem. zerorpc/cli.py calls inspect.formatargspec(*argspec) in two places to print a method's signature, and Python 3.11 removed that function as well. The agent replaces it with a few lines that build the same (a, b=2) text from the names and defaults, and runs the command line's inspect mode against the test server to read the output.

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.11 or later with \"AttributeError: module 'inspect' has no attribute 'getargspec'\". Fix it. First find every use of inspect.getargspec and inspect.formatargspec, and any helper that depends on the old four-field ArgSpec tuple, in the package, its command-line entry points and its tests. For each call site, say which code path reaches it and whether the existing tests cover that path. If they do not, write a test that reproduces the error on the current Python before changing anything. Replace getargspec with inspect.getfullargspec where callers read the result by position or by the args, varargs and defaults names, and with inspect.signature where the code only needs parameter names or binding. Check every consumer of the returned value: if it is unpacked into four names, sent over a network protocol, serialised, or passed to formatargspec, keep the shape those consumers expect and state what you chose. Replace formatargspec with code that prints the same text. Keep the Python versions the project declares working if you can install them; if an old version cannot be installed here, say so and do not drop it silently. Run the test suite before and after on the newest Python available and report failures that existed before your change separately from anything you caused. Do not fix unrelated failures; list them. 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 a small diff and a description that explains more than the diff does. For zerorpc it touches zerorpc/decorators.py, zerorpc/cli.py and one new test file. The description lists each call site with the path that reaches it, then shows what a client now receives from _zerorpc_inspect for a method add(self, a, b=2): three arguments by name, self included as before, with the default attached to b.

It states the wire-format decision for _zerorpc_args and why, and it separates results: the new test fails before the change and passes after, and the one failing test that predates the change is named with its cause and left alone. If the project's oldest declared Pythons cannot be installed in the sandbox, the description says which versions the change was exercised on and which it was not.

A reviewer checks two things. First, that a client built from the last release can still inspect a server built from this branch, since the two ends upgrade at different times. Second, that functions with keyword-only arguments or annotations, which the old function refused, now produce output the command line prints sensibly.

Run it on every CI failure on a new Python version

This error arrives the day a base image or a CI matrix moves to Python 3.11, so the natural trigger is the failing build. Save the prompt above as an automation on the repository's Project with a github trigger on workflow_run.failure. The event is placed after the saved prompt as third-party data and fills in nothing, which is fine here: the prompt already tells the agent what to look for, and the failed run's name and branch tell it where. Only runs caused by a repository owner, member or collaborator fire the trigger, and CI runs from forks never do.

Because the failure hides behind a code path tests may not reach, some teams will see it in production first. In that case send the traceback from your own handler to a webhook trigger with Authorization: Bearer and the trigger's secret. Keep the automation's max runs per hour low: every worker that hits the path raises the same error.

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

Why run it on Rush?

The fix is two lines and the risk is everywhere else. A helper that describes functions has consumers, and here one of them is another process on the far side of a socket. Knowing what that process receives means starting a server and a client and looking, which the agent does in its own sandbox with pyzmq and gevent installed, on a Python new enough to show the error. The commands and their output stay in the session, so the reviewer reads the actual dictionary that came back instead of a claim that it works.

It also keeps the change honest about scope. A suite that was last green on Python 3.5 will show other failures on 3.11, and a person fixing one error on a laptop tends to either fix them all in one unreviewable commit or stop at the first red line. The agent runs the suite before touching anything, records what was already failing, and hands back a pull request that fixes the title's error and a list of the rest.

Frequently asked questions

What replaces inspect.getargspec?

inspect.getfullargspec is the closest match: its first four fields carry the same information as the old tuple, with three more for keyword-only arguments and annotations. inspect.signature is the better choice when code needs parameter names, kinds or argument binding, and it is the only one of the three that follows functools.wraps to the wrapped function.

Can I just rename getargspec to getfullargspec?

Only where the result is read by index or by the args, varargs and defaults names. Code that unpacks it into four variables raises ValueError on seven, and the third field is called varkw where the old one was keywords. Check anything that serialises the tuple or passes it to another function.

The traceback points into a package I installed. What then?

Then the fix is a newer release of that package, or a replacement if it is unmaintained. The agent reads the traceback, finds the first release whose source no longer calls the removed function, and opens a pull request that changes the pin. If no such release exists, it reports that and proposes staying on Python 3.10 for that service until one does.

Why did my tests pass on Python 3.11 if the function is gone?

Because a missing attribute only raises when the line runs. In zerorpc the call is inside a method that only introspection requests reach, and no test makes one. A search of the source for the removed names is the reliable check, and a test for each path found is what keeps it fixed.

Will the code still run on Python 2.7?

getfullargspec does not exist there, so a project that still supports Python 2 needs a small fallback: try the new name and use the old one on AttributeError. The agent keeps whatever versions your setup.py and tox.ini declare, and tells you which of them it could install and run.

More jobs like this: Fix a bug from an alert

Keep reading