Skip to content

maths/greatest_common_divisor: fix Python 3 except syntax#14932

Open
thejesh23 wants to merge 2 commits into
TheAlgorithms:masterfrom
thejesh23:fix/gcd-syntax-error
Open

maths/greatest_common_divisor: fix Python 3 except syntax#14932
thejesh23 wants to merge 2 commits into
TheAlgorithms:masterfrom
thejesh23:fix/gcd-syntax-error

Conversation

@thejesh23

Copy link
Copy Markdown

Describe your change:

Fixes #14929

maths/greatest_common_divisor.py currently fails to import under any Python 3 interpreter because line 76 uses the Python 2 form except A, B, C: instead of except (A, B, C):. The regression was introduced by the pre-commit autoupdate in commit 840ca00, when the ruff bump to v0.15.4 stripped the parentheses from the previously-correct form.

This one-line change restores the parenthesised tuple form, which:

  • makes the module importable again,
  • unblocks maths.least_common_multiple and maths.primelib, which both import from it and are currently broken by the same SyntaxError,
  • passes all 18 existing doctests.

The wider ruff regression affects several other files (see issue #14929 for the full list); those are being handled separately per the one-file-per-PR rule.

Verified locally with python3.12 -c "import maths.greatest_common_divisor" and python3.12 -m doctest maths/greatest_common_divisor.py -v (18/18 pass).

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

`except IndexError, UnboundLocalError, ValueError:` is Python 2
syntax and raises SyntaxError under Python 3. The module currently
fails to import at all, and downstream modules that depend on it
(maths.least_common_multiple, maths.primelib) also fail with
ImportError.

Restore the parenthesised tuple form `except (IndexError,
UnboundLocalError, ValueError):`, which was the correct form before
a recent pre-commit autoupdate stripped the parentheses.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@algorithms-keeper algorithms-keeper Bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant