Skip to content

Conversation

raman118
Copy link

Summary

Added better error messaging for the case when a case statement is used outside of a match statement.

Changes Made

  1. Added a new error code E_INVALID_CASE_OUTSIDE_MATCH to handle this specific syntax error
  2. Added corresponding error message "case statement must be inside match statement"
  3. Updated the error handling in the parser_error_msg function to use this new code when a case statement is found outside a match statement

Problem Solved

Before this change, users would see a generic "invalid syntax" error when accidentally using a case statement outside of a match statement. This provided little guidance on how to fix the issue.

After this change, users will see the more descriptive error message "case statement must be inside match statement", which clearly indicates what needs to be fixed.

Type of Change

  • Bug fix (improves error handling)
  • Enhancement (adds more descriptive error message)
  • Code cleanup (makes error messages more consistent and helpful)

Testing

  • Added tests in test_grammar.py to verify the new error message is displayed correctly
  • Verified existing tests still pass

Checklist

  • Added appropriate tests
  • All tests pass locally
  • Error message is clear and helpful
  • Code follows CPython style guidelines

Fixes gh-138857

@python-cla-bot
Copy link

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@bedevere-app
Copy link

bedevere-app bot commented Sep 12, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@StanFromIreland
Copy link
Member

Hello, please don't open a PR when one is already open (#138858). I am closing this as a duplicate. cc @sobolevn

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve syntax error message for case 1: statement

2 participants