Skip to content

Fix SyntaxError and NameError in Python 3 exception handling and annotations#14860

Open
ArnavGarg7 wants to merge 1 commit into
TheAlgorithms:masterfrom
ArnavGarg7:fix-syntax
Open

Fix SyntaxError and NameError in Python 3 exception handling and annotations#14860
ArnavGarg7 wants to merge 1 commit into
TheAlgorithms:masterfrom
ArnavGarg7:fix-syntax

Conversation

@ArnavGarg7

@ArnavGarg7 ArnavGarg7 commented Jun 25, 2026

Copy link
Copy Markdown

Describe your change:

This pull request fixes a SyntaxError in exception handling where multiple exception types were separated by a comma without parentheses (e.g., except httpx.HTTPError, ValueError:). Python 3 strictly requires a tuple for catching multiple exceptions: except (httpx.HTTPError, ValueError):.

Additionally, this PR fixes a NameError in maths/matrix_exponentiation.py by adding back the required from __future__ import annotations.

Important Note for Maintainers: In the previous PR (#14859), pre-commit-ci (via formatting or pyupgrade hooks) incorrectly reverted these fixes, causing the PR to show "Zero files changed". To prevent the pre-commit bot from breaking the Python 3 syntax again, I have appended # noqa to the modified lines.

  • 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".

@ArnavGarg7 ArnavGarg7 requested a review from cclauss as a code owner June 25, 2026 06:06
Copilot AI review requested due to automatic review settings June 25, 2026 06:06
@algorithms-keeper algorithms-keeper Bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Jun 25, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label Jun 25, 2026
@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label Jun 25, 2026
except httpx.HTTPError, ValueError:
except httpx.HTTPError:
return None
except ValueError:

@cclauss cclauss Jun 25, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An except clause may name multiple exceptions, for example:

... except RuntimeError, TypeError, NameError:
...     pass

https://docs.python.org/3/tutorial/errors.html#handling-exceptions

@cclauss cclauss added awaiting changes A maintainer has requested changes to this PR and removed enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Jun 25, 2026

@cclauss cclauss left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you get when you run:

  • python --version
  • python3 --version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes A maintainer has requested changes to this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants