After updating PyCharm to 2017.1, a new inspection warning started to pop up in a place where everything's seemingly simple and correct. It looks as following:
It looks like open()
doesn't return the expected type for file
parameter, but the code is pretty straightforward and, most importantly, does work as expected (using Python 3.5.2).
Python docs mention a way using StringIO
and it removes the warning indeed, but is it the correct pythonic way to specify output stream for print
? If so, why? Is this warning important to follow?
The only solution I found is to type hint PyCharm correctly:
from typing import IO
with open('output_filename', 'w') as f: # type: IO[str]
print('some text to go to the file...', file=f)
This keeps PyCharm happy and does not in any way impede the correct running of your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With