This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Documentation either unclear or incorrect on comparisons between bytes and strings in Python 3
Type: enhancement Stage:
Components: Documentation Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Brian Ward, Mariatta, docs@python, eryksun, ipatrol, martin.panter
Priority: normal Keywords:

Created on 2017-05-22 23:32 by ipatrol, last changed 2022-04-11 14:58 by admin.

Messages (4)
msg294200 - (view) Author: (ipatrol) Date: 2017-05-22 23:32
https://docs.python.org/3/reference/expressions.html#comparisons says that "Strings and binary sequences cannot be directly compared." That would seem to me to imply that an equality between them would raise an exception, as also claimed by https://wiki.python.org/moin/BytesStr

However, that is manifestly incorrect. Bytes and strings can be compared: they are always unequal. This appears to be a result of the operation falling through to the default comparison, which returns False since they are not identical objects. Equality is a comparison, though it is not an order comparison. A brief search of the word "cannot" in the documentation suggest that saying a certain thing cannot be done usually implies that attempting to do so anyway will raise an exception, typically a TypeError. That this is not the case for string-bytes comparisons should be mentioned.
msg294226 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-05-23 05:25
How about "cannot be compared by value" or "cannot be ordered by value"? Emphasizing the value aspect doesn't conflict with the default equality comparison by identity.

Note that starting Python with the -b option causes the bytes type to raise a warning in this case:

    >>> sys.flags.bytes_warning
    1
    >>> 'a'.__eq__(b'a')
    NotImplemented

    >>> b'a'.__eq__('a')
    __main__:1: BytesWarning: Comparison between bytes and string
    NotImplemented
msg294233 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2017-05-23 06:11
Also, “-bb” turns it into an exception, and the same applies to bytes vs int:

>>> b"a" == "a"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
BytesWarning: Comparison between bytes and string
>>> b"a" == 0x61
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
BytesWarning: Comparison between bytes and int

Perhaps the documentation should say “should not be compared” rather than “cannot”. Or perhaps it should mention “-b”.

Mariatta: I don’t think this is relevant to 2.7. In 2.7, you do get an error with bytearray vs unicode, but bytearray is not discussed in this bit of the Py 2 documentation.
msg294267 - (view) Author: Brian Ward (Brian Ward) * Date: 2017-05-23 17:55
The "should" recommendation seems like the right direction, but feels like it needs an explanation (in case someone's trying to run some 2.7 code and is wondering why their comparisons don't work anymore). Unfortunately, it's a little complicated to pile into a section like this. Putting it somewhere else (that includes a reference to -b) might work.

A somewhat related aside is that I've sometimes thought that there ought to be something a little more extensive regarding converting bytes to strings, as it's the kind of thing that can lead you down this road anyway.
History
Date User Action Args
2022-04-11 14:58:46adminsetgithub: 74620
2021-03-13 03:53:49eryksunsettype: behavior -> enhancement
versions: + Python 3.8, Python 3.9, Python 3.10, - Python 3.5, Python 3.6, Python 3.7
2017-05-23 17:55:53Brian Wardsetnosy: + Brian Ward
messages: + msg294267
2017-05-23 08:15:00Mariattasetversions: - Python 2.7
2017-05-23 06:11:26martin.pantersetnosy: + Mariatta, martin.panter
messages: + msg294233
2017-05-23 05:25:35eryksunsetnosy: + eryksun
messages: + msg294226
2017-05-22 23:40:39Mariattasetversions: + Python 2.7, Python 3.5, - Python 3.3
2017-05-22 23:34:38Mariattasetversions: - Python 3.4, Python 3.5
2017-05-22 23:32:59ipatrolcreate