I would like to propose that the SyntaxWarning
that is raised when an invalid escape sequence is used be updated to better reflect the fact that the ability of Python users to employ invalid escape sequences is intended to be removed in a future Python release.
At present, if one runs the code path = 'C:\Windows'
in Python, they get this warning: SyntaxWarning: invalid escape sequence '\W'
.
I argue that that warning is not as semantically meaningful as it could be. It does not immediately convey to the untrained and/or uninitiated that path = 'C:\Windows'
will in fact break in a future Python release.
What is a better of way communicating that? How about, SyntaxWarning: '\W' is currently an invalid escape sequence. In the future, invalid escape sequences will raise a SyntaxError. Did you mean '\\W'?
.
That message is a little bit longer but it immediately tells the user, without the need for heading to Stack Overflow or Python documentation, that:
- Although the code runs today, it wonât run soon!
- You can fix the code easily, just add an extra backslash.
Whereas all that SyntaxWarning: invalid escape sequence '\W'
tells me is, at best, hey, thereâs something wrong here, but youâve gotta figure out what that is. Someone could easily read that message and think maybe Python is trying to be helpful and make me double check that I didnât actually mean to type a valid escape sequence like \f
or \n
.
A message like SyntaxWarning: '\W' is currently an invalid escape sequence. In the future, invalid escape sequences will raise a SyntaxError. Did you mean '\\W'?
makes it much more difficult to come away with the message that the Python developers might not like what Iâve just done but it works and itâll keep working forever.
Asides
- If you have an issue with the wording of the proposed message, please suggest an alteration instead of rejecting the overall need to make the message more semantically meaningful.
- This suggestion originates in my thread âPlease don't break invalid escape sequencesâ.