Expected type 'optional[bytes]' got 'str' instead

Chris Angelico rosuav at gmail.com
Tue Sep 13 07:13:36 EDT 2016


On Tue, Sep 13, 2016 at 9:06 PM, Daiyue Weng <daiyueweng at gmail.com> wrote:
> Hi, I have the following piece of code,
>
> rootPath = os.path.abspath(__file__)
>
> rootPath = (rootPath.rsplit('\\', 1)[0]).rsplit('\\', 1)[0]
>
>
> PyCharm inspection gave me warning on argument '\\' of the 2nd rsplit,
>
> Expected type 'optional[bytes]' got 'str' instead
>

Well, the first thing I'd do is use os.path.split instead of rsplit :)
Or alternatively, os.path.join using dot-dot. And if that's not
sufficient, I'd reach for pathlib.

Whichever way I do it, I would *not* mix bytes paths and str paths.

ChrisA



More information about the Python-list mailing list