Are dicts supposed to raise comparison errors

Chris Angelico rosuav at
Tue Jul 31 11:52:08 EDT 2018

On Wed, Aug 1, 2018 at 1:28 AM, MRAB <python at> wrote:
> On 2018-07-31 08:40, Robin Becker wrote:
>> A bitbucket user complains that python 3.6.6 with -Wall -b prints warnings
>> for some reportlab code; the
>> example boils down to the following
>> ##########
>> C:\code\hg-repos\reportlab\tmp>cat
>> if __name__=='__main__':
>>       d={'a':1}
>>       d[b'a'] = d['a']
>> ##########
>> C:\code\hg-repos\reportlab\tmp>\python36\python -Wall -b
>> BytesWarning: Comparison between bytes and string
>>     d[b'a'] = d['a']
>> I had always assumed that dicts didn't care about the type of keys
>> although some types might cause issue with hashability, but
>> obviously the implementation seems to be comparing b'a' with 'a' (I
>> suppose because they hash to the same chain).
>> Is this code erroneous or is the warning spurious or wrong?
> The warning looks wrong to be.
> In Python 2, u'a' and b'a' would be treated as the same key, but in Python 3
> they are distinct and can co-exist.
> Something for Python's bug tracker, I think!

It's a warning specifically requested by the -b option. The two keys
in question have the same hash, which means they have to be compared
directly; they will compare unequal, but because of the -b flag, the
comparison triggers a warning. If that warning is spurious, *don't use
the -b option*.


More information about the Python-list mailing list