Are dicts supposed to raise comparison errors

MRAB python at mrabarnett.plus.com
Tue Jul 31 11:28:30 EDT 2018


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 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?
> 
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!


More information about the Python-list mailing list