Are dicts supposed to raise comparison errors

Robin Becker robin at reportlab.com
Tue Jul 31 03:40:08 EDT 2018


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 tb.py
if __name__=='__main__':
     d={'a':1}
     d[b'a'] = d['a']
##########


C:\code\hg-repos\reportlab\tmp>\python36\python -Wall -b tb.py
tb.py:3: 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?
-- 
Robin Becker




More information about the Python-list mailing list