python / python/cpython

Assigning to a keyword generates unclear SyntaxError message

Offen
#138,885 0 Kommentare 1 Reaktion 1 zugewiesene Person Auf GitHub ansehen

@StanFromIreland arbeitet bereits daran.

Seit 14.9.2025.

interpreter-core topic-parser type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.