Skip to content
Latchkey

Python "SyntaxError" under a newer Python in CI

Code that parses on your local Python raises a SyntaxError on the CI runner because the interpreter version differs. A feature you rely on may be unavailable, or a deprecated construct is now rejected.

What this error means

CI fails at import/collection with "SyntaxError" pointing at valid-looking code, while it runs cleanly on a developer machine.

python
  File "app/handlers.py", line 12
    match command:
          ^
SyntaxError: invalid syntax

Common causes

A feature missing on the runner Python

Syntax like structural pattern matching (3.10+) or except* (3.11+) raises a SyntaxError on an older CI interpreter.

A removed construct on a newer runner Python

A newer CI Python rejects something removed since 2->3 era code, like a bare print statement.

How to fix it

Pin the CI Python to the supported version

  1. Identify the syntax and the minimum/maximum Python it requires.
  2. Set the runner interpreter with setup-python to a supported version.
  3. Test across the full version matrix you claim to support.
.github/workflows/ci.yml
- uses: actions/setup-python@v5
  with:
    python-version: '3.11'

Match the matrix to your declared support

Run CI on every Python you advertise in requires-python so a version-specific syntax error cannot slip through.

.github/workflows/ci.yml
strategy:
  matrix:
    python-version: ['3.10', '3.11', '3.12']

How to prevent it

  • Test on every Python version listed in requires-python.
  • Pin the CI interpreter explicitly with setup-python.
  • Keep requires-python honest about features you use.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →