Addition of error message for invalid statements in PEPs

current scenario -

let us take PEP-572,

in

it has some code like this,

[[(j := j) for i in range(5)] for j in range(5)] # INVALID
[i := 0 for i, j in stuff]                       # INVALID
[i+1 for i in (i := stuff)]                      # INVALID

but it does not specify what error will be raised for these.

expected scenario -

the error message is also described in the pep.
so, the above would become,

[[(j := j) for i in range(5)] for j in range(5)] # INVALID

raises,

SyntaxError: assignment expression cannot rebind comprehension iteration variable 'j'
[i := 0 for i, j in stuff]                       # INVALID

raises,

SyntaxError: assignment expression cannot rebind comprehension iteration variable 'i'
[i+1 for i in (i := stuff)]                      # INVALID

raises,

SyntaxError: assignment expression cannot be used in a comprehension iterable expression

in case there are multiple statements which raise the same error message, then could use like,

statement_1 # INVALID
statement_2 # INVALID

both of the above raise,

error_type: error_message

motivation -

it becomes easier to reverse search, like if I get an error, then I could search for it in the pep, and the associated explanation.

I don’t think this would fit in PEPs because they are meant to be specifications, including for alternate implementations of Python. Alternate implementations are not required to output the exact same messages as CPython for syntax errors, and they might be unable to do so if their parsing technology is different.

Also, it wouldn’t help in this case because, if I read the history correctly, these error messages were added in 3.9, whereas PEP 572 was for Python 3.8. In general, the specific error messages can evolve with major CPython versions, whereas once accepted a PEP is a historical document that is no longer meant to evolve.

2 Likes

As @jeanas says, and as PEP 1 makes clear, Standards Track PEPs are generally considered historical documents once accepted (or rejected), and the canonical specification and certainly the living documentation is moved elsewhere, typically somewhere in the Python documentation—in this case, to a section in the Python Language Reference.

Syntax that does not conform to what is specified by definition raises a SyntaxError in the parser (or elsewhere before runtime); you should not programmatically depend on the details of the user-facing error message text as they are not considered stable between Python versions.

1 Like