On 31 July 2018 at 08:40, Robin Becker <ro...@reportlab.com> 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 seems right to me. Behaviour differs between Python 2 and Python 3: >py -Wall -b Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> b'a' == 'a' __main__:1: BytesWarning: Comparison between bytes and string False >>> ^Z >py -2 Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> b'a' == 'a' True >>> b'a' == u'a' True >>> which is basically the sort of thing that -b should warn about. Specifically the quoted code would end up with a dictionary with 2 entries on Python 3, but 1 entry on Python 2. Paul -- https://mail.python.org/mailman/listinfo/python-list