This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: DeprecationWarning: invalid escape sequence: Only appears on first run
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: jdufresne, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-04-17 17:19 by jdufresne, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg291805 - (view) Author: Jon Dufresne (jdufresne) * Date: 2017-04-17 17:19
After upgrading to Python 3.6, I'm working towards cleaning up "DeprecationWarning: invalid escape sequence". I've noticed that the Deprecation warning only appears on the first run. It looks like once the code is compiled to `__pycache__`, the deprecation warning does not show. This makes debugging more difficult as I need clean out `__pycache__` directories for the runs to be reproducible.

Example script:

foo.py
```
import bar
```

bar.py
```
s = '\.'
```

First run
```
$ python36 -Wall foo.py 
.../test/bar.py:1: DeprecationWarning: invalid escape sequence \.
  s = '\.'
```

Second run (no DeprecationWarning)
```
$ python36 -Wall foo.py
```

Third run after cleaning
```
$ rm -rf __pycache__
$ python36 -Wall foo.py 
.../test/bar.py:1: DeprecationWarning: invalid escape sequence \.
  s = '\.'
```

I expect the deprecation warning to output on every run.
msg291806 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-04-17 17:41
This warning is emitted by the compiler when it compiles string literals with invalid escape sequences. Once the source is compiled to the bytecode and saved to the .pyc file the compiler no longer involved. You can disable writing compiled bytecode by calling python with option -B.
msg291809 - (view) Author: Jon Dufresne (jdufresne) * Date: 2017-04-17 19:42
I see.

I think if the goal is for developers to see and fix these DeprecationWarnings, it would help if the warnings were reproducible without taking steps different from normal Python development. TBH, this is the first time I've ever used the -B CLI argument.

For an example of where developers saw different sets of warnings which lead to confusion, see https://github.com/dateutil/dateutil/pull/358.
msg291812 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-04-17 20:04
This is how Python works. This can't be changed without changing the compiler, the format of .pyc files (for saving warnings) and importing system. And this may slow down normal compilation due to generating warnings for saving them in .pyc files. I don't think this is worth.
msg291813 - (view) Author: Jon Dufresne (jdufresne) * Date: 2017-04-17 21:47
Understood. Thanks for the response. I'll have to keep this in mind as I debug these warnings in the future.
History
Date User Action Args
2022-04-11 14:58:45adminsetgithub: 74277
2017-04-17 21:47:35jdufresnesetstatus: open -> closed
resolution: not a bug
messages: + msg291813

stage: resolved
2017-04-17 20:04:13serhiy.storchakasetmessages: + msg291812
2017-04-17 19:42:22jdufresnesetmessages: + msg291809
2017-04-17 17:41:28serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg291806
2017-04-17 17:19:05jdufresnecreate