Pycharm expected type 'optional[bytes]' got 'str' instead
Question:
I am using rsplit
to split up a path name,
rootPath = os.path.abspath(__file__)
rootPath = (rootPath.rsplit('/', 1)[0]).rsplit('/', 1)[0]
But Pycharm warns,
expected type optional [bytes]
, got str
instead
In python doc
, it stated using sep
as the delimiter string.
So how to fix this?
Answers:
It seems like rootPath
is being treated as a bytes object (a small bug maybe?) or the warning is for another part.
In general, what PyCharm and the error is essentially warning you about is that the parameter has to either be None
or bytes
. That’s what Optional
means, Optional[type]
is either None
or type
which in your case is bytes
.
In a simple Python REPL the message is slightly different but the gist is the same:
b'hello/world'.rsplit('/') # error bytes-like object required
Instead you need to supply a byte
separator:
b'hello/world'.rsplit(b'/')
or None
in order to get it to work.
Either there’s a small bug in PyCharm and it’s reporting rsplit
incorrectly here or the warning is for another part of your code.
I came here with the same problem and found a slightly different solution – thought of adding it for anyone that may land into the same issue in future.
rootPath = os.path.abspath(__file__)
rootPath = str(rootPath.rsplit('/', 1)[0]).rsplit('/', 1)[0]
I came across this earlier:
import subprocess
result = subprocess.run(
"ls /etc/systemd/system",
shell=True, capture_output=True, text=True
)
for line in result.stdout.split("n"):
if "banana" in line:
print(line.split(".")[0].strip().split("_"))
PyCharm began highlighting: "n", "banana", ".", "_":
It says it expects Optional[bytes]
or Union[bytes,int]
, and yet this code runs perfectly well in python 3.10, 3.9, and 3.8.
I suspected it was my recent selection of Settings->Editor->Inspections->Code compatibility inspection so I disabled it and restarted that window to no avail.
I have added .decode()
after .stdout
to convert bytes. That quietens PyCharm, but is noise upon noise. In python 3.10 .stdout
is not bytes
but str
and I’d get an error:
AttributeError: ‘str’ object has no attribute ‘decode’.
In the python docs I find the same method names in bytes that I do in str
I am using rsplit
to split up a path name,
rootPath = os.path.abspath(__file__)
rootPath = (rootPath.rsplit('/', 1)[0]).rsplit('/', 1)[0]
But Pycharm warns,
expected type
optional [bytes]
, gotstr
instead
In python doc
, it stated using sep
as the delimiter string.
So how to fix this?
It seems like rootPath
is being treated as a bytes object (a small bug maybe?) or the warning is for another part.
In general, what PyCharm and the error is essentially warning you about is that the parameter has to either be None
or bytes
. That’s what Optional
means, Optional[type]
is either None
or type
which in your case is bytes
.
In a simple Python REPL the message is slightly different but the gist is the same:
b'hello/world'.rsplit('/') # error bytes-like object required
Instead you need to supply a byte
separator:
b'hello/world'.rsplit(b'/')
or None
in order to get it to work.
Either there’s a small bug in PyCharm and it’s reporting rsplit
incorrectly here or the warning is for another part of your code.
I came here with the same problem and found a slightly different solution – thought of adding it for anyone that may land into the same issue in future.
rootPath = os.path.abspath(__file__)
rootPath = str(rootPath.rsplit('/', 1)[0]).rsplit('/', 1)[0]
I came across this earlier:
import subprocess
result = subprocess.run(
"ls /etc/systemd/system",
shell=True, capture_output=True, text=True
)
for line in result.stdout.split("n"):
if "banana" in line:
print(line.split(".")[0].strip().split("_"))
PyCharm began highlighting: "n", "banana", ".", "_":
It says it expects Optional[bytes]
or Union[bytes,int]
, and yet this code runs perfectly well in python 3.10, 3.9, and 3.8.
I suspected it was my recent selection of Settings->Editor->Inspections->Code compatibility inspection so I disabled it and restarted that window to no avail.
I have added .decode()
after .stdout
to convert bytes. That quietens PyCharm, but is noise upon noise. In python 3.10 .stdout
is not bytes
but str
and I’d get an error:
AttributeError: ‘str’ object has no attribute ‘decode’.
In the python docs I find the same method names in bytes that I do in str