openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Fix Python 3 syntax error in mockservices container

Open CipherZ3r0 opened this issue 2 months ago • 5 comments

Problem

The mockservices Docker container currently fails to start because docker/mockservices/main.py contains Python 2 exception-handling syntax:

https://github.com/internetarchive/openlibrary/blob/30022332bd7dabb2dd4a87d2a75dbae3e8893629/docker/mockservices/main.py#L425

When the container runs with Python 3, this results in:

SyntaxError: multiple exception types must be parenthesized
Image

As a result, the mockservices container becomes unhealthy and the local Docker stack cannot start correctly.

Expected behavior

The mockservices container should start successfully with the Python version used by the current Docker setup.

Proposed fix

Update the exception handler to Python 3 syntax: except (TypeError, ValueError):

This is a small compatibility fix and does not change the inteended exception handling behavior.

Reproduction

Run: sudo docker compose up -d

Then check: sudo docker compose logs mockservices

The container fails with: SyntaxError: multiple exception types must be parenthesized

CipherZ3r0 avatar Sep 13 '26 15:09 CipherZ3r0

Thank you @CipherZ3r0 for submitting this issue!

⚠️ Contributors, this issue will be ready to work on once:

  • [ ] Triage — maintainers will add Priority: * and Lead: @*
  • [ ] Needs: Investigation — root cause or approach unknown; research required first

Context

This exact symptom — SyntaxError: multiple exception types must be parenthesized — has come up twice before in this repo, and both times the parenthesize-only fix turned out to be the wrong resolution:

  • pyproject.toml#L49 sets target-version = "py314" for Ruff.
  • docker/mockservices/Dockerfile#L1 already builds on python:3.14-slim.
  • Python 3.14 introduced PEP 758, which makes except TypeError, ValueError: (no parens) valid syntax, equivalent to except (TypeError, ValueError):. With target-version = "py314", ruff format actively rewrites parenthesized except-clauses into the bare form, so it's the current intended style in this codebase, not leftover Python 2 syntax.
  • In issue 12827 (closed), the identical error message was traced to Docker images that had not been rebuilt against the updated python:3.14 base, see @cdrini and @hornc comments there, with a PEP 758 citation. A premature parenthesize-only fix (PR 12969) ended up a no-op there because ruff format immediately strips the parens back out under target-version = "py314".
  • Issue 12886 (closed) is the same story for openlibrary/core/db.py, closed once the base image was updated, with a maintainer noting the bare syntax is now correct.

docker/mockservices/main.py#L425 is the line in question, introduced when the mockservices file was added, and it has not been touched by any later commit.


Issue Assessment

Open questions

  • [ ] What does docker compose exec mockservices python --version (or the mockservices container logs at startup) actually report? If it's below 3.14, the container is very likely running a stale/cached image built before the Dockerfile pinned python:3.14-slim, rather than hitting a real code bug.
  • [ ] Was the mockservices image rebuilt fresh (e.g. docker compose build --no-cache mockservices / docker compose build --pull mockservices) before reproducing, or reused from a prior build?

Action items

  • [x] Would parenthesizing the except clause hold as a fix? (Answer: No — with target-version = "py314" in pyproject.toml, ruff format will reformat except (TypeError, ValueError): back to the bare form on the next pre-commit run, exactly as happened with PR 12969 in issue 12827. Any fix here needs to address why the container is not running Python 3.14, not add parentheses back.)

🔔 Stakeholders

@RayBB, dev-experience/Docker lead, tagged on the near-identical issue 12886 for this same class of stale-image-vs-PEP-758 issue.


Full triage checklist
  • [x] Triage — labels applied (Needs: Triage, Needs: Investigation, Module: Docker, Affects: Configuration)
  • [x] Problem / opportunity — clear: mockservices container reportedly fails to start with a SyntaxError
  • [x] Scope — narrow, single line/file
  • [x] Justification — impact stated (local dev stack cannot start)
  • [x] Success & testing criteria — repro steps given (docker compose up -d, docker compose logs mockservices)
  • [ ] Proposal & breakdown — proposed fix is plausible-looking but likely incorrect given target-version = "py314", see Context
  • [ ] If bug: environment & repro steps — no Python/Docker image version or fresh-build confirmation provided
  • [x] Risks, concerns, open questions — identified above
  • [x] Richy context — relevant code, commits, and two near-duplicate issues (12827, 12886) reviewed
  • [ ] References — no logs/screenshots attached beyond the quoted error text

[!NOTE] This comment was automatically generated by Richy, Open Library's issue enrichment assistant, on behalf of @mekarpeles. This is NOT a sign-off. This issue needs lead review before work begins.

openlibrary-bot avatar Sep 13 '26 15:09 openlibrary-bot

just changing except TypeError, ValueError: to except (TypeError, ValueError):

fixes the error but ruff formatting changes it back to except TypeError, ValueError: in pre-commit

CipherZ3r0 avatar Sep 13 '26 15:09 CipherZ3r0

I believe this may be a stale mockservices image that needs to be rebuilt

See the auto response comment:

This exact symptom — SyntaxError: multiple exception types must be parenthesized — has come up twice before in this repo, and both times the parenthesize-only fix turned out to be the wrong resolution

mekarpeles avatar Sep 13 '26 21:09 mekarpeles

yes rebuilding works but changing those syntax also worked.

CipherZ3r0 avatar Sep 14 '26 03:09 CipherZ3r0

Thanks for researching. This is an issue about the the latest python syntax supporting something that looks like an old syntax but unfortunately when we run python from machines with a version in between it hits this error. I've seen this a few times too outside of mock services.

I think maybe we should update our agents problem solving document with info about this so it can quickly resolve in the future. Basically it should always be using the python we use for our docker images.

RayBB avatar Sep 14 '26 06:09 RayBB