You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
open() returns IO[str] instead of io.TextIOWrapper #1229
The solution to your problem is probably to use IO[str], which is compatible with the IO[Any] returned by open() and has (most of) the right methods. It doesn't have the TextIO specific ones (like .encoding, .line_buffering, etc.), so if you want those you should use cast(TextIO, open(...)).
However, I agree that this is an interesting problem and worth thinking about. It means TextIO and BinaryIO are annoyingly impractical to use in real code (a cast is necessary whenever open() is called). The documentation doesn't give many examples or suggest that IO[str] is what most people should use.
Yeah, this is not one of the smoothest APIs. Note that in master, open(fn, 'rb') will now return IO[bytes] while open(fn, 'r') will return IO[str]. (However, if the mode argument is a variable, it returns IO[Any]; and in PY2 they all return BinaryIO regardless of the mode.)
I think this is still wrong though. open() with a recognized mode argument should return BinaryIO or TextIO. That way you can write
f: TextIO=open(fn, 'rb')
Some classes used by the implementation in the io module are subclasses of TextIO or BinaryIO, but you're better off not referencing them.
(Another note: it's not great to use runtime inspection to come up with type names to put into annotations -- things at runtime are a bit different from how things are dine on the static typing side of things.)
changed the title [-]Mypy assumes and only accepts type IO[Any] when it is _io.TextIOWrapper (GVR told me to open this here)[/-][+]open() returns IO[str] instead of io.TextIOWrapper[/+]on Oct 28, 2018
Activity
lincolnq commentedon May 30, 2017
The solution to your problem is probably to use IO[str], which is compatible with the IO[Any] returned by open() and has (most of) the right methods. It doesn't have the TextIO specific ones (like .encoding, .line_buffering, etc.), so if you want those you should use cast(TextIO, open(...)).
However, I agree that this is an interesting problem and worth thinking about. It means TextIO and BinaryIO are annoyingly impractical to use in real code (a cast is necessary whenever open() is called). The documentation doesn't give many examples or suggest that IO[str] is what most people should use.
gvanrossum commentedon May 30, 2017
Yeah, this is not one of the smoothest APIs. Note that in master,
open(fn, 'rb')will now returnIO[bytes]whileopen(fn, 'r')will returnIO[str]. (However, if the mode argument is a variable, it returnsIO[Any]; and in PY2 they all returnBinaryIOregardless of the mode.)I think this is still wrong though. open() with a recognized mode argument should return
BinaryIOorTextIO. That way you can writeSome classes used by the implementation in the
iomodule are subclasses of TextIO or BinaryIO, but you're better off not referencing them.(Another note: it's not great to use runtime inspection to come up with type names to put into annotations -- things at runtime are a bit different from how things are dine on the static typing side of things.)
srittau commentedon Sep 11, 2018
I think any further improvements in typeshed for this are dependent on implementing dependent types, see python/mypy#3062.
[-]Mypy assumes and only accepts type IO[Any] when it is _io.TextIOWrapper (GVR told me to open this here)[/-][+]open() returns IO[str] instead of io.TextIOWrapper[/+]srittau commentedon Oct 28, 2018
This is part of the IO classes restructuring.
Work around python/typeshed#1229
hauntsaninja commentedon Jun 11, 2020
This was fixed in #4146 and others!