All jobs

Fix "AttributeError: module 'werkzeug' has no attribute '__version__'" in Flask Tests

Yes. An agent finds the Flask and Werkzeug pair your install resolves to, tests the pin and the upgrade that fix the test client, and opens a PR with both results.

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

What the agent does

Werkzeug 3.1 removed the module attribute werkzeug.__version__. Flask's test client used it to build a default User-Agent header, so on an affected Flask release every call to app.test_client() raises AttributeError: module 'werkzeug' has no attribute '__version__' from flask/testing.py. The application itself imports and serves requests normally. Only code that creates a test client fails, which in most projects means the whole test suite goes red while production is fine, and that makes the cause hard to guess from the traceback alone.

curityio/flask-of-oil, an OAuth filter for Flask APIs, pins the framework and nothing beneath it. requirements.txt has five lines: requests==2.31.0, cachelib==0.1.1, Flask==2.3.2, pyjwkest==1.4.2 and pytest==7.1.2. There is no Werkzeug line, and Flask 2.3.2 accepts any Werkzeug from 2.3.3 upward, so a fresh install on Python 3.11 now resolves Werkzeug 3.1.9. The 19 tests in tests/test_jwt_validator.py and tests/test_opaque_validator.py still pass. All six tests in tests/test_oauth_filter.py need a client, starting with the fixture on line 183, valid_introspection_app.test_client(), and the full test run ends at 19 passed, 3 failed, 3 errors.

The agent reproduces that in a clean virtual environment and writes down the resolved versions, then tests the two ways out and does not pick one on reputation. The first keeps Flask where it is and adds a Werkzeug line below 3.1 to the requirements. With that pin the install resolves Werkzeug 3.0.6 and all 25 tests of this suite pass. The second moves Flask forward. The attribute lookup is gone from Flask's test client in 2.3.3, so the smallest upgrade is one patch release: with Flask 2.3.3, and equally with 3.0.3 and 3.1.3, the same 25 tests pass on Werkzeug 3.1.9.

Which one goes in the pull request depends on what else the repository pins. A project that states Flask as ==2.3.2 for a reason gets the Werkzeug pin and a note that it is a holding position, because it keeps a transitive dependency behind its current release. A project with no such reason gets the Flask bump, and the agent reads the Flask changelog between the two versions for anything the code uses. In a library like this one it also checks setup.py, where install_requires lists Flask with no bounds, so the published package has the same exposure as the development requirements and the description says so.

Start it with one API call

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

{
  "agent": "claude",
  "input": "The test suite in this repository fails with AttributeError: module 'werkzeug' has no attribute '__version__', raised from flask/testing.py when a test calls app.test_client(). Read every dependency file (requirements*.txt, setup.py, setup.cfg, pyproject.toml, Pipfile, constraints files) and report how Flask and Werkzeug are specified in each, including the case where Werkzeug is not listed at all. Create a clean virtual environment on the Python version CI uses, install the dependencies the way CI does, and report the resolved Flask, Werkzeug, Jinja2 and pytest versions. Run the full test suite without stopping at the first failure and report passed, failed and errored counts, with one full traceback. Then test two fixes in separate environments: (1) keep Flask as pinned and constrain Werkzeug below 3.1; (2) leave Werkzeug unconstrained and raise Flask to the nearest release whose test client no longer reads werkzeug.__version__, trying the next patch release first. For each, report the resolved versions and the test counts. Do not monkeypatch werkzeug.__version__ in conftest.py and do not set the attribute at import time. Choose the fix that changes the fewest versions unless the project already allows newer Flask, apply it to every file that pins these packages so they agree, and explain the choice. If our own code reads werkzeug.__version__ or flask.__version__, replace it with importlib.metadata.version. Open a pull request with the before and after test counts and the version table for both options.",
  "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 that edits dependency files only, unless your own code reads the removed attribute. In the common case that is one changed line: a new Werkzeug<3.1 constraint, or Flask==2.3.2 raised by a patch release. Where a lockfile or a constraints file exists it is regenerated with the project's own tool, and the description names the command.

The description holds a small table with a row for the failing state and a row for each fix: the Flask and Werkzeug versions that were resolved and the passed, failed and errored counts from the full suite. It states which option was applied and why, and what the other one would have changed, so a reviewer who disagrees can ask for the alternative in a follow-up message to the same session without starting over.

Before merging, a reviewer checks that the test counts after the change equal the number of tests in the suite, not merely that nothing errored, and reads the note on duration: a pin below Werkzeug 3.1 fixes today's failure and should carry an issue or a comment that says when to remove it.

Run it on every failed check suite

This failure arrives without a commit of yours: a scheduled CI run or an unrelated pull request installs the new Werkzeug and every client test errors at once. Give the Project an automation with a github trigger on check_suite.failure and a prompt that tells the agent to read the failing job's log and continue only when it contains the __version__ traceback or another error that points at a dependency release. The event is added after the prompt as text. A check suite from a fork does not fire the trigger.

Many pull requests can fail for the same reason within the hour. The automation's max_runs_per_hour bounds how many runs that starts, and the prompt should tell the agent to look for an open pull request that already fixes the pin before it opens another.

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

Why run it on Rush?

The traceback points into Flask, the changelog entry is in Werkzeug, and the line to change is in your requirements file. Connecting the three is quick for someone who has seen it before. Proving which of two fixes is right for this repository takes three clean environments and three full test runs. An agent does those runs in a sandbox, in parallel with whatever else you are doing, and the resolved versions and test counts for each are kept in the session and quoted in the pull request.

A clean environment matters more than usual here. The bug exists only where the resolver has picked Werkzeug 3.1, and a virtual environment created last year still has 3.0 and passes. The sandbox installs from an empty cache, so it sees what CI sees, and the fix is verified against the same resolution that produced the failure.

Frequently asked questions

Which Flask versions are affected?

In a check against Werkzeug 3.1.9, Flask 2.3.2 raised the error and Flask 2.3.3, 3.0.0, 3.0.2, 3.0.3 and 3.1.3 did not. Flask 2.2.5 still reads the attribute on line 116 of its flask/testing.py, so it is affected as well. The agent tests the exact version your project pins.

Does this error affect production?

Not through Flask itself. The attribute is read in flask/testing.py when a test client is constructed, and a served request never runs that code. It does affect production if your own code or an extension reads werkzeug.__version__, which is why the request asks the agent to search for it.

Can I set werkzeug.__version__ myself in conftest.py?

It makes the tests pass and hides the mismatch between the two libraries, so the next incompatibility arrives with no warning. A one-line dependency change fixes the cause. The request above rules the monkeypatch out.

How should my own code read the Werkzeug version now?

With importlib.metadata.version("werkzeug"), which reads the installed package metadata and works on every Werkzeug release. The agent makes that replacement wherever the repository reads the old attribute.

More Werkzeug jobs

More jobs like this: Fix a bug from an alert

Keep reading