Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycharm expected type 'optional[bytes]' got 'str' instead

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?

like image 727
daiyue Avatar asked Sep 12 '16 09:09

daiyue


People also ask

What does “optional” mean in the PyCharm error messages?

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:

How to get PyCharm to stop issuing warnings about incorrect function arguments?

If it's getting it wrong, it's best to raise a ticket with them to see if it can be fixed. On the sidebar, click Inspections (under the Editor category) PyCharm should now stop issuing warnings about incorrect function arguments. Doesn't work. Tested on PyCharm 2019.1.2 (Community Edition).

Do you get an error when you run expit in PyCharm?

@Stack: I don't get an error. Only the warning from PyCharm. If you actually run expit (0.0) you get neither an error, nor a warning, just the result. I think the question whether to modify the code or to ignore/disable the warning depends mostly on taste and personal preference.

Why is rootpath being treated as a bytes object in PyCharm?

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.


1 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.

like image 167
Dimitris Fasarakis Hilliard Avatar answered Sep 23 '22 12:09

Dimitris Fasarakis Hilliard