Repository navigation
Improve the warning message for invalid escape sequences #72315
Description
Activity
In bpo-27364, invalid escape sequences in string literals are deprecated. Currently the deprecation message is not so useful when fixing lots of files in one or more large projects. For example, I have two files foo.py and bar.py:
# foo.py import bar # bar.py print('\d')
It gives: $ python3.6 -W error foo.py Traceback (most recent call last): File "foo.py", line 1, in <module> import bar DeprecationWarning: invalid escape sequence '\d'
My idea is that the warning message can be improved to provide more information. In http://bugs.python.org/issue27364#msg269373 it's proposed to let a linter check such misuses. It's useful within a single project. For a project that depends on lots of external projects, a linter is not enough. Things are worse when __import__, imp or importlib are involved, or sys.path is modified. I have to either add some codes or use a debugger to show which module is imported.
For above reasons, I propose to add at least the filename and the line number to the warning message. For example:
$ ./python -W error foo.py Traceback (most recent call last): File "foo.py", line 1, in <module> import bar File "/home/yen/Projects/cpython/build/bar.py", line 1 print('\d') ^ SyntaxError: (deprecated usage) invalid escape sequence '\d'
With that I can know which file or project I should blame.
Added some of reviewers from bpo-27364 to nosy list.
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory3.7 (EOL)end of lifeend of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement
on Sep 13, 2016 And I'd like to ask Ned: for me it's an improvement of an existing feature, so I guess it can enter the 3.6 branch?
It sounds like a fix but let's see the final patch first. If a core developer wants to apply it to the default branch for 3.7, we can decide whether it should go into 3.6, too.
The error can't be a SyntaxError, it must be a DeprecationWarning. If you can improve the deprecation warning text, I'd be in favor of calling that a fix to the original feature and put it in 3.6. The deprecation warning can even say that this will be a SyntaxError some time in the future.
Hello, and thanks! I'll work on a patch this week, or at most next week. I will make it so that it's completely uncontroversial to apply it to 3.6 as well (won't change the actual feature, only prettify the error message), so no need to worry about that :)
- removedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Sep 13, 2016 See also bpo-28028. Serhiy suggested translating warnings to SyntaxWarning in general. Looks like that may help narrowing down the location of escaping problems.
Besides converting the DeprecationWarning to a Syntax{Error,Warning}, I don't see an easy way to include the offending line (or even file). The place in the code where the strings are created has no idea *where* they are being defined. AIUI, either we special-case this, or we resolve bpo-28028 (but I don't think the latter can go in 3.6).
Are SyntaxWarnings silent by default? If not it can't even go into 3.7.
38 remaining items
I've pushed this to the default branch. I'll watch the buildbots.
Then Ned can decide if this goes in to 3.6.
Searching on GitHub it seems to me that the most frequent issue with supporting Python 3.6 is eliminating or silencing warnings about invalid escape sequences. Any help with this is very important.
As Nick pointed out in an earlier message on this thread and as Serhiy observed on GitHub issues, backporting this patch to 3.6 is a must. Large projects' use of Python 3.6 has shown that it's hard to track down the actual cause of the error; it only makes sense to improve that before the final release.
Serhiy, are you working on bpo-28028 alongside this, or can it be removed from the dependencies?
I agree it would be nice to get this in to 3.6. I'm not sure I'd go so far as to say it's a must and can't wait for 3.6.1. It's a non-trivial change, and it's up to Ned to say if it can go in to 3.6.
If you don't run with -Wall or -Werror, then you won't notice any new behavior with invalid escapes, correct?
Maybe post to python-dev and see if we can get more reviewers?
I agree that the current behavior for 3.6 is very user-unfriendly so I think the risks of making such an extensive change at this point in the release cycle are outweighed by the severity of the problem. So let's get it into 3.6 now; there's still time for it to make 360b3.
Even better than what I was aiming for :)
The patch is working well to identify warnings when running Django's test suite. Thanks!
I'll work on this as soon as I can, coordinating with Ned.
The error message is much better now, thanks you all!
Seems the ^ pointer is not always correct. For example, in the function scope it's correct:
$ cat test.py def foo(): s = 'C:\Program Files\Microsoft' $ python3.7 -W error test.py File "test.py", line 2 s = 'C:\Program Files\Microsoft' ^ SyntaxError: invalid escape sequence \P
On the other hand, top-level literals confuses the pointer:
$ cat test.py s = 'C:\Program Files\Microsoft' $ python3.7 -W error test.py File "test.py", line 1 s = 'C:\Program Files\Microsoft' ^ SyntaxError: invalid escape sequence \P
Is that expected?
Using 259745f9a1e4 on Arch Linux 64-bit
New changeset ee82266ad35b by Eric V. Smith in branch '3.6':
bpo-28128: Print out better error/warning messages for invalid string escapes. Backport to 3.6.
https://hg.python.org/cpython/rev/ee82266ad35bNew changeset 7aa001a48120 by Eric V. Smith in branch 'default':
bpo-28128: Null merge with 3.6: already applied to default.
https://hg.python.org/cpython/rev/7aa001a48120Chi Hsuan Yen:
I'll investigate, and open another issue as needed.
Misc/NEWSso that it is managed by towncrier #552Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: