Make `SyntaxWarning` for invalid escape sequences better reflect their intended deprecation

It was intended as an example.

Did you mean '\\W'? is concise and also consistent with the SyntaxWarning you get for trying to run 1 is 1: "is" with 'int' literal. Did you mean "=="?. Arguably, you could make that message more verbose by also suggesting isinstance() as an option (as the warning will also get raised with 1 is int and if you’re trying to run code like that, my strong suspicion is that you probably want to use isinstance() and not ==).

In this case, however, there is no such ambiguity with the recommendation of '\\W'. It will work where a raw string might not. A raw string is nicer for certain uses, agreed, but its better to keep the message simple and give a quick immediate fix that is guaranteed to work. I agree with @Rosuav on this.

I appreciate your efforts to condense the message. However, the reason I proposed '\W' is currently an invalid escape sequence. In the future, invalid escape sequences will raise a SyntaxError. is that, if we say categorically, for example, \e is an invalid escape sequence and will break in the future, that’s not necessarily correct. As you highlighted in our other thread, \e is intended to be added to Python after removing invalid escape sequences. Therefore, there is a possibility where we jump from \e raising a warning to \e working perfectly.

The segmentation of the message into two statements: '\e' is currently an invalid escape sequence and In the future, invalid escape sequences will raise a SyntaxError is intended to convey that, right now \e belongs to the category of invalid escape sequences, and, in the future, the category of invalid escape sequences will not work. Which is not the same as saying, in the future, \e will not work.

Otherwise, I do like how you have simplified my message.

I think we can revise it to:
"\W" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\W"?

This is only slightly longer than your revision but does not guarantee to the user that \W will not work in the future, only that invalid escape sequences will not work.

I also switched to double quotations as I use single quotations due to force of habit of Australian English.

2 Likes