PyCharm warns about string and bytes concatenation, but everything is string

Viewed 265

PyCharm warns me about this:

days = os.listdir(os.path.join(os.path.dirname(__file__), src))
day = days[0]
mystring = day.split('.')[0] + ';' + str(entering)

expected type 'bytes', got 'str' instead

but everything seems to be str for me.. Anyway it works, but that warnings make me suspicious. Any hints?

EDIT Adding more details: Pycharm is version 2019.3.3 in Linux. Entering is int, and src is a path coming from:

p = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description="")
p.add_argument('-src', dest='source', action='store', default='results', help='source path')

args = p.parse_args()
src = args.source
1 Answers

If you look at os.listdir definition1, you can read section:

If path is of type bytes (directly or indirectly through the PathLike interface), the filenames returned will also be of type bytes; in all other circumstances, they will be of type str.

I think this is indirect cause of os.path.join because it returns object implementing PathLike interface. I think PyCharm is assuming that os.path.join will return bytes somehow. Easiest way is just to convert to path to str using str(os.path.join(os.path.dirname(__file__), src)) (although it's already a str)

Related