Mypy: str-bytes-safe is not working for logging

Created on 17 Oct 2019  路  4Comments  路  Source: python/mypy

The following code:

from logging import warning

print('%s' % b'test')
warning('%s', b'test')

produces the following output:

$ python3 Test.py 
b'test'
WARNING:root:b'test'
$ mypy Test.py 
Test.py:3: error: On Python 3 '%s' % b'abc' produces "b'abc'"; use %r if this is a desired behavior
Found 1 error in 1 file (checked 1 source file)
$ mypy --version
mypy 0.740
$ python3 --version
Python 3.6.8

The problem in line 3 is detected and reported correctly, but essentially the same problem in line 4 is not.

feature priority-2-low

Most helpful comment

use %r if this is a desired behavior

Can somebody tell where to use "%r"? The message is not helpful at all. It is required to read all the documentation to found what does it mean.

All 4 comments

We can special case some functions using a plugin (we are going to do this at least for str(b'abc')), but warning() is low priority because it will unlikely break something.

use %r if this is a desired behavior

Can somebody tell where to use "%r"? The message is not helpful at all. It is required to read all the documentation to found what does it mean.

use %r if this is a desired behavior

Can somebody tell where to use "%r"? The message is not helpful at all. It is required to read all the documentation to found what does it mean.

Same here.
Does anyone have an example how to change the code to show that this is the desired behavior?

I improved the error message in a recent release of mypy; it now says On Python 3 '%s' % b'abc' produces "b'abc'", not 'abc'; use '%r' % b'abc' if this is desired behavior

Was this page helpful?
0 / 5 - 0 ratings