Pycharm expected type 'optional[bytes]' got 'str' instead
<p>I am using <code>rsplit</code> to split up a path name, </p>

<pre><code>rootPath = os.path.abspath(__file__)
rootPath = (rootPath.rsplit('/', 1)[0]).rsplit('/', 1)[0]
</code></pre>

<p>But <strong>Pycharm</strong> warns,</p>

<blockquote>
  <p>expected type <code>optional [bytes]</code>, got <code>str</code> instead</p>
</blockquote>

<p>In <code>python doc</code>, it stated <em>using <code>sep</code> as the delimiter string</em>. </p>

<p>So how to fix this?</p>

<p>I'm guessing <code>rootPath</code> is a bytes object and PyCharm is warning you that the <code>sep</code> parameter on <code>rsplit</code> has to either be <code>None</code> or <code>bytes</code>. That's specified in the beginning of the Documentation on <a href="https://docs.python.org/3/library/stdtypes.html#bytes-methods" rel="nofollow">Bytes and ByteArray Operations:</a></p>

<blockquote>
  <p>The methods on bytes and bytearray objects donât accept strings as their arguments, just as the methods on strings donât accept bytes as their arguments.</p>
</blockquote>

<p>That's what <a href="https://docs.python.org/3/library/typing.html#typing.Optional" rel="nofollow"><code>Optional</code></a> means, <code>Optional[type]</code> is either <code>None</code> or <code>type</code> which in your case is <code>bytes</code>.</p>

<p>In a simple Python REPL the message is slightly different but the gist is the same:</p>

<pre><code>b'hello/world'.rsplit('/') # error bytes-like object required
</code></pre>

<p>Instead you need to supply a <code>byte</code> separator:</p>

<pre><code>b'hello/world'.rsplit(b'/') 
</code></pre>

<p>or <code>None</code> in order to get it to work.</p>

python python-3.x split pycharm