python / python/cpython

Assigning to a keyword generates unclear SyntaxError message

Đang mở
#138,885 0 bình luận 1 reaction 1 người được giao Xem trên GitHub

@StanFromIreland đang làm issue này rồi.

Từ ngày 14/9/2025.

interpreter-core topic-parser type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Feature or enhancement

Proposal:

When one attempts to assign to a keyword, one receives a SyntaxError with the unhelpful message invalid syntax:

>>> class = "Mammalia"
  File "<python-input-0>", line 1
    class = "Mammalia"
          ^
SyntaxError: invalid syntax

I propose changing this to something along the lines of (undecided on what the message should be, adding "reserved" makes it simpler for beginners IMO):

>>> class = "Mammalia"
  File "<python-input-0>", line 1
    class = "Mammalia"
    ^^^^^^^^^^^^^^^^^^
SyntaxError: cannot assign to reserved keyword 'class'

Note that this is already implemented for True, False and None:

>>> True = 1
  File "<python-input-0>", line 1
    True = 1
    ^^^^
SyntaxError: cannot assign to True

Adding this is a simple grammar change, I have a PR ready.
cc @sobolevn

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-138888

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.