Skip to content

SyntaxError in maths/greatest_common_divisor.py — invalid Python 3 except clause #14929

Description

@thejesh23

Repository commit

c0db072 (master, latest as of this report)

Python version (python --version)

Python 3.12.13 (also reproduces on 3.13 / 3.14)

Dependencies version (pip freeze)

None required — this is a parse-time SyntaxError, so importing the module fails before any dependency is touched.

Expected behavior

import maths.greatest_common_divisor should succeed and the module's doctests should run, as they do for other files in maths/.

Actual behavior

Line 76 contains invalid Python 3 syntax:

except IndexError, UnboundLocalError, ValueError:
    print("Wrong input")

except A, B: is Python 2 syntax; in Python 3 the exception list must be parenthesised (except (A, B):). The module raises SyntaxError at import time, so it is completely unusable:

$ python3 -c "import maths.greatest_common_divisor"
  File "maths/greatest_common_divisor.py", line 76
    except IndexError, UnboundLocalError, ValueError:
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: multiple exception types must be parenthesized

The failure also breaks downstream modules that depend on greatest_common_divisor:

  • maths/least_common_multiple.py (from maths.greatest_common_divisor import greatest_common_divisor) — fails to import.
  • maths/primelib.py — same.

Root cause

Introduced by the pre-commit autoupdate in commit 840ca00 ("[pre-commit.ci] pre-commit autoupdate (#14325)"). That commit updated astral-sh/ruff-pre-commit from v0.14.14 to v0.15.4, and the new ruff version rewrote the previously-correct except (IndexError, UnboundLocalError, ValueError): into the unparenthesised form. The regression affected several other files in the same commit:

  • digital_image_processing/filters/local_binary_pattern.py
  • divide_and_conquer/convex_hull.py
  • dynamic_programming/catalan_numbers.py
  • project_euler/problem_002/sol4.py
  • project_euler/problem_003/sol1.py, sol2.py, sol3.py
  • project_euler/problem_005/sol1.py
  • project_euler/problem_007/sol2.py
  • web_programming/fetch_well_rx_price.py
  • web_programming/instagram_crawler.py

This issue tracks the fix for maths/greatest_common_divisor.py specifically (per the one-file-per-PR rule). The wider ruff regression may deserve a separate tracker.

Suggested fix

Restore the parenthesised form. A PR is being opened.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions