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: Confusing error message when trying split bytes.
Type: behavior Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, sahsariga111
Priority: normal Keywords:

Created on 2021-09-02 19:37 by sahsariga111, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bug_in_python.py sahsariga111, 2021-09-02 19:57
Messages (9)
msg400948 - (view) Author: Alex Zaslavskis (sahsariga111) Date: 2021-09-02 19:37
If we will try to split bytes we will got quite strange error in console. 

Traceback (most recent call last):
  File "C:/Users/ProAdmin/Desktop/bug_in_python.py", line 6, in <module>
    byte_message.split(",")
TypeError: a bytes-like object is required, not 'str'

The problem here is that object should be string and in if I convert it to string the error is going. So that mean that there are error in mistake . Correct variant should be :
Traceback (most recent call last):
  File "C:/Users/ProAdmin/Desktop/bug_in_python.py", line 6, in <module>
    byte_message.split(",")
TypeError:  str is required, not a bytes-like object


message = 'Python is fun'
byte_message = bytes(message, 'utf-8')
print(byte_message)
#byte_message.split(",") causes error
str(byte_message).split(",") # works
msg400950 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-09-02 20:28
This is working as designed. The error is telling you that the argument to bytes.split() must be a string:

>>> b''.split(',')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'

>>> b''.split(b',')
[b'']

Same for str.split():

>>> ''.split(b',')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str or None, not bytes

>>> ''.split(',')
['']
msg400960 - (view) Author: Alex Zaslavskis (sahsariga111) Date: 2021-09-02 21:30
I am not really sure that it is working as it should be. Even with your example it gives : 
TypeError: a bytes-like object is required, not 'str'
The problem is why error message says that  bytes-like is  required , when the string is required.
msg400961 - (view) Author: Alex Zaslavskis (sahsariga111) Date: 2021-09-02 21:35
''.split(',') # works as it should be 
b''.split(',') # gives strange error message.

TypeError: a bytes-like object is required, not 'str'

The problem here is that message is saying that for fix error you should use bytes-like object that not a true. In reality the string or None is required .
msg400962 - (view) Author: Alex Zaslavskis (sahsariga111) Date: 2021-09-02 21:40
Moreover according Python docs : This exception may be raised by user code to indicate that an attempted operation on an object is not supported. But also the Python interpreter gives that  a bytes-like object is required, not 'str' that I am already passing , so it says that all fine. But the question is why then TypeError happens .
msg400963 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-09-02 22:16
You said: 
"""
b''.split(',') # gives strange error message.

TypeError: a bytes-like object is required, not 'str'
"""

It is not a strange error message. You passed in a str (the ','), and you should have passed in bytes.
msg400964 - (view) Author: Alex Zaslavskis (sahsariga111) Date: 2021-09-02 22:29
Thanks that really works. 
So as I understand that b''.split() != ''.split()
msg400965 - (view) Author: Alex Zaslavskis (sahsariga111) Date: 2021-09-02 22:31
What about adding alert message for beginners that can stuck with that in docs? To warn others , is that good idea you think. Or it is redundant ?
msg400970 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-09-03 00:05
I (and many others) think the error message is fine the way it is. Also, it's hard for the error to be more specific about the method it's being called on. But if you have some concrete suggestions that can be implemented, you can add them to this issue.
History
Date User Action Args
2022-04-11 14:59:49adminsetgithub: 89250
2021-09-03 00:05:24eric.smithsetstatus: open -> closed

messages: + msg400970
stage: resolved
2021-09-02 22:31:31sahsariga111setmessages: + msg400965
2021-09-02 22:29:15sahsariga111setresolution: not a bug
messages: + msg400964
2021-09-02 22:16:28eric.smithsetmessages: + msg400963
2021-09-02 21:40:01sahsariga111setmessages: + msg400962
2021-09-02 21:35:08sahsariga111setmessages: + msg400961
2021-09-02 21:30:37sahsariga111setmessages: + msg400960
2021-09-02 20:28:26eric.smithsetnosy: + eric.smith, - larry
messages: + msg400950
components: - Argument Clinic
2021-09-02 19:57:10sahsariga111setfiles: - bug_in_python.py
2021-09-02 19:57:05sahsariga111setfiles: + bug_in_python.py
2021-09-02 19:54:43sahsariga111settype: compile error -> behavior
2021-09-02 19:37:44sahsariga111create