Skip to content

Improve syntax error messages for keywords with typos #132449

Closed
@pablogsal

Description

@pablogsal

Currently, when users make typos in Python keywords, they receive generic "invalid syntax" error messages without any helpful suggestions about what might be wrong. This creates a frustrating experience, especially for beginners who might not immediately recognize that they've misspelled a keyword.

For example, typing whille True: instead of while True: would currently result in a generic syntax error without any hint that the problem is a misspelled keyword.

I propose to start raising errors like these:

==================================================

Traceback (most recent call last):
  File "/home/pablogsal/github/python/main/check.py", line 18, in test_mistyped_program
    exec(code_example)
    ~~~~^^^^^^^^^^^^^^
  File "<string>", line 2
    asynch def fetch_data():
    ^^^^^^
SyntaxError: invalid syntax. Did you mean 'async'?

==================================================

Traceback (most recent call last):
  File "/home/pablogsal/github/python/main/check.py", line 18, in test_mistyped_program
    exec(code_example)
    ~~~~^^^^^^^^^^^^^^
  File "<string>", line 6
    result = awaid fetch_data()
             ^^^^^
SyntaxError: invalid syntax. Did you mean 'await'?

==================================================

finally:
Traceback (most recent call last):
  File "/home/pablogsal/github/python/main/check.py", line 18, in test_mistyped_program
    exec(code_example)
    ~~~~^^^^^^^^^^^^^^
  File "<string>", line 6
    finaly:
    ^^^^^^
SyntaxError: invalid syntax. Did you mean 'finally'?

==================================================

Traceback (most recent call last):
  File "/home/pablogsal/github/python/main/check.py", line 18, in test_mistyped_program
    exec(code_example)
    ~~~~^^^^^^^^^^^^^^
  File "<string>", line 4
    raisee ValueError("Value cannot be negative")
    ^^^^^^
SyntaxError: invalid syntax. Did you mean 'raise'?

==================================================

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions